Click here to get back home

Problem with loop control LAST exiting prematurely

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Problem with loop control LAST exiting prematurely Rodion 03-18-2008
Posted by Rodion on March 18, 2008, 6:22 am
Please log in for more thread options
Hi!
I got into a problem with looking up stuff in hasheshes, the sub is
supposed to check if $data is in the hash, and when found exit the loop,
as there is no need to go through the rest of the hash.
Only for some reason it exits immediately :?

the code:
--------
CODECHECK:        while ( ($k,$v) = each %}) {
                        switch ($test){
                                case 'numbers' {
                                                if ($data==$k){        
                                                        $found=1;
                                                 last CODECHECK if ($found==1);
                                        }
                                }
                                case 'text' {
                                                        if ($data eq $k){        
                                                                $found=1;
                                                  last CODECHECK if ($found==1);
                                                        }
                                }
                        }
                }
-----------
The problem is that last exits the loop before $found is set which
shouldnt happen.

TIA!


--

Professor: Now, be careful, Fry. And if you kill anyone, make sure
to eat their heart to gain their courage. Their rich tasty courage.



Posted by Willem on March 18, 2008, 6:28 am
Please log in for more thread options
Rodion wrote:
) Hi!
) I got into a problem with looking up stuff in hasheshes, the sub is
) supposed to check if $data is in the hash, and when found exit the loop,
) as there is no need to go through the rest of the hash.
) Only for some reason it exits immediately :?
)
) the code:
) --------
) CODECHECK:        while ( ($k,$v) = each %}) {
)                         switch ($test){
)                                 case 'numbers' {
)                                                 if ($data==$k){        
)                                                         $found=1;
)                                                  last CODECHECK if ($found==1);
)                                         }
)                                 }
)                                 case 'text' {
)                                                         if ($data eq $k){        
)                                                                 $found=1;
)                                                   last CODECHECK if ($found==1);
)                                                         }
)                                 }
)                         }
)                 }
) -----------
) The problem is that last exits the loop before $found is set which
) shouldnt happen.

How do you know that it's the 'last' that is exiting the loop ?


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Posted by Rodion on March 18, 2008, 6:38 am
Please log in for more thread options
Jednom davno, ne znam vise kad, Willem duboko zamisljen/a rece:

> Rodion wrote:
> ) The problem is that last exits the loop before $found is set which
> ) shouldnt happen.
>
> How do you know that it's the 'last' that is exiting the loop ?

Easily, if I comment it out, it works correctly, albait slowly as it has
to go through the whole hash which is unnaceptable :(


--

Professor: Now, be careful, Fry. And if you kill anyone, make sure
to eat their heart to gain their courage. Their rich tasty courage.



Posted by John W. Krahn on March 18, 2008, 7:33 am
Please log in for more thread options
Rodion wrote:
> Hi!
> I got into a problem with looking up stuff in hasheshes, the sub is
> supposed to check if $data is in the hash, and when found exit the loop,
> as there is no need to go through the rest of the hash.
> Only for some reason it exits immediately :?
>
> the code:
> --------
> CODECHECK:        while ( ($k,$v) = each %}) {
>                         switch ($test){
>                                 case 'numbers' {
>                                                 if ($data==$k){        
>                                                         $found=1;
>                                                  last CODECHECK if ($found==1);
>                                         }
>                                 }
>                                 case 'text' {
>                                                         if ($data eq $k){        
>                                                                 $found=1;
>                                                   last CODECHECK if ($found==1);
>                                                         }
>                                 }
>                         }
>                 }
> -----------
> The problem is that last exits the loop before $found is set which
> shouldnt happen.

Couldn't you just write that as:

$found = 1 if exists $cb;


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Posted by Rodion on March 18, 2008, 9:17 am
Please log in for more thread options
Jednom davno, ne znam vise kad, John W. Krahn duboko zamisljen/a rece:


> Couldn't you just write that as:
>
> $found = 1 if exists $cb;
>

Well I get the same problem, some cases arent found in the hash key...
I think it's a data type problem...confusing...

--

Professor: Now, be careful, Fry. And if you kill anyone, make sure
to eat their heart to gain their courage. Their rich tasty courage.





Similar ThreadsPosted
Re: stuck in a control loop October 24, 2007, 3:05 am
What does "exit the block or routine with a loop control operator" mean? July 24, 2004, 4:17 am
prematurely closed filehandle June 29, 2006, 11:02 am
Problem expanding filenames in loop June 23, 2008, 2:43 pm
Exiting without printing December 22, 2005, 8:48 pm
executing code without exiting the interpreter April 1, 2007, 4:35 pm
How can I prevent perl from exiting during a network error? May 8, 2005, 9:32 am
How to stop perl from exiting when stat($_)->size fails February 15, 2006, 11:50 am
process control September 28, 2004, 12:12 am
How to control version of OS September 27, 2005, 1:52 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap