Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Localizing hash keys in an AoH

by BrowserUk (Pope)
on Mar 15, 2013 at 19:48 UTC ( #1023766=note: print w/ replies, xml ) Need Help??


in reply to Localizing hash keys in an AoH

Can you not just localise the entire AoH?:

{ local @data; $data[ $_ ]{unit} = 'something' for 0 .. $#data; ... } # all changes undone here

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re: Localizing hash keys in an AoH
Download Code
Re^2: Localizing hash keys in an AoH
by NetWallah (Abbot) on Mar 15, 2013 at 20:48 UTC
    That would probably require:
    local @data = @data;
    else local @data would initialize empty. (At least it does, in my tests).

    Suffers from "Can't localize lexical variable @data at ..."

    Update: this works:

    LOCAL_BLOCK_5: { local @data[0..$#data]; $_->{unit} = 'S' for @data; print Dumper \@data; }
    Update 2: Just noticed kennethks solution below, that changes @data to a package (our) variable, and allows it to be localized.

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

      Localizing the array slice seems to work, which is weird because localizing the array did not -- see Re^2: Localizing hash keys in an AoH. Huh. Regardless, thank you! Just what the doctor ordered.


      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        Looks like you can do all that in a single line as well:
        perl -E 'use strict;my @x=map {Z=>$_+999}, 0..5;{$_->{Z}=33 for local + @x[0..$#x]; say $x[2]{Z}}; say $x[2]{Z}' 33 1001

                     "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
                -- Dr. Cox, Scrubs

Re^2: Localizing hash keys in an AoH
by kennethk (Monsignor) on Mar 15, 2013 at 20:58 UTC
    When you localize the array, it's only the array elements that get localized; you still have the original hash refs.
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; our @data = ( {unit => 'S', value => 50, }, {unit => 'T', value => 60, }, {unit => 'Q', value => 70, }, ); LOCAL_BLOCK: { local(@data) = @data; $_->{unit} = 'S' for @data; } print Dumper \@data;
    outputs
    $VAR1 = [ { 'unit' => 'S', 'value' => 50 }, { 'unit' => 'S', 'value' => 60 }, { 'unit' => 'S', 'value' => 70 } ];

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-07-29 06:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (211 votes), past polls