Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

While on hashes doesn't work?

by blahblah (Friar)
on Feb 04, 2002 at 22:04 UTC ( [id://143303]=perlquestion: print w/replies, xml ) Need Help??

blahblah has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/sbin/perl -w use strict; use diagnostics; # QUESTION: # Why does the for loop execute and the while loop does not? # Obviously the while conditional is not evaluating true, but why? The +re are still elements in the list...? # Also, in the case of longer hashes, wouldn't the while be more effic +ient? my %data = ( a01 => "two", a02 => "peas", a03 => "in", a04 => "a", a05 => "pod." ); for (sort (keys %data)) { print "for: $data{$_}\n"; } while (sort (keys %data)) { print "while: $data{$_}\n"; }
Thanks,
Alex

Edit by tye to replace PRE tags around long lines with CODE tags

Replies are listed 'Best First'.
Re: While on hashes doesn't work?
by gellyfish (Monsignor) on Feb 04, 2002 at 22:27 UTC

    Simply put because while doesn't work like that :)

    Explicitly, while doesn't iterate over the items of a list in the way that for does and it doesn't set $_ unless the expression is the diamond operator.

    To get your example to work with while you will need to do something like :

    my @foo = sort (keys %data); while( $_ = shift @foo ) { print "while: $data{$_}\n"; }

    /J\

Re: While on hashes doesn't work?
by mkmcconn (Chaplain) on Feb 04, 2002 at 23:26 UTC

    Not recommending it, but perhaps it gives insight into how while works (in addition to what gellyfish said above)

    #!/usr/bin/perl -w use strict; my %data = ( a01 => "two", a02 => "peas", a03 => "in", a04 => "a", a05 => "pod." ); while (($_) = sort keys %data){ print "while: $data{$_}\n"; delete $data{$_}; } print "while loop terminates because", "%data is now empty >", %data,"< \n"; __END__

    Without those parens around $_, I would expect it to try to pass '5' into the block (which it would do if you took away the sort function), but it doesn't do that. I suppose that might qualify as an insignificant bug.
    update: thanks to merlyn for fixing the bug in my understanding of how sort behaves.
    mkmcconn </code>
      while (($_) = sort keys %data){ print "while: $data{$_}\n"; delete $data{$_}; }
      This is a pretty expensive loop. You'd be much better doing:
      while (my ($k, $v) = each %data) { print "key is $k, value is $v\n"; }
      Without those parens around $_, I would expect it to try to pass '5' into the block (which it would do) if you took away the sort function, but it doesn't do that. I suppose that might qualify as an insignificant bug.
      No, sort in a scalar context always returns undef. Maybe someday, it'll return something useful. But not yet. No bug, except in perhaps your understanding. {grin}

      -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://143303]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-26 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found