Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^4: Dereferencing a Hash of Arrays

by toro (Beadle)
on Jun 14, 2011 at 07:48 UTC ( [id://909554]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Dereferencing a Hash of Arrays
in thread Dereferencing a Hash of Arrays

Thanks stefby, that works. But I don't understand why. Where does $_ derive from in this case, and why do I need ""?

Replies are listed 'Best First'.
Re^5: Dereferencing a Hash of Arrays
by stefbv (Curate) on Jun 14, 2011 at 08:21 UTC

    From the perlvar man page: $_ is "The default iterator variable in a foreach loop if no other variable is supplied."

    Using double quotes is equivalent with a join operation that uses a space as EXPR.

    join(' ', @$_)
      Using double quotes is equivalent with a join operation that uses a space as EXPR.

      Using double quotes is equivalent with a join operation that uses the variable $" as EXPR, where $" normally contains a single space character.

      Stefby, I am actually familiar with $_ in general, but in this specific case I don't understand where its value comes from. I'm guessing it's because $_ can handle lists and say for cannot? But, why?

      Here is my understanding of what's going on in your answer:

      1. values %alphabet spits out a list;
      2. $_ iterates through that list (but why wasn't my original say for iterating through the list?)
      3. @ dereferences the array (again, why didn't my original @{...} dereference it?)
      4. "   " joins the output with $".
      Would you mind correcting / clarifying the above? Thank you for your help so far.

        $_ iterates through that list (but why wasn't my original say for iterating through the list?)

        @ dereferences the array (again, why didn't my original @{...} dereference it?)

        It was iterating through the list and it did dereference , but the number 2 is not an array reference, so the list was empty :) strict noticed this problem, so it ended your program . This is why we love strict.

        Whatever text you're learning from, you need to go back and do the exercises you skipped :) the only way to absorb and remember is by typing it yourself :)

        $ perl -E " say for [1..4],[5..8] " ARRAY(0x3f8adc) ARRAY(0x9b9eac)
        aww, array references stringify like that, to get at the values, we need to dereference

        Trying it your way

        $ perl -E " say for @{ [1..4],[5..8] }" 5 6 7 8
        Aww, it only printed the contents of the last reference. To print both, we need to iterate over a list of reference, not a list of values
        $ perl -E " say @$_ for [1..4],[5..8] " 1234 5678
        Oh look, the numbers are all joined together, thats how say works
        $ perl -E " say 1,2,3,4; say 5..8; " 1234 5678
        I know, we'll put it in double quotes string, that will improve it
        $ perl -E " say qq{@$_} for [1..4],[5..8] " 1 2 3 4 5 6 7 8
        Now for the brackets
        $ perl -E " say qq{[@$_]} for [1..4],[5..8] " [1 2 3 4] [5 6 7 8]
        Now for the comma by changing the $", the LIST_SEPARATOR (the \" is for my shell, cmd.exe )
        $ perl -E " local $\"=q{,}; say qq{[@$_]} for [1..4],[5..8] " [1,2,3,4] [5,6,7,8]
        Even if we employ $" properly by limiting the scope of the changes with local, its better to use join function
        $ perl -E " say q{[}, join( q{,}, @$_ ), q{]} for [1..4],[5..8] " [1,2,3,4] [5,6,7,8]
        The values function is context sensitive
        $ perl -E " %f = 6..9; say for values %f; " 9 7 $ perl -E " %f = 6..9; say for scalar values %f; " 2
        Using @{} puts values %f in scalar context, just like the scalar function. This makes the values function return the number of values in %f; The number of values is not an array reference.

        To learn more about context see Tutorials: Context in Perl: Context tutorial, or see the free book Modern Perl, a loose description of how experienced and effective Perl 5 programmers work....You can learn this too.

Re^5: Dereferencing a Hash of Arrays
by Anonymous Monk on Jun 14, 2011 at 09:23 UTC
    Most every perl comes with perldoc
    $ perldoc -v $_ $ARG $_ The default input and pattern-searching space. The followi +ng ... ... ... ... ... ... ... (Mnemonic: underline is understood in certain operations.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://909554]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-25 21:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found