Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Count the number of lines in a file

by BlaisePascal (Monk)
on May 25, 2004 at 17:02 UTC ( [id://356292]=note: print w/replies, xml ) Need Help??


in reply to Count the number of lines in a file

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-19 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found