If you want to have all records in memory at once (not just one record at a time), you can simply declare an array before the first while loop, and then after the fields are loaded into %record just push @array, { %record }; to build an array of hashes (AoH), and get to individual fields of a record like this: $array[0]{"a"}open( IN, $filename ) or die "$filename: $!"; { local $/ = ''; # cf. perldoc perlvar about $/ and "paragraph mode +" while (<IN>) # read a whole record into $_ { @fields = split( /\](\w)\[/ ); # use parens to capture the let +ters shift @fields; # split puts an empty element before ']a[', so +drop that my %record = @fields; # convert array to key=>value hash # you now have tags as hash keys, strings as hash values: # $record{"a"}==1, $record{"b"}=="FORTUNE BAY", etc. # to use as you see fit. } }
I wouldn't use "$a" or "$b" as names for scalar variables like you suggested -- this can get messed up if you use the "sort" function in the same scope as these variables.
If the input data varies from file to file or record to record regarding the features listed above, you'll need to tweak this approach (or tweak the data before using it).
(update: I simplified the split regex so that it's easier to read -- and it accepts a wider range of tags than appears in the OP data, which probably won't cause a problem. (ahem...) Then I updated again to get the bracketing right in that regex.)
In reply to Re: Tag pattern matching
by graff
in thread Tag pattern matching
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |