Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: unexpected modify hash in a distance with grep { $_ }

by afoken (Canon)
on Dec 20, 2019 at 17:42 UTC ( #11110438=note: print w/replies, xml ) Need Help??


in reply to unexpected modify hash in a distance with grep { $_ }

printf "grep: %s\n", (join " ... ", grep { $_ }  $$h{a}, $$h{x}, $$h{b}, $$h{c});

Not a problem with grep. $$h{x} is autovivified before grep happens.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^2: unexpected modify hash in a distance with grep { $_ }
by LanX (Archbishop) on Dec 20, 2019 at 18:19 UTC
    > Not a problem with grep. $$h{x} is autovivified before grep happens.

    Nope, it happens because of the aliasing of the hash value inside grep, but I'm not sure how.

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $h = { 'a' => 1, 'b' => 2, 'c' => 3, }; warn Dumper [ $$h{a}, $$h{x}, $$h{b}, $$h{c} ]; warn Dumper [ keys %$h ]; warn Dumper [ grep {$_} $$h{a}, $$h{x}, $$h{b}, $$h{c} ]; warn Dumper [ keys %$h ]; warn !$$h{y}; warn Dumper [ keys %$h ];

    C:/Perl_524/bin\perl.exe d:/tmp/pm/grep_autovivify.pl $VAR1 = [ 1, undef, 2, 3 ]; $VAR1 = [ 'b', 'a', 'c' ]; $VAR1 = [ 1, 2, 3 ]; $VAR1 = [ 'a', 'x', 'c', 'b' ]; 1 at d:/tmp/pm/grep_autovivify.pl line 19. $VAR1 = [ 'a', 'x', 'c', 'b' ];

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Nope, it happens because of the aliasing of the hash value inside grep, but I'm not sure how.

      Interesting effect. I see your output with plain perl v5.22.2 (from 2016) on Linux x86-64 and with Strawberry v5.14.2 (from 2011) on Windows 64 bit. So this seems to be an old, unsolved problem.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Maybe I should file a bug on that? I will report bug for Debian...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2020-05-27 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (152 votes). Check out past polls.

    Notices?