CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Trying to understand someone elses code

 



viored
New User

May 29, 2013, 2:06 PM

Post #1 of 6 (570 views)
Trying to understand someone elses code Can't Post

So a very verbose individual in another forum has informed the world that this is the best way to count the number of lines in a file


Code
#!/usr/bin/perl 
use strict;
use warnings;

while (<>) {}
print "$.\n";


however, he neglected to mention where I would put the file I'm interested in, say, file.txt. Can anyone help me out on this?


Laurent_R
Veteran / Moderator

May 29, 2013, 2:34 PM

Post #2 of 6 (569 views)
Re: [viored] Trying to understand someone elses code [In reply to] Can't Post

If your script is called number_of_line.pl, just run it as follows:


Code
perl number_of_line.pl file.txt

It will print the number of lines in file.txt.


viored
New User

May 29, 2013, 2:37 PM

Post #3 of 6 (568 views)
Re: [Laurent_R] Trying to understand someone elses code [In reply to] Can't Post

I'm trying to do it within a script so I can use it elsewhere.


FishMonger
Veteran / Moderator

May 29, 2013, 2:45 PM

Post #4 of 6 (564 views)
Re: [viored] Trying to understand someone elses code [In reply to] Can't Post

That is one method, but not the best.

This is a FAQ.
perldoc -q "How do I count the number of lines in a file"

Code
Found in C:\strawberry\perl\lib\perlfaq5.pod 
How do I count the number of lines in a file?
(contributed by brian d foy)

Conceptually, the easiest way to count the lines in a file is to simply
read them and count them:

my $count = 0;
while( <$fh> ) { $count++; }

You don't really have to count them yourself, though, since Perl already
does that with the $. variable, which is the current line number from
the last filehandle read:

1 while( <$fh> );
my $count = $.;

If you want to use $., you can reduce it to a simple one-liner, like one
of these:

% perl -lne '} print $.; {' file

% perl -lne 'END { print $. }' file

Those can be rather inefficient though. If they aren't fast enough for
you, you might just read chunks of data and count the number of
newlines:

my $lines = 0;
open my($fh), '<:raw', $filename or die "Can't open $filename: $!";
while( sysread $fh, $buffer, 4096 ) {
$lines += ( $buffer =~ tr/\n// );
}
close FILE;

However, that doesn't work if the line ending isn't a newline. You might
change that "tr///" to a "s///" so you can count the number of times the
input record separator, $/, shows up:

my $lines = 0;
open my($fh), '<:raw', $filename or die "Can't open $filename: $!";
while( sysread $fh, $buffer, 4096 ) {
$lines += ( $buffer =~ s|$/||g; );
}
close FILE;

If you don't mind shelling out, the "wc" command is usually the fastest,
even with the extra interprocess overhead. Ensure that you have an
untainted filename though:

#!perl -T

$ENV{PATH} = undef;

my $lines;
if( $filename =~ /^([0-9a-z_.]+)\z/ ) {
$lines = `/usr/bin/wc -l $1`
chomp $lines;
}



hwnd
User

May 29, 2013, 2:56 PM

Post #5 of 6 (559 views)
Re: [FishMonger] Trying to understand someone elses code [In reply to] Can't Post

Here's one way.


Code
#!/usr/bin/perl  

use strict;
use warnings;

my $n = 0;

open my $fh, '<', 'file.txt' or die "failed: $!";
++$n while (<$fh>);
close $fh;

print "Number of lines: $n\n";



Laurent_R
Veteran / Moderator

May 29, 2013, 3:03 PM

Post #6 of 6 (557 views)
Re: [viored] Trying to understand someone elses code [In reply to] Can't Post


In Reply To
I'm trying to do it within a script so I can use it elsewhere.


Please explain what you mean.

If you need a reusable function that computes a line count on a file, you could do something like that (untested):


Code
my $file_size = line_count ($my_file_name); 

sub line_count {
my $file_name = shift;
open my $FH, "<', $file_name or die "could not open $file_name $! \n";
my $count;
while (<$FH>) {count ++;}
return $count
}

But this is just an example, you could also use the $. special variable (line number) or other things, Fishmonger has shown some other ways.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives