Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I have a question on a problem that occured in v5.12.4 using Ubuntu:
My code makes it neccessary to loop through an array and to output all elements using a reference. My first try did not work as expected (simplyfied example):
However, the following code does work (and is my solution):$sref = \$string; foreach $string ( @array_of_strings ) { print $$sref; }
Why? I would have expected that using the foreach $string acutally is assigned to whatever is the element of the array but aparently this is not the case. In the first example the print will only print what was assigned to $string before executing the foreach loop. Of course my actual code isn't that simple.$sref \$string1; foreach $string2 ( @array_of_strings ) { $string1 = $string2; print $$sref; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dereference string in foreach
by 7stud (Deacon) on Feb 13, 2013 at 23:23 UTC | |
|
Re: Dereference string in foreach
by choroba (Cardinal) on Feb 13, 2013 at 21:05 UTC | |
by 7stud (Deacon) on Feb 13, 2013 at 23:01 UTC | |
|
Re: Dereference string in foreach
by blue_cowdawg (Monsignor) on Feb 13, 2013 at 20:28 UTC | |
by Anonymous Monk on Feb 13, 2013 at 21:06 UTC | |
by Anonymous Monk on Feb 13, 2013 at 21:32 UTC | |
by blue_cowdawg (Monsignor) on Feb 14, 2013 at 15:30 UTC | |
by LanX (Saint) on Feb 14, 2013 at 22:12 UTC | |
by Anonymous Monk on Feb 14, 2013 at 19:29 UTC | |
by Anonymous Monk on Feb 14, 2013 at 21:34 UTC |