Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: Match on line, read backwards to opening xml tag then forward to closing tag

by furry_marmot (Pilgrim)
on Nov 16, 2011 at 20:08 UTC ( #938458=note: print w/replies, xml ) Need Help??


in reply to Re^2: Match on line, read backwards to opening xml tag then forward to closing tag
in thread Match on line, read backwards to opening xml tag then forward to closing tag

I want it to loop through and store each <DataRecord>...</DataRecord>. Then, if it matches on 4 digits followed by a forward slash I want it to output the whole <DataRecord> to screen, not just the matched lines from second filter.

Okay, try this. You were very close, but it seems a bit more complicated than necessary. Also, is the cache just to hold the matches until you print them? If so, you could eliminate that step entirely.

open(FILE, "< $FILE") or die "ERROR: $!"; my $data; { local $/=undef; $data=<FILE> } while ( $data =~ m{<DataRecord>(.+?)</DataRecord>}sg ) { my $rec = $1; if ( $rec =~ m{\d+/\d+} ) { push @cache, $rec; print "$rec"; } } close (FILE);
Prints:
$ test.pl <Data>123456</Data> <Data>123456</Data> <Data2>123456</Data2> <Data>1234/3456</Data> <Data>123456</Data> <Data>1234/3456</Data> <Data3>123456</Data3> <Data>123456</Data>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://938458]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2016-12-04 05:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (61 votes). Check out past polls.