> > PRADEEP GOEL said:
> > > If i am not wrong somewhere , there is a bug come to my notice ,
> > > in PERL
> > >
> > > @lastnotpop =
> > > ("where_gone_me","remaining_is_alright","no_fear_now"); foreach
> > > $faltu (@lastnotpop)  #$faltu is extra doesn't makes difference
> > > even if removed { $pch = pop(@lastnotpop );
> > >  print " \n  can't pop last  one from @lastnotpop
> > >  i.e.($lastnotpop[0] )
> > > :
> > > popped element is  $pch #";
> > >  }
> > > If there are more than one elemnt in a array(say @lastnotpop  than
> > > the last/first element i.e. $lastnotpop[0]  can't be popped out in
> > > this single foreach loop.
> > >
> > > I don't think it's intensional feature , else it should also not
> > > be able to pop the only element , if it has one , but it does.

If would help a lot if you tried to find out what's going on:

        @a = (1,2,3);
        foreach my $loopvar (@a) {
                print "loop var: $loopvar\n";
                print "poped: ".pop(@a)."\n";
                print "\@a: (" . join(', ',@a) . ")\n\n";
        }

This prints:
        loop var: 1
        poped: 3
        @a: (1, 2)

        loop var: 2
        poped: 2
        @a: (1)

Which is exactly what I would expect and the only sensible thing to 
do. It iterates on the array and you pop the last elements at the 
same time. Therefore in the first iteration it sets the $loopvar to 
the first element of the array and removes the last element (3rd 
element), in the second iteration it sets $loopvar to the second 
element and again removes the last element (2nd elemenr). And of 
course there'll be no more iterations because the array has only one 
element now and the loop wants the third element.

The only thing you have to remember here is that the foreach(){} 
doesn't make a copy of the array it iterates over. Therefore if you 
change the array somehow the foreach(){} will operate on the modified 
array. See this:

        @a = (1,2,3,4,5);
        foreach my $loopvar (@a) {
                print "loop var: $loopvar\n";
                print "shifted: ".shift(@a)."\n\n";
        }

Look what it prints! Now that seems confusing at first. but only if 
you do not remember that the foreach(){} only remembers its POSITION 
in the array, not the actual element. Therefore if you shift() off 
the first element, you move the other elements!

The documentation says you should not do it. Not because it would be 
that hard to say what happens (once you understand what did I say 
above), but because it would be easy to create hard to find infinite 
loops:

        @a = (1,2,3,4);
        foreach my $loopvar (@a) {
                print "loop var: $loopvar\n";
                if ($loopvar == 2) {
                        unshift @a, 0;
                }
        }

or

        @a = (1,2,3,4);
        foreach my $loopvar (@a) {
                print "loop var: $loopvar\n";
                if ($loopvar == 4) {
                        push @a, 4;
                }
        }

(Well the loops would not be infinite, you'd run out of memory soon.)

HTH, Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to