Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

variable like $_

by perlthirst (Scribe)
on Feb 04, 2009 at 06:00 UTC ( #741185=perlquestion: print w/ replies, xml ) Need Help??
perlthirst has asked for the wisdom of the Perl Monks concerning the following question:

hi,

In the following code,
use strict; use warnings; my %var = ( 'a' => { 'one' => '1', 'two' => '2', 'three' => '3', }, 'b' => { 'four' => '4', 'five' => '5', } ); foreach (keys %var ) { ## Here $_ will have a , b foreach (keys %{$var{$_}} ) { ## Here $_ will have one, two , + three, etc. print $_; ## With in this for loop, I wa +nt to access a and b. } }

How can i access $_ available in the first for loop, with in the inner for loop, with out using temporary variable to store $_ in the first for loop. How do i achieve this?

Comment on variable like $_
Download Code
Re: variable like $_
by targetsmart (Curate) on Feb 04, 2009 at 06:07 UTC
    Usage of a temporary variable gives more clarity to your code and solves your problem, this $_ is a global variable it gets changed depending on the loop/block/function that you are currently in. I think that using the outer loop's $_ in the inner loop without a temporary variable isn't possible and it is not good; it would lead to confusion for the reader/maintainer or even you when you look back.

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: variable like $_
by fullermd (Curate) on Feb 04, 2009 at 06:10 UTC

    There are potentially ways by doing really deep trickery, but why would you bother? Just use assigned variables for each loop.

    foreach my $k1 (keys %var) { foreach my $k2 (keys %{$var{$k1}}) { print('$var{$k1}{$k2}=', $var{$k1}{$k2}, "\n"); } }
      I like this post and looks good to me! There is no penalty for having a lexical "my" variable as an iterator in a "foreach" loop. Think about it, here $k1 and $k2 are separate things. Even if you succeed in writing this with just $_, you haven't saved anything, Perl will essentially create these vars even if you haven't given them names - they are separate things to Perl (inner $_ is separate from outer $_). Give them names! I agree!
Re: variable like $_
by CountZero (Bishop) on Feb 04, 2009 at 06:11 UTC
    You cannot (at least not in Perl <= 5.8.8; I do not know about 5.10 or Perl 6). There is only one $_ and the second use of it destroys the contents of the first use.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      There is only one $_
      True.
      and the second use of it destroys the contents of the first use.
      False.
      foreach ("a", "b") { # First use of $_ foreach ("c", "d") { # Second use of $_ say; } say; # 'Old' value of $_ } __END__ c d a c d b
      The second use of '$_' doesn't destroy the content of the first use. It stores it somewhere else, and restores the value after the second use is finished. That's the principle of local. And that's what's happening with a foreach loop.
        "Destroy was indeed not the right word, "hide" or "shadow" would have been better.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: variable like $_
by tilly (Archbishop) on Feb 04, 2009 at 06:13 UTC
    Why the aversion for the temporary variable?
    for my $foo (keys %var) { for my $bar (keys %{$var{$_}} { # You can access both now. } }
    I do not believe that this is any less efficient than using $_, and it has great advantages in flexibility and clarity.
Re: variable like $_
by ysth (Canon) on Feb 04, 2009 at 06:22 UTC
    Don't do that. But, like this:
    foreach (keys %var) { ## Here $_ will have a , b foreach ( map [scalar each %{$var{$_}}, $_], ($_) x keys %{$var{$_ +}} ) { print "inner '\$_': $_->[0]\n"; print "outer '\$_': $_->[1]\n"; } keys %{$var{$_}}; # iterator reset }
      I believe that you will find that this code is inferior by FAR performance-wise to the above solutions.
        And far inferior maintenance-wise. But ysth did say not to do it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://741185]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-12-21 02:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (101 votes), past polls