[tpm] find, manipulate, then output

Peter Vereshagin peter at vereshagin.org
Mon May 28 08:51:32 PDT 2012


Hello.

2012/05/28 11:13:00 -0400 Antonio Sun <antoniosun at lavabit.com> => To James E Keenan :
AS> >> perl -ne 'print $2 . ", ". $1. "\n" while(/.../)'
AS> >>
AS> >> But I really can't work out the rest now.
AS> >> Please help.

Sure.

perl -Mstrict -wE 'my ( $blob, $fname, $lname ) = map {""} 0 .. 2; while ( my $str = <> ) { $blob .= $str; if ( $blob =~ m/<(first|last)-name[^>]*>([^>]*)</ ) { my ( $kind => $name ) = ( $1 => $2 ); $name =~ s/^\s+|\s+$//g; if ( $kind eq "first" ) { $fname = $name; } else { $lname = $name; }  $blob = ""; } if ( $fname and $lname ) { print "$lname, $fname\n"; ( $fname => $lname ) = map {""} 0 .. 1; } }'

AS> >> Here is an example that you can work on. Given the following input,
AS> >> I want to output, "<last-name>, <first-name>" on each line.
AS> >> I.e., the output would be:
AS> >>
AS> >> Franklin, Benjamin
AS> >> Melville, Herman
AS> >>
AS> >> Thanks
AS> >>
AS> >> <bookstore>
AS> >> <book genre="autobiography">
AS> >> <title>The Autobiography of Benjamin Franklin</title>
AS> >> <author>
AS> >> <first-name>Benjamin</first-**name>
AS> >> <last-name>
AS> >>   Franklin</last-name>
AS> >> </author>
AS> >>
AS> >
AS> > It looks like you are trying to roll your own XML parser.  Why?
AS> >
AS> 
AS> Thank you James for you reply.
AS> 
AS> I am fully aware of Perl's XML parser and XPATH handling, and I admit that
AS> it'll be much easier using the XPATH. However, my question was regarding
AS> working on the strings found and output the processed content, the Perl
AS> way. The enclosed XML was just an example. It can well be anything than
AS> XML.

Try Marpa::XS?

For your particular XML I'd take SAX kind of, say, XML::LibXML or Expat::XS.

AS> Thanks

--
Peter Vereshagin <peter at vereshagin.org> (http://vereshagin.org) pgp: A0E26627 


More information about the toronto-pm mailing list