Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: file parsing

by martin (Friar)
on Jul 11, 2008 at 05:04 UTC ( #696890=note: print w/replies, xml ) Need Help??

in reply to file parsing

While XML parsing might help a lot if you wanted to process the structure of the file, weeding out stuff between certain markers is a lot easier. I see a classical use case for the flip-flop operator here. You want to copy input to output, skipping some of the input. Seeing certain patterns should switch between the two modes. Perl can do that like this:
#!/usr/bin/perl while (<>) { next if m{^\Q<?xml version="1.0" encoding="UTF-8"?>} ... m{</table>}; print; }
Note the use of \Q to protect a literal string in a regex. The three dots are not something to fill in but precisely three dots here. This is (in scalar context) a flip-flop operator evaluating either the left or the right expression according to its state. To catch beginning and end markers on the same line, you'd have to replace the three dots by two dots. Also look up the -n and the -i switch in perlrun if you like to edit your files in-place with a single short command line.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2016-09-25 02:46 GMT
Find Nodes?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:

    Results (463 votes). Check out past polls.