[sf-perl] double filter (but short)

Michael Friedman friedman at highwire.stanford.edu
Tue Jul 1 08:45:26 PDT 2008


Well, since reverse() works on the array of lines, but you also want  
to transform each line individually, you probably will need to use  
map().

I haven't tried this, but something like:
print reverse( map {s/^\([^:]*\):/_$1_:/} <> );

maybe?
-- Mike

On Jul 1, 2008, at 8:30 AM, Vicki Brown wrote:

> I have a text file, with lines like this:
>
> carrot: backwards
> banana: ran
> apple: sentences
>
> I am using a very short, sweet bit of Perl to reverse the lines
>
>    perl -e 'print reverse <>'
>
> resulting in
>
> apple: sentences
> banana: ran
> carrot: backwards
>
> I also want to convert the part before each initial colon, so the  
> result is
>
> _apple_: sentences
> _banana_: ran
> _carrot_: backwards
>
> I can do this with
>
>     perl -e 'print reverse <>' |
>        sed 's/^\([^:]*\):/_\1_:/'
>
> or with all Perl
>
>     perl -e  'print reverse <>'  |
>        perl -pe  's/^([^:]*):/_$1_:/'
>
> but I haven't been able to come up with a tidy way to combine the two
> filters into one call to perl. (s//)(reverse) or (reverse)(s//).
>
> Possible?
> -- 
> - Vicki
>
>    ZZZ
> zzZ                   San Francisco Bay Area, CA
> z  |\     _,,,---,,_   Books, Cats, Tech
> zz /,`.-'`'    -.  ;-;;,_  http://cfcl.com/vlb
> |,4-  ) )-,_. ,\ ( `'-'  http://cfcl.com/vlb/weblog
> '---''(_/--'  `-'\_)      http://twitter.com/vlb
> _______________________________________________
> SanFrancisco-pm mailing list
> SanFrancisco-pm at pm.org
> http://mail.pm.org/mailman/listinfo/sanfrancisco-pm

---------------------------------------------------------------------
Michael Friedman                     HighWire Press
Phone: 650-725-1974                  Stanford University
FAX:   270-721-8034                  <friedman at highwire.stanford.edu>
---------------------------------------------------------------------




More information about the SanFrancisco-pm mailing list