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:
skip new lines and some other beginner questions

 



b3asttt
New User

Feb 9, 2013, 8:17 PM

Post #1 of 4 (579 views)
skip new lines and some other beginner questions Can't Post

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.

Code
my @array = <FILE>; 
foreach my $line (@array){
chomp($line);
my @temp = split(',', $line);
print "@temp[1]\n";
}


how do i make sure $test[1] is a number? also, how would i ignore the blank lines in the txt file? Thanks a lot guys.


BillKSmith
Veteran

Feb 9, 2013, 9:00 PM

Post #2 of 4 (576 views)
Re: [b3asttt] skip new lines and some other beginner questions [In reply to] Can't Post

It would be better to read and process your file one line at a time rather than slurping the whole file into an array.

You can skip blank lines with a single statement:

Code
next if $line eq '' # that is two consecutive single quotes


In simple cases, you can validate a number with a regular expression, however the best way is to use a module. I use the function is_numeric in the module Data::Validate.

Your average needs a sum and a count. You can compute both of them in your loop.
Good Luck,
Bill


b3asttt
New User

Feb 9, 2013, 11:31 PM

Post #3 of 4 (569 views)
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


Laurent_R
Enthusiast / Moderator

Feb 10, 2013, 6:35 AM

Post #4 of 4 (562 views)
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:


Code
my $arg1 = shift; 
chomp $arg1;
foobar() if ($arg1 eq "foo");
barfoo() if $arg1 eq "bar";


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).

 
 


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

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