Re: [PBML] More than one way (was: How to remove a line in data file)
- joach@... wrote:
>Sure. First, here's the code:
> Tom........ this one line works exceptionally well....... print OUT
> grep(!/^19/, <IN>); It removed all lines beginning with 19 and allowed
> all others to be printed to the file.
> I would like to understand this statement a little better could you please
> help me break this down into understandabe parts.....??/
print OUT grep(!/^19/, <IN>);
Note that IN and OUT are open file handles. IN is open for reading on
the input we want to process and OUT is open for writing on the file we
want to generate.
The expression <IN> says to read the file. In a scalar context (eg.,
$rec = <IN>), one line is read from the file. In an array or list
context, the entire file is read, one line to each array element. Okay?
Since grep() expects an array or list as its second argument, our <IN>
is in an array context. So grep() will get to see every line in the
input file, one at a time, in order.
The first argument to grep() is "!/^19/". That says look for lines that
contain a '1' followed by a '9' at the beginning of the line ('^'). The
'!' says, okay now throw the matches away and keep everything else. The
'!' is a NOT operator -- it reverses the sense of the expression that
The grep() function returns an array of the input items for which the
expression in the first argument returns true. In our case, that will
be all lines that *don't* begin with the characters "19". The print()
function gets what grep() returns and writes it, in order, to the file
handle passed to it first. If no file handle is mentioned, the default
is STDOUT (normally the screen, or a file if redirection is used).
> Print out........... what is it's function??There's no need to apologize. I think that's what this list is for. I
> grep........ looks for "true relationship".......
> !........ what does this do......??
> /^19/........ looks for lines begining with 19
> <IN>........... what does this do.....?
> Sorry to be asking all of these beginer type questions....... but it's
> through reading and asking that I have made some progress...... beats just
> downloading the script...... which is where I started. At least now I can
> modify some scripts and understand some of why they work
agree completely that reading, asking questions, and playing with code
is the way to learn. And if my explanation isn't clear, keep asking.