in reply to Re: Code clarification - use of map and $$_
in thread Code clarification - use of map and $$_

Thanks for that. Why exactly would dereferencing be used here? Why not direct access to the variable?
  • Comment on Re^2: Code clarification - use of map and $$_

Replies are listed 'Best First'.
Re^3: Code clarification - use of map and $$_
by davido (Cardinal) on Aug 09, 2016 at 14:56 UTC

    Because the list returned by @{$r{$k}} is a list of array references. On each iteration of the map loop one element is passed in from the array, @{$r{$k}}, to $_. That element is a reference to an array. Thus, to act upon its contents, you dereference it.


    Dave

Re^3: Code clarification - use of map and $$_
by LanX (Saint) on Aug 09, 2016 at 14:55 UTC
    > Why exactly would dereferencing be used here? Why not direct access to the variable?

    without digging too deep into this code ...

    map can only iterate over scalars.

    I.e. like a list of $array_refs, if you want to address different arrays ...

    > Why not direct access to the variable?

    if you mean something like @array as the "direct" variable, you CAN'T do something like

    map { $_[0]++ } (@a,@b,@c)

    to increment the first element of each array.

    The real problem with that code is the laziness of the author to use a clear style.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re^3: Code clarification - use of map and $$_
by Corion (Patriarch) on Aug 09, 2016 at 14:06 UTC

    I don't know. Maybe ask the original author of the script.