Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Keep It Simple, Stupid
 
PerlMonks  

Reading and storing multiple lines from a file

by shewang (Initiate)
on Feb 19, 2013 at 17:40 UTC ( #1019629=perlquestion: print w/ replies, xml ) Need Help??
shewang has asked for the wisdom of the Perl Monks concerning the following question:

So I have a config file which I need to read into a perl script and store in variables to use later in the process.

The file looks like so:

----- SECTION=cr NX_NTF_ID=400017 NX_NTF_PRODUCER_ID=cr NX_NTF_PERSISTENT_ID=cr:400017 NX_NTF_NEW_DELAY_LOG= ----- SECTION=cnt NX_NTF_ID=F9F342055699954C93DE36923835A182 UUID String NX_NTF_PRODUCER_ID=cnt NX_NTF_PERSISTENT_ID=cnt:F9F342055699954C93DE36923835A182 NX_NTF_LAST_MOD_DT=02/04/2013 13:52:27

The code I have done to date reads in the file and stores the values in variable names consistent with what they are in the file.

my $input = "..//NX.env"; open(NX_ENV, "<", $input) or die "Can't open input file $input\n"; my %NX_NTF_vars; while (<NX_ENV>) { chomp; if ($_ =~ /^NX_NTF_/) { my ($key, $value) = split("=", $_); $NX_NTF_vars{$key} = $value; }; };

I then use the following to set my variables for later use:

my $NTF_ID = $NX_NTF_vars{'NX_NTF_ID'}; my $NTF_PROD = $NX_NTF_vars{'NX_NTF_PRODUCER'}; my $NTF_PID = $NX_NTF_vars{'NX_NTF_PERSISTENT_ID'};

However, you can probably see the problem here... as there's an entry for NX_NTF_PERSISTENT_ID under the section headed SECTION=cr and an entry under SECTION=cnt.
So I need to know if there's a way of prefixing the $key variables with the name from the SECTION line?

Any advice greatly received!

Comment on Reading and storing multiple lines from a file
Select or Download Code
Re: Reading and storing multiple lines from a file
by toolic (Chancellor) on Feb 19, 2013 at 18:11 UTC
    You could use a hash-of-hashes: perldsc
    use warnings; use strict; my %NX_NTF_vars; my $sec; while (<DATA>) { chomp; if (/^SECTION=(\w+)/) { $sec = $1; } if (/^NX_NTF_/) { my ($key, $value) = split("=", $_); $NX_NTF_vars{$sec}{$key} = $value; } } use Data::Dumper; $Data::Dumper::Sortkeys=1; print Dumper(\%NX_NTF_vars); __DATA__ ----- SECTION=cr NX_NTF_ID=400017 NX_NTF_PRODUCER_ID=cr NX_NTF_PERSISTENT_ID=cr:400017 NX_NTF_NEW_DELAY_LOG= ----- SECTION=cnt NX_NTF_ID=F9F342055699954C93DE36923835A182 UUID String NX_NTF_PRODUCER_ID=cnt NX_NTF_PERSISTENT_ID=cnt:F9F342055699954C93DE36923835A182 NX_NTF_LAST_MOD_DT=02/04/2013 13:52:27
    $VAR1 = { 'cnt' => { 'NX_NTF_ID' => 'F9F342055699954C93DE36923835A182 +UUID String', 'NX_NTF_LAST_MOD_DT' => '02/04/2013 13:52:27', 'NX_NTF_PERSISTENT_ID' => 'cnt:F9F342055699954C93 +DE36923835A182', 'NX_NTF_PRODUCER_ID' => 'cnt' }, 'cr' => { 'NX_NTF_ID' => '400017', 'NX_NTF_NEW_DELAY_LOG' => '', 'NX_NTF_PERSISTENT_ID' => 'cr:400017', 'NX_NTF_PRODUCER_ID' => 'cr' } };
      Thanks very much for both your responses, they both seem to do the job... but how would I go about capturing the values of each in single variables? Something like:
      my $crhandle = $NX_NTF_vars{'cr' -> 'NX_NTF_PERSISTENT_ID'}; my $cnthandle= $NX_NTF_vars{'cnt' -> 'NX_NTF_PERSISTENT_ID'};
        Ah, OK, I got it now:
        my $crHandle = $NX_NTF_vars{'cr'}{'NX_NTF_PERSISTENT_ID'}; my $cntHandle = $NX_NTF_vars{'cnt'}{'NX_NTF_PERSISTENT_ID'};

        Thanks very much for the help!

Re: Reading and storing multiple lines from a file
by blue_cowdawg (Prior) on Feb 19, 2013 at 19:00 UTC

    My solution is a bit more long winded:

    #!/usr/bin/perl -w use strict; use Data::Dumper; my $oldIFS=$/; $/=undef; my $stuff = <DATA>; my ($j1,@sects)=split('-----',$stuff); my $sections={}; foreach my $sect(@sects){ my @parts=split(/[\n]+/,$sect); my $sect=""; my $other_hash={}; foreach my $part(@parts){ next unless $part =~ m@=@; # ignore trash lines my($key,$value)=split("=",$part); if ( $key eq 'SECTION' ) { $sect=$value; } else { $other_hash->{$key}=$value; } } $sections->{$sect}=$other_hash; } print Dumper($sections); __END__ ----- SECTION=cr NX_NTF_ID=400017 NX_NTF_PRODUCER_ID=cr NX_NTF_PERSISTENT_ID=cr:400017 NX_NTF_NEW_DELAY_LOG= ----- SECTION=cnt NX_NTF_ID=F9F342055699954C93DE36923835A182 UUID String NX_NTF_PRODUCER_ID=cnt NX_NTF_PERSISTENT_ID=cnt:F9F342055699954C93DE36923835A182 NX_NTF_LAST_MOD_DT=02/04/2013 13:52:27
    and produces:
    $ ./ftricks.pl $VAR1 = { 'cr' => { 'NX_NTF_NEW_DELAY_LOG' => '', 'NX_NTF_PERSISTENT_ID' => 'cr:400017', 'NX_NTF_ID' => '400017', 'NX_NTF_PRODUCER_ID' => 'cr' }, 'cnt' => { 'NX_NTF_PERSISTENT_ID' => 'cnt:F9F342055699954C93 +DE36923835A182', 'NX_NTF_ID' => 'F9F342055699954C93DE36923835A182 +UUID String', 'NX_NTF_LAST_MOD_DT' => '02/04/2013 13:52:27', 'NX_NTF_PRODUCER_ID' => 'cnt' } };


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2014-04-20 12:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls