Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
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>


Comment on Re^3: Match on line, read backwards to opening xml tag then forward to closing tag
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2015-07-06 01:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls