Re: perl loop not exiting
- From: Alex Hall <mehgcap@xxxxxxxxx>
- To: programmingblind@xxxxxxxxxxxxx
- Date: Tue, 27 Oct 2009 18:33:15 -0400
Sorry, there is a "Last;" statement right after "$found=1;" in the
program, not sure why it did not make it into the email. This is
supposed to be Perl's version of "break", but it is not breaking.
On 10/27/09, qubit <lauraeaves@xxxxxxxxx> wrote:
> I didn't see a break statement in the if state where you set $found to 1.
> Is there a break statement in perl?
> You are doing nothing to tell the interpreter to break from the loop.
> --le
>
> ----- Original Message -----
> From: "Alex Hall" <mehgcap@xxxxxxxxx>
> To: "programmingblind" <programmingblind@xxxxxxxxxxxxx>
> Sent: Tuesday, October 27, 2009 5:00 PM
> Subject: perl loop not exiting
>
>
> Hi all,
> I am still new to perl and have to use it for a class assignment. I
> have a for loop inside another for loop, and I want the outer one to
> exit when the regular expression built in the inner loop matches a
> string. It will not exit, though, and just keeps running until its
> termination condition is reached, ignoring my "Last" keyword
> completely. I have pasted the two loops below; again, this is perl
> 5.1.
>
> for $i(2...25){
> #build regexp by looping through @msg
> for $j(0...@msg-2){
> #each itteration, add the next char to the regexp
> $r.=$msg[$j].'.{'.$i.'}';
> #this searches for a char in the message, $i (period) number of
> characters, then the next char in the msg, and so on
> }
> #now that we have the regexp, match it to the string that is the file
> content to see if there is a match at this period
> if($txt=~m/$r/gi ){
> #found the regexp, so print success and quit the loop
> print("Match found. Period: $i.");
> $found=1;
> }
> if($i==24 and $found==0){ #message will not be found, so tell the user
> print("Your message was not found in the file.");
> }
> $r=""; #reset $r for the next period to be built
> }
>
> The problem is that, even if the string is found once, the main loop
> keeps going and finds the string at all other periods instead of
> quitting.
>
>
> --
> Have a great day,
> Alex
> My email is now: mehgcap@xxxxxxxxx
> __________
> View the list's information and change your settings at
> http://www.freelists.org/list/programmingblind
>
> __________
> View the list's information and change your settings at
> http://www.freelists.org/list/programmingblind
>
>
--
Have a great day,
Alex
My email is now: mehgcap@xxxxxxxxx
__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind
Other related posts: