Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Loop consuming my list

Reply
Thread Tools

Loop consuming my list

 
 
Marc Girod
Guest
Posts: n/a
 
      10-09-2009
Hello,

I was surprised to see in my own code, a difference between
the two lines (out of context...):

$self->rmhlink($type, $_) for @pl;
and:
for my $p (@pl) { $self->rmhlink($type, $p) }

The former consumed the contents of @pl.
The latter did not.

The rmhlink (member) function reads its arguments from @_,
and returns a value, often undef.

I tried to reproduce the case naively, but failed:

$ perl -wle '@s=qw(a b c);sub f{return};f($_)for@s;map{print $_}@s'
a
b
c

Is there thus an interaction from the package?

I cannot get this either...

$ perl -wle 'package Foo;@s=qw(a b c);sub new{$t=shift;$s={};bless $s,
$t}sub foo{return};$f=new Foo;$f->foo($_)for@s;map{print $_}@s'
a
b
c

I am afraid this is a symptom that I missed something fundamental...
Who is eating my list?

Thanks,
Marc
 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      10-09-2009
Marc Girod <(E-Mail Removed)> writes:

> Hello,
>
> I was surprised to see in my own code, a difference between
> the two lines (out of context...):
>
> $self->rmhlink($type, $_) for @pl;
> and:
> for my $p (@pl) { $self->rmhlink($type, $p) }
>
> The former consumed the contents of @pl.
> The latter did not.


You probably have to defined what you mean by 'consumed the contents
of @pl'. I assume that you mean that the content of @pl has changed
somehow. The problme is that the first construction makes $_ an alias
to the elements in @pl and that $_ is a global variable.

In perl 5.10 you can make $_ a lexical variable by adding a 'my $_;'
somewhere in scope befor the loop:

my $_;
$self->rmhlink($type, $_) for @pl;

For older perls you have to localize $_ in your rmhlink method. I
would probably do both.

See also:
http://peter.makholm.net/2009/06/24/...of-a-global-_/

//Makholm
 
Reply With Quote
 
 
 
 
Marc Girod
Guest
Posts: n/a
 
      10-09-2009
Thank you Peter,

On Oct 9, 12:29*pm, Peter Makholm <(E-Mail Removed)> wrote:

> You probably have to defined what you mean by 'consumed the contents
> of @pl'.


Indeed. In this case, the list was empty after running the loop.

> The problem is that the first construction makes $_ an alias
> to the elements in @pl and that $_ is a global variable.


That's what I didn't understand.
I thought it was lexical...

> In perl 5.10 you can make $_ a lexical variable by adding a 'my $_;'
> somewhere in scope befor the loop:


Thanks. I can now see that it is what I did, without the
side-effect of the name.

> See also:http://peter.makholm.net/2009/06/24/...of-a-global-_/


Excellent.
I *did* miss something important.

Marc
 
Reply With Quote
 
Frank Seitz
Guest
Posts: n/a
 
      10-09-2009
Marc Girod wrote:
> On Oct 9, 12:29 pm, Peter Makholm <(E-Mail Removed)> wrote:
>
>> You probably have to defined what you mean by 'consumed the contents
>> of @pl'.

>
> Indeed. In this case, the list was empty after running the loop.


It is not possible to change array size by manipulating $_.
The array size must be the same.

Frank
--
Dipl.-Inform. Frank Seitz
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Homepage: http://www.fseitz.de/
XING-Profil: http://www.xing.com/profile/Frank_Seitz2
 
Reply With Quote
 
Marc Girod
Guest
Posts: n/a
 
      10-09-2009
On Oct 9, 1:50*pm, Frank Seitz <(E-Mail Removed)> wrote:

> It is not possible to change array size by manipulating $_.


Sorry, you are correct. The items were set to ''.
I tried the advice on 5.8.8, and checked that I cannot
make $_ lexical there, but that local $_ within rmhlink
works.

I believe that I'll be happy with the insight, and with
lexicalizing by naming as I did.

Thanks,
Marc
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Re: How to loop through a list while inside the loop, the list size may be decreased? Roedy Green Java 3 09-13-2008 01:51 AM
loop thru a STL list causes an infinite loop Allerdyce.John@gmail.com C++ 5 01-31-2006 03:21 PM
Loop the loop... =?Utf-8?B?VGltOjouLg==?= ASP .Net 2 02-16-2005 12:21 PM
Loop a list (contain multiple list) author ASP .Net 5 07-28-2004 01:48 PM



Advertisments