Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How to use 'last' / 'next' commands to exit loop?

by sara2005 (Scribe)
on May 30, 2006 at 17:20 UTC ( [id://552558] : perlquestion . print w/replies, xml ) Need Help??

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

Dear Monks,

I define a hash table and an array upfront. Then, I want to read each element of the array, find if a 'key' exists in the hash table. If found, push the corresponding 'value' into a new array, if not, push the original element (read from the array) into the new array.

The program does what I expect it to do but I want to break the loop and contine with the next element in the array as soon as a match is found in

if ($_ eq $key){ ...

but, I am not too sure as to where to use the last or next command to break the loop as I didn't get the right results when I introduced last/next. Please help..

Here is the code:-

use Data::Dumper; use strict; # Define actual names (values) for the references (keys) in a hash tab +le my %mapped_vars = ( '1a' => 'One' '2a' => 'Tw +o', '3a' => 'Three', '4a' => 'Four' ); # List of array variables that has to be evaluated. It can also have n +ames that are NOT present in the hash table my @arr_vars = ( '3a','9d', '4a', '7b'); my (@act_vars, $i); my $found=0; # Checking... print Dumper(\%mapped_vars); print Dumper(\@arr_vars); foreach (@arr_vars){ while( (my $key, my $value) = each %mapped_vars){ print "$_-inside while()"; if ($_ eq $key){ push @act_vars, $value; $found =1; #last; } #last LBL; } unless ($found){ print "Inside unless()\n"; push @act_vars, $_; } $found = 0; print "\n"; } print Dumper(\@act_vars);

The Output is as follws:-

$VAR1 = { '3a' => 'Three', '2a' => 'Two', '4a' => 'Four', '1a' => 'One' }; $VAR1 = [ '3a', '9d', '4a', '7b' ]; --------------------------------------- 3a-inside while()3a-inside while()3a-inside while()3a-inside while() 9d-inside while()9d-inside while()9d-inside while()9d-inside while()In +side unless() 4a-inside while()4a-inside while()4a-inside while()4a-inside while() 7b-inside while()7b-inside while()7b-inside while()7b-inside while()In +side unless() ------------------------ $VAR1 = [ 'Three', '9d', 'Four', '7b' ];

Replies are listed 'Best First'.
Re: How to use 'last' / 'next' commands to exit loop?
by blazar (Canon) on May 30, 2006 at 17:29 UTC
    I define a hash table and an array upfront. Then, I want to read each element of the array, find if a 'key' exists in the hash table. If found, push the corresponding 'value' into a new array, if not, push the original element (read from the array) into the new array.
    my @new=map { exists $hash{$_} ? $hash{$_} : $_ } @array;

    If the values of %hash are guaranteed not to be false, then also

    my @new=map $hash{$_} || $_, @array;

    (I prefer the comma form with short expressions and the block form for longer ones, not to mention those cases in which you have more statements and you are "forced" to use a block, or else a do, but... why?)

      worked perfectly!!

      Thanks a lot.

Re: How to use 'last' / 'next' commands to exit loop?
by borisz (Canon) on May 30, 2006 at 17:27 UTC
    last exit the current loop ( in your case the while loop ). if you like to exit all loops and continue at with the next entry on the for loop lable that.
    ... loop: foreach ( @arr_vars ){ while( (my $key, my $value) = each %mapped_vars){ ... if ( $_ eq $key ) { ... next loop; } }
    Boris
Re: How to use 'last' / 'next' commands to exit loop?
by Zaxo (Archbishop) on May 30, 2006 at 17:30 UTC

    I'm not clear about what you want next or last to do there, but I think you can simplify your key search like this:

    my @act_vars = grep {exists $mapped_vars{$_}} @arr_vars;
    Looks like blazar++ read the question better.

    After Compline,
    Zaxo