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!


In reply to Reading and storing multiple lines from a file by shewang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.