P is for Practical | |
PerlMonks |
You can't assign $_ to a hash in a foreach statementby markdibley (Sexton) |
on Apr 08, 2022 at 16:33 UTC ( [id://11142845]=perlquestion: print w/replies, xml ) | Need Help?? |
markdibley has asked for the wisdom of the Perl Monks concerning the following question: I have tried everything I can think of (plus a tonne of random experiments) to assign $_ within the foreach statement to a hash. I know that you can assign it after the foreach line, but I have a very particular code limitation which doesn't allow me to do this.
What I think is happening is the perl compiler cannot handle '{' and '}' characters in the position where it is looking for $VAR to assign $_ to. When I run the code not only do I get an error about the foreach line, but I also get a syntax error about the print line where there is no syntax error.
Is there any way to assign $_ within the foreach statement directly to a hash reference rather than assigning it with $var->{$y} = $_; on the following line? I am looking for the following output
Also, I am aware that the following works, but again, I have a particular code limitation which means I have to be able to handle foreach statements
Back to
Seekers of Perl Wisdom
|
|