Hi guys, just started perl programming. I have a few questions first off. I am reading in a txt file and I want to ignore all blank lines. I also want to take a certain element from each line and add them all up and then get the average. Here is what I have so far.

Re: [BillKSmith] skip new lines and some other beginner questions
[In reply to]

Can't Post

thank you for your reply. just wondering how would you process the file one line at a time like you suggested? Also, one more quick question. how do i invoke a subroutine from the cmd line? for example, if i put in "perl -w myprog.pl -b" that would invoke the subroutine "b". and if i put in "perl -w myprog.pl -b -a" that would invoke subroutines a and b. I'm using GetOpt right now but i'm not sure how to use it with subroutines.. I've been searching all day

Re: [b3asttt] skip new lines and some other beginner questions
[In reply to]

Can't Post

Iterating over a file one line at a time:

Code

while (my $line = <FILE>) { chomp($line); # ...

The main advantage it that your code will continue to work even if your file has 100 million lines, whithout concuming more memory than what is needed to store one line. While your version slurping all lines into an array will use very large amount of memory if the file is big.

If you want to call a subroutine, from the command line, you'll have to code that explicitly with something like:

Of course using a module like GetOpt may simplify the analysis or the command line arguments, but you still have to explicitly implement the function call. If you have more than a couple of possibilities as above, you may want to implement a dispatch table (i.e. a hash containing code references to functions).