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' ];

In reply to How to use 'last' / 'next' commands to exit loop? by sara2005

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.