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.
In reply to Re: Read a file into a hash
by kennethk
in thread Read a file into a hash
by fritz1968
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |