in reply to grep from file

You could use grep and s with a funky regex, but I think map would be simpler...

my @cxx = map { /(\w+\.cxx)@@/ ? ($1) : () } <FILE>;

Although, if all you're going to do with @cxx is print it out to a new file, you can avoid building a temporary array with something like...

print NEWFILE map { /(\w+\.cxx)@@/ ? ("$1\n") : () } <FILE>;

    --k.


Replies are listed 'Best First'.
Re^2: grep from file
by Aristotle (Chancellor) on Jul 03, 2002 at 19:12 UTC
    Some additional notes: it actually gets even simpler. my @cxx = map /(\w+\.cxx)@@/, <FILE>; Lines that don't match will not return anything, so they simply fall through the cracks. Lines that match return a list of captured strings, of which there's only one here, so we get exactly what we want. For the latter snippet I'd advise using <> so that you can take input from an arbitrary number of files or optionally from STDIN. By printing to STDOUT the user can redirect output to wherever he wants, including piping to another command. Not only is this much more flexible, you can also skip typing out a lot of file interaction and error checking code. The entire script is now reduced to a single line: print map /(\w+\.cxx)@@/, <>; That's all it takes. Pass it to perl -e and you have a perfect oneliner. :)

    Makeshifts last the longest.