Some hints:
- Check the "perlvar" man page to read about the "INPUT_RECORD_SEPARATOR" variable, "$/". You can set it to read each block of lines (up to the next blank line) instead of one line at a time, so each element of your "@all" array contains a full config record.
- Look up the "split" function (perldoc -f split). After reading each block of lines into each array element, separate the lines ( split /\n/), then split each non-initial line into "attribute" and "value" ( split /=/).
- You can load your array of hashes while reading the file, so you don't end up with two copies of the data in memory and you don't need to do repeated loops over the same set of data.
At the risk of short-cutting your exploration of the essential man pages, it could end up looking like this:
my @AoH_attribs;
open( FILE, "<$file" );
{
local $/ = ''; # empty string is "magic" for "paragraph-mode"
while (<FILE>) # read a group of lines into $_
{
my %hash = ();
my $itemname = ( /^\[(.*?)\]/ ) ? $1 : 'NO_KEY';
next if ( !/=/ or $hashkey eq 'NO_KEY' );
$hash{item} = $itemname;
for my $line ( split /\n/ ) {
next unless ( $line =~ /=/ );
my ( $attrib, $value ) = split /=/, $line;
$hash{$attrib} = $value;
}
push @AoH_attribs, { %hash }; # note use of curly braces
}
}
close FILE;
# now, @AoH_attribs contains a list of hash refs, one for each input b
+lock;
# you can access each hash like this:
for my $itemref ( @AoH_attribs ) {
my %itemhash = %$itemref;
print "$_ = $itemhash{$_}\n" for ( sort keys %itemhash );
print "\n";
}
There are other ways as well.
(update: fixed syntax error in code)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.