Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Parse tags into an array

by BrowserUk (Pope)
on Jan 15, 2013 at 16:33 UTC ( #1013425=note: print w/ replies, xml ) Need Help??


in reply to Parse tags into an array

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.


Comment on Re: Parse tags into an array
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2014-10-01 08:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (391 votes), past polls