Note that there are some explanatory texts on larger screens.

plurals
  1. POEquivalent of "shift" for a hash to create a $class->next() method
    primarykey
    data
    text
    <p>I almost feel like saying "it's me again!".</p> <p>Anyway, here we go.</p> <p>I like using <code>while $object-&gt;next()</code> style constructs. They appeal to me and seem "neat".</p> <p>Now, when the thing I'm iterating over is an array, it's straightforward ("<code>shift @ary or return undef</code>")</p> <pre><code>sub next { my ( $self, $args ) = @_; my $next = shift @{ $self-&gt;{list_of_things} } or return undef; my ( $car, $engine_size, $color ) = split( /\Q$opts-&gt;{fieldsep}/, $next ); $self-&gt;car = $host; $self-&gt;engine_size = $engine_size; $self-&gt;color = $color; </code></pre> <p>}</p> <p>In this example I use AUTOLOAD to create the getters and setters and then have those instance variables available in my object during the <code>while</code> loop.</p> <p>I'd like to do something similar but with the "list_of_things" being a <code>%hash</code>.</p> <p>Here's a non-OO example that doesn't make it into the first iteration. Any ideas why?</p> <p>(The total "list_of_things" is not that big - maybe 100 entries - so to do a <code>keys(%{$hash})</code> every time doesn't seem too wasteful to me).</p> <pre><code>use strict; use warnings; use Data::Dumper; my $list_of_things = { volvo =&gt; { color =&gt; "red", engine_size =&gt; 2000, }, bmw =&gt; { color =&gt; "black", engine_size =&gt; 2500, }, mini =&gt; { color =&gt; "british racing green", engine_size =&gt; 1200, } }; sub next { my $args = $_; my @list = keys( %{$list_of_things} ); return undef if scalar @list == "0"; my $next = $list_of_things-&gt;{ $list[0] }; delete $list_of_things-&gt;{ $list[0] }; return $next; } while ( next()) { print Dumper $_; print scalar keys %{ $list_of_things } } </code></pre> <p>Is there a better way of doing this? Am I doing something crazy?</p> <h1>EDIT:</h1> <p>I tried Ikegami's suggestion. Of course, Ikegami's example works flawlessly. When I try and abstract a little, so that all that is exposed to the object is a next->() method, I get the same "perl-going-to-100%-cpu" problem as in my original example.</p> <p>Here's a non-OO example:</p> <pre><code>use Data::Dumper qw( Dumper ); sub make_list_iter { my @list = @_; return sub { @list ? shift(@list) : () }; } sub next { make_list_iter( keys %$hash ); } my $hash = { ... }; while ( my ($k) = next-&gt;() ) { print Dumper $hash-&gt;{$k}; } </code></pre> <p>It does not seem to get past the first step of the while() loop.</p> <p>I am obviously missing something here...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload