PDA

View Full Version : Perl script help "next;"



elli
08 Dec 2006, 08:02 PM
Can someone Please help me with this stupid script! AAHHHH!
The script counts the words either typed by the user or counts the words in a file.
ok, I am trying to learn PERL and this is one of the scripts our instructor gave us to figure out. I am trying really hard but I am stuck in parts like the "next;"
I know next is suppose to just direct you to the next iteration but I that would be the for control structure and I can't make sence of it like that or any way for that matter but it works and there is nothing wrong with it. I just need more understanding of it.

The course I am taking is online and the help is limited, so I would really really appreciate it if someone could point me in the right direction with this script.

#!/usr/bin/perl

$wordcount = 0;


while (defined($line = <>))
{
chomp($line);
for ($position = 0;
$position < length($line) && $position != -1;
$position = $newposition)
{
if (substr($line, $position, 1) eq " ")
{
$newposition = $position + 1;
next;
}

$wordcount++;
$newposition = index($line, " ", $position);
}
}

print("$wordcount words counted in the file\n");



Thankyou elli

elli
09 Dec 2006, 04:53 AM
Can someone Please help me with this stupid script! AAHHHH!
The script counts the words either typed by the user or counts the words in a file.
ok, I am trying to learn PERL and this is one of the scripts our instructor gave us to figure out. I am trying really hard but I am stuck in parts like the "next;"
I know next is suppose to just direct you to the next iteration but I that would be the for control structure and I can't make sence of it like that or any way for that matter but it works and there is nothing wrong with it. I just need more understanding of it.

The course I am taking is online and the help is limited, so I would really really appreciate it if someone could point me in the right direction with this script.

#!/usr/bin/perl

$wordcount = 0;


while (defined($line = <>))
{
chomp($line);
for ($position = 0;
$position < length($line) && $position != -1;
$position = $newposition)
{
if (substr($line, $position, 1) eq " ")
{
$newposition = $position + 1;
next;
}

$wordcount++;
$newposition = index($line, " ", $position);
}
}

print("$wordcount words counted in the file\n");



Thankyou elli


I finally figured it out, so I don't need help anymore. Thanks elli