Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Count the number of lines in a file

by pastorizah (Initiate)
on May 25, 2004 at 16:47 UTC ( [id://356284]=perlquestion: print w/replies, xml ) Need Help??

pastorizah has asked for the wisdom of the Perl Monks concerning the following question:

Hi I just started using the program, and I am trying to figure out how to count the number of lines and the number of characters in an existing file and write the result to another file. Don't need the shortest way to do it but the most basic way considering I just started using Perl.

Can someone help me?

janitored by ybiC: Retitle from "Reading a text file"

Replies are listed 'Best First'.
Re: Count the number of lines in a file
by BlaisePascal (Monk) on May 25, 2004 at 17:02 UTC
    I was once given a very similar problem as a test to show I knew minimal perl for a tech-support job I once had. It's also very similar to what I would expect from a homework assignment in an intro-to-perl class.

    It's also something I could see someone doing self-study in perl might choose as a sample problem to solve themselves.

    Either way, my answer is going to be the same: Read the documentation.

    Specifically, you'll probably want to open a file for reading and/or writing (check the perl docs for the "open" function), use a while loop to read each line in the file one by one (check the docs for the "while" statement), find the length of the line read (can you guess the function name to find the length of a string?), and possibly look up how to print stuff to a file.

    It's sort of cheating to do system("wc -lc $infile > $outfile"); in my opinion.

Re: Count the number of lines in a file
by sacked (Hermit) on May 25, 2004 at 17:10 UTC
    perldoc perlintro (section under "Files and I/O")

    perldoc -q 'number of lines in a file'

    perldoc -f length

    perldoc perlvar (see $.)

    --sacked
Re: Count the number of lines in a file
by nobull (Friar) on May 25, 2004 at 17:11 UTC
    You can get the size of a file with -s but that's not the same as the number of characters unless the file uses an encoding that is always one byte per character.
    #!/bin/perl use strict; use warnings; my $chars = 0; my $lines = 0; while (<>) { $chars += length; $lines ++; } print "$chars $lines\n";
    You can call this script with the files as standard input and standard output.

    If you want to open the files within the Perl script instead use the open() function.

Re: Count the number of lines in a file
by talexb (Chancellor) on May 25, 2004 at 17:06 UTC

    It sounds like to me that you haven't tried any of the things in the Perl book that might answer this question. You do have a Perl book, don't you? I can highly recommend the fine series of books from O'Reilly.

    In any event, the variable $. tells you what line you're on, so printing that out after reading through the file would tell you what number the last line of the file was.

    #!/usr/bin/perl -w use strict; while(<>){} print "Last line number was $.\n";

    If you also want to fine out how many characters there were, you'd add something to count the length of each line:

    #!/usr/bin/perl -w use strict; my $totalLength = 0; while(<>){$totalLength += length($_);} print "Last line number was $.\n"; print "Total length was $totalLength\n";

    I haven't tested this code; don't know if it works or not. I'll leave that up to you. Play around. See what works. Read that Perl book. Try stuff, and come ask questions when you get stuck, and not before.

    Alex / talexb / Toronto

    Life is short: get busy!

Re: Count the number of lines in a file
by Plankton (Vicar) on May 25, 2004 at 17:01 UTC
    You don't really need perl for this ...
    $ wc -l existing_file > another_file
    ... but you can if you want ...
    #!/usr/bin/perl -w my $file = shift; my @cmd = ( "wc", "-l", $file ); system( @cmd );

    Plankton: 1% Evil, 99% Hot Gas.
Re: Count the number of lines in a file
by fletcher_the_dog (Friar) on May 25, 2004 at 17:56 UTC
    Here is one way to do it:
    use strict; my $txt = do{ local $/;<>}; # slurp file from the cmd line my $lines = split /\n/,$txt; # use split in scalar context my $chars = length($txt); # find length of whole text print "Chars = $chars Lines = $lines\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://356284]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-18 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found