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

Hi all, I didn't find any info about a while loop with a "statement that will be executed at the end" (no more true). For exemple:

while ( my $key=keys(%hash) { last if $key eq "$verb"; } $hash{$key}=$verb;

Thanks in advance Adrian

Replies are listed 'Best First'.
Re: while loop question
by Athanasius (Archbishop) on Aug 21, 2014 at 09:42 UTC

    Hello adriang,

    I realise your code snippet is just for illustration; still, it may be instructive to note its various problems:

    1. while ( my $key=keys(%hash) {

      Compile error: the second closing parenthesis is missing.

    2. my $key=keys(%hash)

      Logic error: in scalar context the keys function returns the number of keys in the hash, not the keys themselves. You need a for loop here:

      for my $key (keys %hash) {
    3. keys %hash

      Logic error: since you want to exit the loop when a certain key is reached, the behaviour of the code depends on the order in which the keys are returned; but this order is “apparently random” (keys). To get deterministic behaviour, you need to sort the keys. For example:

      for my $key (sort keys %hash) {
    4. }  $hash{$key}=$verb;

      Compile error: if you’re running under use strict; (and you should be!), you will see this error message:

      Global symbol "$key" requires explicit package name at ...

      You could fix this1 by declaring my $key; before the loop, but it’s better style to restrict the scope of this lexical variable:

      for my $key (sort keys %hash) { if ($key eq $verb) { $hash{$key} = $verb; last; } }

    Update (Aug 23):
    1That is, you could fix the error message. But the code still wouldn’t work correctly, because $key is a temporary alias within the foreach loop (see “alias” in perlglossary), and it reverts to its pre-loop value when the loop ends.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thanks for the quick answer. nr.1 - copy-paste error. nr.2 - 100% RIGHT. nr.3 - understand. nr.4 - fast writing error :-) Sorry, code nr.4 is not good for "since you want to exit the loop when a certain key is reached" because I want to create a hash element if the has key is not there. For now I use the "defined" function (below) to do the work but I realy want to know if there is other solution.

      unless ( defined $email{"email"} ) { $email{$html->param("email")}=$html->param("name"); }
        exists should help you with this problem. As you may have noticed, looping over keys of a hash is much slower than accessing it by key. By the way, very often it is plain wrong.
Re: while loop question
by Anonymous Monk on Aug 21, 2014 at 08:05 UTC
    Funny :)
    print "before\n"; while( 1 ){ print for 1 .. 10; last } print "after\n";
    You can write it as  print "before\n"; while( 1 ){ print for 1 .. 10; last } print "after\n"; its still the same code
Re: while loop question
by choroba (Cardinal) on Aug 21, 2014 at 08:04 UTC
Re: while loop question
by GotToBTru (Prior) on Aug 21, 2014 at 14:26 UTC

    Are you perhaps thinking of the continue statement? The $hash{$key}=$verb; line is unrelated to the while loop.

    1 Peter 4:10
      continue with wile is not good because "it is always executed just before the conditional is about to be evaluated again" I need a something that will be executed only once before the while loope exit and ofcourse if I use "last" in the loop that statment will never be executed. adrian