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, 11:02 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 have to thank you for putting me on the right track!

There was a problem with some numeric data having prefixed zeroes while
some of the hash keys did not so there could be no match ( 0567 != 567).
But I'm still confused why the LAST was triggered before a match was
$found?

In any case the code is now much leaner :)

I heart Perl!

--

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 xhoster on March 18, 2008, 11:28 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 have to thank you for putting me on the right track!
>
> There was a problem with some numeric data having prefixed zeroes while
> some of the hash keys did not so there could be no match ( 0567 != 567).
> But I'm still confused why the LAST was triggered before a match was
> $found?

I rather doubt that it was doing that. If you want to pursue the matter,
post a complete, runnable script to illustrate this. Otherwise I'll assume
you are misinterpreting something.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Posted by Rodion on March 18, 2008, 11:40 am
Please log in for more thread options
Jednom davno, ne znam vise kad, xhoster@gmail.com duboko zamisljen/a
rece:

> > 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 have to thank you for putting me on the right track!
> >
> > There was a problem with some numeric data having prefixed zeroes while
> > some of the hash keys did not so there could be no match ( 0567 != 567).
> > But I'm still confused why the LAST was triggered before a match was
> > $found?
>
> I rather doubt that it was doing that. If you want to pursue the matter,
> post a complete, runnable script to illustrate this. Otherwise I'll assume
> you are misinterpreting something.

Well, I'm most certanly misundrestanding something, but I'd like to know
what, so I don't make the same mistake twice :/

The following subrutine works correctly (and slowly) if the lines with
LAST are commented out.

Code:
sub checkCB{
        my $cbName=$_[0];
        my $data=$_[1];
        my ($k,$v);
        my $found;
        my $g=0;
        my $checked;
        my $test='text';
        
        print "looking for [$data] in [$cbName] control: ";

#----numbers exception
        if ($data=~/^\d$/){
                $data=int($data);
                $test='numbers';
        }
        
CODECHECK:        while ( ($k,$v) = each %}) {
                        #print $g++."\n";
                        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);
                                                        }
                        }
                        }
                }
        
        return $found;
}
---------

--

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 xhoster on March 18, 2008, 12:16 pm
Please log in for more thread options
> Jednom davno, ne znam vise kad, xhoster@gmail.com duboko zamisljen/a
> rece:
>
> > > 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 have to thank you for putting me on the right track!
> > >
> > > There was a problem with some numeric data having prefixed zeroes
> > > while some of the hash keys did not so there could be no match ( 0567
> > > != 567). But I'm still confused why the LAST was triggered before a
> > > match was $found?
> >
> > I rather doubt that it was doing that. If you want to pursue the
> > matter, post a complete, runnable script to illustrate this. Otherwise
> > I'll assume you are misinterpreting something.
>
> Well, I'm most certanly misundrestanding something, but I'd like to know
> what, so I don't make the same mistake twice :/
>
> The following subrutine works correctly (and slowly) if the lines with
> LAST are commented out.

Global symbol "%cb" requires explicit package name...

And if I make up my own values for %cb, then I don't see what you
say you see.

Please post COMPLETE, RUNNABLE code which illustrates that which is to
be illustrated.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Posted by Peter J. Holzer on March 18, 2008, 7:34 pm
Please log in for more thread options
> Jednom davno, ne znam vise kad, xhoster@gmail.com duboko zamisljen/a
> rece:
>
>> > 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 have to thank you for putting me on the right track!
>> >
>> > There was a problem with some numeric data having prefixed zeroes while
>> > some of the hash keys did not so there could be no match ( 0567 != 567).

Try to canonicalize the data.

>> > But I'm still confused why the LAST was triggered before a match was
>> > $found?
>>
>> I rather doubt that it was doing that. If you want to pursue the matter,
>> post a complete, runnable script to illustrate this. Otherwise I'll assume
>> you are misinterpreting something.
>
> Well, I'm most certanly misundrestanding something, but I'd like to know
> what, so I don't make the same mistake twice :/
>
> The following subrutine works correctly (and slowly) if the lines with
> LAST are commented out.

How does it not work correctly if these lines are not commented out?

Please post an example which demonstrates the problem.

> Code:
> sub checkCB{
>         my $cbName=$_[0];
>         my $data=$_[1];
>         my ($k,$v);
>         my $found;
>         my $g=0;
>         my $checked;
>         my $test='text';
>         
>         print "looking for [$data] in [$cbName] control: ";
>
> #----numbers exception
>         if ($data=~/^\d$/){
>                 $data=int($data);
>                 $test='numbers';
>         }
>         
> CODECHECK:        while ( ($k,$v) = each %}) {
>                         #print $g++."\n";
>                         switch ($test){

Don't use switch. It is rather buggy and introduces problems which are
hard to find.

>                                 case 'numbers' {
>                                         if ($data==$k){        
>                                                 $found=1;
>                                                  #last CODECHECK if ($found==1);

$found is always 1 here (you just set it to one), so this is equivalent
to "last CODECHECK;". However, that seems to be what you want.

>                                         }
>                                 }
>                                 case 'text' {
>                                         if ($data eq $k){        
>                                                 $found=1;
>                                                  #last CODECHECK if ($found==1);

Same here.

>                                         }
>                                 }
>                         }
>                 }
>         
>         return $found;
> }
> ---------
>

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