I was reading the node "Surpised by foreach iterator limitation", than I made some test codes to try to make the idea in the node works.

What the user want to do is use the $hash{key} as the string receiptor of the foreach, something like that:

foreach $hash{key} (@array){ ## The value of $hash{key} will be the $array[x] value. ## ... }
But this code can't be compiled since foreach only accept a scalar to receive the array string. Thinking in that I made a code that use a scalar variable that has a direct access to the value of the key in the hash:

my %hash = ( key => 'value'); my $k_ref = \$hash{key} ; *{'main::val'} = $k_ref ; ## To avoid $$k_ref print "val: <$val>\n" ; print "key: <$hash{key}>\n" ; $val = 'test' ; print "val: <$val>\n" ; print "key: <$hash{key}>\n" ;
Soo now I have a scalar variable that I can read and write to it and affect the value of the key in the hash (a scalar linked to the value of the key). Now I tried to use the same scalar in the foreach:
my %hash = ( key => 'value'); my $k_ref = \$hash{key} ; *{'main::val'} = $k_ref ; my @array = (0..5) ; print "val: <$val>\n" ; print "key: <$hash{key}>\n" ; foreach $val ( @array ) { print "for>> val: <$val>\n" ; print "for>> key: <$hash{key}>\n" ; } print "val: <$val>\n" ; print "key: <$hash{key}>\n" ;
But running the code you can see that it doesn't work like you want. The output inside the foreach will be:
for>> val: <0> for>> key: <value> for>> val: <1> for>> key: <value> for>> val: <2> for>> key: <value> ...
And not:
for>> val: <0> for>> key: <0> for>> val: <1> for>> key: <1> for>> val: <2> for>> key: <2> ...
Why this hapens?! Because when you put a $scalar in the foreach to reaceive the string, you are accessing the scalar inside the array directly, and not copying the content to the scalar! This is why when you change the value of the scalar the array is changed too:
my @array = (1..5) ; print "@array\n" ; foreach my $array_i ( @array ) { $array_i *= 10 ;} print "@array\n" ;
And this is a good thing, since is a speed improvement too. I know that the idea to affect the value of a key with the foreach is not a code that is useful, since you can make it explicit:
my %hash = ( key => 'value'); my @array = (0..5) ; foreach my $val ( @array ) { $hash{key} = $val ; print "for>> val: <$val>\n" ; print "for>> key: <$hash{key}>\n" ; }
But you can see that Perl is very powerful, specially with variables. only using other languages to see that... ;-P

Graciliano M. P.
"The creativity is the expression of the liberty".


In reply to Foreach & scalar ref: break of way that it is suposed to work! by gmpassos

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.