in reply to Sometimes undef is initialized and sometimes not when hash values are fed to grep

We had a similar discussion recently.°

What's happening in "third try" is that you are passing an alias to map which is forcing an autovivification.

The second try is only a copy.

One workaround would be to surround every value with quotes "$h{bla}" , to enforce a copy.

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

°) I'll link to it ... here it is: unexpected modify hash in a distance with grep { $_ }

Well, it's the last thread you started and the one you linked to (!?!)

  • Comment on Re: Sometimes undef is initialized and sometimes not when hash values are fed to grep (already discussed)
  • Download Code

Replies are listed 'Best First'.
Re^2: Sometimes undef is initialized and sometimes not when hash values are fed to grep
by LanX (Saint) on Feb 11, 2020 at 16:47 UTC
    Here two or three workarounds (can't test the latter)

    use utf8; use strict; use warnings; use Data::Dumper; my %h = (a => 'alfa', b => 'beta'); print "\n\nWorkaround 1 \n"; print map { "\t>>$_<<\n" } "$h{a}", "$h{111}", "$h{b}"; print "after third:\n", Dumper(\%h); print "\n\nWorkaround 2 \n"; print map { "\t>>$h{$_}<<\n" } qw/a 222 b/; print "after third:\n", Dumper(\%h); # print "\n\nWorkaround 3 \n"; # no autovivification; # print map { "\t>>$_<<\n" } $h{a}, $h{333}, $h{b}; # print "after third:\n", Dumper(\%h);

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

      can't test the latter

      Unfortunately, that one doesn't work:

      use warnings; use strict; use Data::Dumper; no autovivification; my %h = (a => 'alfa', b => 'beta'); print map { ">>$_<<\n" } $h{a}, $h{333}, $h{b}; print Dumper(\%h); __END__ Use of uninitialized value $_ in concatenation (.) or string at 111127 +86.pl line 8. >>alfa<< >><< >>beta<< $VAR1 = { '333' => undef, 'a' => 'alfa', 'b' => 'beta' };
        ugh .... :/

        But I seem to remember that this glitch was also mentioned in the last thread.

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