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:
exit condition while

 



ticketE
Novice

Mar 6, 2013, 8:51 AM

Post #1 of 3 (445 views)
exit condition while Can't Post

Hi,
i'm trying to do my first program in Perl, but i've a problem

The exercise requires that I have to insert a flow of number, recognized by the "start characters" '<>' and terminated by '=' and calculate the sum of this numbers inserted.
This is my code. It enter in the second while but don't exit.

Code
#!/usr/bin/perl 

while ($var=<STDIN>){
if ($var =~ /<>/){
while($number=<STDIN>){
if ($number=~/=$/){
break;
}else{
$sum+=$number;
}
}
}
if ($number=~/=$/){
break;
}
}

print "sum: $sum";
;


How could I do?


BillKSmith
Veteran

Mar 6, 2013, 10:01 AM

Post #2 of 3 (440 views)
Re: [ticketE] exit condition while [In reply to] Can't Post

The word break is a keyword in C, but not in perl. There is no exact equivalent, but in this case, you can use last.

Although your code will work with just this change, it does have several issues. You should always use use strict; and use warnings;. Strict will require you declare your variables (use my)

Your second test for the equal character is not needed. When it is true, you will never get there.

You should prompt for input. Your user has no indication that nothing is happening because you are waiting for input from him. There are good reasons to use a prompt module from CPAN rather than printing your own prompts.

Read about the range operator in perdoc perlop. You should be able to simplify your logic.
Good Luck,
Bill


Rahul6990
Novice

Mar 6, 2013, 11:03 PM

Post #3 of 3 (427 views)
Re: [ticketE] exit condition while [In reply to] Can't Post

Hi,

with respect to C break and continue in Perl we have:


break : last
continue : next

 
 


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

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