Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Read a file into a hash

by fritz1968 (Sexton)
on Sep 28, 2010 at 13:53 UTC ( #862416=perlquestion: print w/replies, xml ) Need Help??

fritz1968 has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I want to read the following file into a hash, minus the comment lines (#):

# config file
port: 8888
logfile: /data/log


Also, if the hash file is called %hash1, how would I reference the port number in the hash one the above file is read in? Is it as simple as:

my $serverPort = $hash1{port};

Thanks in advance for your help.

Replies are listed 'Best First'.
Re: Read a file into a hash
by kennethk (Abbot) on Sep 28, 2010 at 14:06 UTC
    I want to read the following file into a hash, minus the comment lines (#):

    You should read perlopentut for an intro to interacting with files if you are not familiar with opening and reading files. I would read the file in line by line. I would then use next to skip lines starting with '#'. Finally, I would split on a semicolon with a maximum of two terms, based on what you've posted. See perlretut if you are not familiar with regular expressions - they are a core part of Perl and incredibly powerful once you get the hang of them.

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %data; while my $line (<DATA>) { next if $line =~ /^#/; my ($key,$value) = split /:/, $line, 2; $data{$key} = $value; } print Dumper \%data; __DATA__ # config file port: 8888 logfile: /data/log

    how would I reference the port number in the hash one the above file is read in?

    The short answer is yes. In general, for this sort of question, you should write up a small test script to test the answer in a script. It is also discussed in perldata, the intro to Perl data types.

    As a side note, please wrap all code, input and output in <code> tags so things do not get mangled between posting and display. See Writeup Formatting Tips.

Re: Read a file into a hash
by merzy (Scribe) on Sep 28, 2010 at 18:57 UTC
    There are also various modules to simplify the parsing of a config file. Config::Fast appears to be pretty straightforward.
Re: Read a file into a hash
by sundialsvc4 (Abbot) on Sep 28, 2010 at 19:11 UTC

    If you are dealing with a “well-known format,” such as (say...) a Windows .INI file or an OS/X plist, look for an existing CPAN module that is specific to that format.

Re: Read a file into a hash
by suhailck (Friar) on Sep 28, 2010 at 14:06 UTC
    Here is a solution using regex grouping

    perl -le '$_="port: 8888";$hash1{$1}=$2 if m/(.*)\s*:\s*(.*)/;print $h +ash1{port}' 8888

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://862416]
Approved by kennethk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2022-05-21 02:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (76 votes). Check out past polls.

    Notices?