Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Hash value modification

by Tanalis (Curate)
on Jul 25, 2005 at 07:54 UTC ( #477712=perlquestion: print w/replies, xml ) Need Help??

Tanalis has asked for the wisdom of the Perl Monks concerning the following question:

Update: Some more digging and I've answered my own question: Old Perl Behavior(TM). This behaviour was changed in Perl 5.6.1 (changelog) to make access to hash elements faster. More ammunition for the Update the Perl Version Campaign ... :)
I've been bitten this morning by some interesting behaviour when performing regular expression substitution on the values of a hash.

I'm used to being able to do something like

foreach( @array ) { s/something/something_else/g }
to an array, and expected a similar thing to work for hashes:
foreach( values %hash ) { s/something/something_else/g }
but it appears that this isn't the case. The substitution is performed, but on a copy of the original value, not a reference to it. Of course,
foreach( keys %hash ) { $hash{$_} =~ s/something/something_else/g; }
works as expected.

This interests me, and appears a little inconsistent. Is there some reason that my coffee-deprived brain can't see this morning for this inconsistency to exist?

Replies are listed 'Best First'.
Re: Hash value modification
by sk (Curate) on Jul 25, 2005 at 08:08 UTC
     perldoc -f values

    Note that the values are not copied, which means modifying them will modify the contents of the hash:

    for (values %hash) { s/foo/bar/g }   # modifies %hash values

    Can you give an example on what you are seeing?


    Update: Are you not seeing this behavior?

    #!/usr/bin/perl -w my @array = qw(1 hello 2 hello 3 hellome 4 hellothere); my %hash = @array; foreach( values %hash ) { s/hello/hi/g; } print +("$_ = $hash{$_}\n") for (sort keys %hash);


    1 = hi 2 = hi 3 = hime 4 = hithere
Re: Hash value modification
by tlm (Prior) on Jul 25, 2005 at 12:34 UTC

    FWIW, slices also work:

    for ( @hash{ keys %hash } ) { s/something/something_else/g; }

    the lowliest monk

Re: Hash value modification
by anonymized user 468275 (Curate) on Jul 25, 2005 at 08:57 UTC
    Here's a test, which shows the same behaviour happening as for arrays:
    #!/usr/bin/perl use Data::Dumper; my %h = ( aa => hello, bb => ghello ); foreach ( values %h ) { s/el/ig/g }; print Dumper( \%h );


    $VAR1 = { 'aa' => 'higlo', 'bb' => 'ghiglo' };

    One world, one people

Re: Hash value modification
by siliconGopher (Initiate) on Jul 25, 2005 at 12:21 UTC
    Im not sure I understand the inconsistancy. I performed similar operations on both, a hash and an array and I found the results as expected. Using the following code.

    #!/usr/bin/perl use Data::Dumper; my %hash = ( b => bell, c => cell, d => dell, f => fell, h => hell ); foreach ( values %hash ) { s/ell/it/g }; print Dumper( \%hash ); my @array = qw ( b bell c cell d dell f fell h hell ); foreach ( @array ) { s/ell/it/g }; print Dumper ( \@array );
    The results were as follows,
    $VAR1 = { 'c' => 'cit', 'h' => 'hit', 'b' => 'bit', 'd' => 'dit', 'f' => 'fit' }; $VAR1 = [ 'b', 'bit', 'c', 'cit', 'd', 'dit', 'f', 'fit', 'h', 'hit' ];

    Wisdom:For every problem, there is a solution that is simple, neat and wrong.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://477712]
Approved by jbrugger
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2022-09-27 17:50 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (122 votes). Check out past polls.