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:
A question about a sample script.

 



King Norre
Novice

Apr 15, 2003, 6:07 AM

Post #1 of 7 (1084 views)
A question about a sample script. Can't Post

Hi,

I'm learning Perl using the book "Teach yourself Perl5 in 21 days" from David Till.
Somewhere in the book there's the following sample script:

#!/usr/bin/perl
# a word-counting program that uses the NEXT statement

$total = 0;
while ($line = <STDIN>) {
$line =~ s/^[\t ]*//;
$line =~ s/[\t ]*\n$//;
next if ($line eq "");
@words = split(/[\t ]+/, $line);
$total += @words;
}
print ("The total number of words is $total\n");

---------- END

Normally the input should end when I press CTRL + D.
However, when I run the script I can only put in 1 line.
When I press ENTER I get the following error message:

Split loop at ./ex8.11 line 9, <STDIN> line 1.

I have absolutely NO idea what could be wrong.
Can anyone help me?
Thanks in advance,

KN


davorg
Thaumaturge / Moderator

Apr 15, 2003, 6:57 AM

Post #2 of 7 (1082 views)
Re: [King Norre] A question about a sample script. [In reply to] Can't Post

I just cut and pasted that code and it worked straight off as expected.

The book you mention is rather old and has a reputation in the Perl community of not being very good. The code you quote contains a number of bad practices. I'd recommend getting rid of it and replacing it with a better book.

Try replacing your code with something like this:


Code
#!/usr/bin/perl 
# a word-counting program that uses the NEXT statement

use strict;
use warnings;

my $total = 0;
while (<STDIN>) {
s/^\s+//;
s/\s+$//;
next unless $_;
my @words = split(/\s+/, $_);
$total += @words;
}
print ("The total number of words is $total\n");


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


King Norre
Novice

Apr 15, 2003, 7:20 AM

Post #3 of 7 (1081 views)
Re: [davorg] A question about a sample script. [In reply to] Can't Post

Hey thanks for the advice Dave. The code you gave me works.
I've been looking for some books about Perl.
Is "Learning Perl, 3rd edition" from O'Reilly a good/better book to start with?

KN


davorg
Thaumaturge / Moderator

Apr 15, 2003, 7:29 AM

Post #4 of 7 (1079 views)
Re: [King Norre] A question about a sample script. [In reply to] Can't Post

Every Perl installation comes complete with the Perl FAQ which has answers to all sorts of interesting questions about Perl. You should really read it. It includes a list of recommended Perl books.

perldoc perlfaq2

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Paul
Enthusiast

Apr 15, 2003, 7:37 AM

Post #5 of 7 (1077 views)
Re: [davorg] A question about a sample script. [In reply to] Can't Post


Quote
s/^\s+//;
s/\s+$//;
next unless $_;
my @words = split(/\s+/, $_);


or:


Code
s/^\s+|\s+$//g;    
next unless $_;
my @words = split /\s+/;



(This post was edited by Paul on Apr 15, 2003, 7:38 AM)


davorg
Thaumaturge / Moderator

Apr 15, 2003, 7:44 AM

Post #6 of 7 (1072 views)
Re: [Paul] A question about a sample script. [In reply to] Can't Post


Quote

Code
my @words = split /\s+/;



or even just


Code
my @words = split;


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Paul
Enthusiast

Apr 15, 2003, 8:30 AM

Post #7 of 7 (1070 views)
Re: [davorg] A question about a sample script. [In reply to] Can't Post

Exactly, wasn't so hard was it =)

 
 


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

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