> -----Original Message-----
> From: Jason Normandin [mailto:[EMAIL PROTECTED]
> Sent: Thursday, 26 February 2004 10:56 AM
> To: Perl Beginners
> Subject: Count the number of lines in a file without actually
> iterating through the file
>
> Hi List.
>
>
>
> Is there a way to determine the number of lines in a file
> without actually
> iterating through the file and incrementing a file?
>
>
No. Part of the problem is that different operating systems
have different ideas of what constitutes a line, hence you
have to read every byte to make sure::
Unix : the '0x0a' or linefeed ends a line'
DOS : the '0x0d 0x0a' or carriage-return linefeed sequence ends a line.
hence an '0x0a' on its own should not count.
CYBER: zero padded 60bit word constitues an end of line. you cannot
search the data for 'end of line' characters, because an end of
line is the ABSENCE of character data which you can only
identify
by scanning the file.
>
> I found the following on perlmonks.org, it works great but
> this is command
> line syntax
>
> :
>
> perl -lpe '}{*_=*.}{' file
>
>
>
> How could I integrate this into a script without invoking
> another instance
> of Perl?
>
Lets look at this a moment:
perl -lpe '}{*_=*.}{' file works this way:
perl -e expect the rest of the command to be a script to run, hence
'}{*_=*.}{' is the script.
perl -l chomps input, and assigns 'output record separator = input
record separator'
or in english, 'print' will always print a newline at the end of a
line.
perl -p wraps
while(<>) {
your code
} continue {
print || die "............"
}
Now, if we insert your code, we get:
while(<>) {
} { *_=*. } {
} continue {
print
}
which is actually
while( <> ) {}
{
*_=*.
} continue {
print
}
Which is actually an obfuscated way to say:
while(<>) { } # scan file, do nothing with the input
print $.
Hence, this script ITERATES over the entire file/input stream, and
at the end, prints the CURRENT INPUT RECORD NUMBER. Naturally the
"CURRENT INPUT RECORD NUMBER" at the end of file happens to equal
the number of lines in the file!
voila.
Please don't use obfuscated code written by insane people until you
know enough about perl to realised whatever is going on is not
necessarily safe or portable.
David
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>