in reply to How To Count Lines In File?

If you're on a Unix box, or a Win32 box with the GNU tools installed, you can always
my $lines = `wc -l $filename`; $lines =~ /(d+)\s/; $lines = $1;


Cheers,
ibanx

$ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

Replies are listed 'Best First'.
Re^2: How To Count Lines In File?
by Aristotle (Chancellor) on Jan 27, 2003 at 00:06 UTC
    But you shouldn't. You're neither checking whether your attempt to run wc succeeded nor whether the pattern matched. Under these circumstances, $lines may wrong be assigned whatever was in $1 from a previous match. Don't use $1 blindly. Always guard your matches with an if or assign the captures to a list. See Gilimanjaro's snippet.

    Makeshifts last the longest.

Re: How To Count Lines In File?
by Cody Pendant (Prior) on Jan 26, 2003 at 23:59 UTC
    I think I should point out before Monks chime in with 347 different ways to count the lines in a file, that my question is, more specifically, "why is this featured in the FAQ as the suggested way?" is it better, and in what way, file I/O, speed, what?


    --
    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.” M-J D

Re: Re: How To Count Lines In File?
by Gilimanjaro (Hermit) on Jan 26, 2003 at 23:24 UTC
    Or even:

    my ($lines) = `wc -l $filename` =~ /(\d+)/;