Here's a simple method that doesn't require you to know what tags are in each section, or even what order the tags you are interested in appear. It is also easily extended to handle more tags of interest. The output is an array of hashes:
#! perl -slw
use strict;
use Data::Dump qw[ pp ];
my @accts;
{
local $/ = 'TAGEND';
while( <DATA> ) {
m[
(?=.* FIRSTNAME \s+ ( \S+ ) )
(?=.* LASTNAME \s+ ( \S+ ) )
(?=.* ACCOUNT \s+ ( \S+ ) )
(?=.* BILLAMOUNT \s+ ( \S+ ) )
]xsm and push @accts, {
firstname => $1,
lastname => $2,
account => $3,
billamount => $4,
};
}
}
pp \@accts;
__DATA__
TAGSTART
FIRSTNAME DAVID
LASTNAME RHODES
UNKNOWNTAG SOMEVALUE
UNKNOWNTAG SOMEVALUE
UNKNOWNTAG SOMEVALUE
ACCOUNT 1000901
UNKNOWNTAG SOMEVALUE
UNKNOWNTAG SOMEVALUE
BILLAMOUNT 4200
UNKNOWNTAG SOMEVALUE
TAGEND
TAGSTART
LASTNAME RHODES
UNKNOWNTAG SOMEVALUE
ACCOUNT 1000902
BILLAMOUNT 10000
UNKNOWNTAG SOMEVALUE
UNKNOWNTAG SOMEVALUE
FIRSTNAME MARY
UNKNOWNTAG SOMEVALUE
TAGEND
TAGSTART
FIRSTNAME BILL
LASTNAME HICKOK
UNKNOWNTAG SOMEVALUE
UNKNOWNTAG SOMEVALUE
ACCOUNT 1000903
UNKNOWNTAG SOMEVALUE
UNKNOWNTAG SOMEVALUE
UNKNOWNTAG SOMEVALUE
BILLAMOUNT 1200
TAGEND
TAGSTART
FIRSTNAME FRED
UNKNOWNTAG SOMEVALUE
LASTNAME BLOGGS
UNKNOWNTAG SOMEVALUE
ACCOUNT 1000909
UNKNOWNTAG SOMEVALUE
BILLAMOUNT -1
UNKNOWNTAG SOMEVALUE
TAGEND
And the output: C:\test>junk47.pl
[
{
account => 1000901,
billamount => 4200,
firstname => "DAVID",
lastname => "RHODES",
},
{
account => 1000902,
billamount => 10000,
firstname => "MARY",
lastname => "RHODES",
},
{
account => 1000903,
billamount => 1200,
firstname => "BILL",
lastname => "HICKOK",
},
{
account => 1000909,
billamount => -1,
firstname => "FRED",
lastname => "BLOGGS",
},
]
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|