From: WC -Sx- Jones <[EMAIL PROTECTED]> > Hanson, Rob wrote: > >>Is there a way to determine the number of lines > >>in a file without actually iterating through the > >>file and incrementing a file? > > > > > > > # No silver bullet; but shorter - > > undef $/; # Slurp; > > foreach $target (@ARGV) { > @lines = (); > > open (HTML_FILE, "<$target") or die "owie"; > @lines = split(/\n/, <HTML_FILE>); > > print "In $target - Seen: ". ($#lines + 1) ." lines...\n"; > } > > __END__
Rather inefficient. First you slurp the file and then you split it? Why? You could do it like this instead : foreach $target (@ARGV) { open (HTML_FILE, "<$target") or die "owie"; my @lines = <HTML_FILE>; print "In $target - Seen: ". scalar(@lines) ." lines...\n"; } Even shorter, slightly more efficient, but still very memory hungry. (The scalar() is not necessary, thanks to the dots @lines would be in scalar context anyway. And since the @lines is lexicalized into the loop I do not have to explicitely clear it.) Since you don't actually need the lines you might also do it like this: foreach $target (@ARGV) { open (HTML_FILE, "<$target") or die "owie"; () = <HTML_FILE>; print "In $target - Seen: ". $. ." lines...\n"; } I'd be worried though that Perl might load the whole file and construct the list anyway so I'd rather use foreach $target (@ARGV) { open (HTML_FILE, "<$target") or die "owie"; 1 while <HTML_FILE>; print "In $target - Seen: ". $. ." lines...\n"; } Jenda ===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz ===== When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>