in reply to Re: regex & variable substitution
in thread regex & variable substitution

Excellent - that's spot on! OK, so I now have a question: doesn't the plain
$lookup{$1}
create the key (and assign a value of null) if the key does not exist? Is it not better to do either "defined" or "exists" for the key? Anyway, my failing was the braces - getting that right made it work. many thanks...


-- Ian Stuart
A man depriving some poor village, somewhere, of a first-class idiot.

Replies are listed 'Best First'.
Re^3: regex & variable substitution
by holli (Abbot) on Jul 11, 2005 at 09:49 UTC
    No, the key will only autivify when you assign a value to it.

    Prove:
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %h; print $h{key} ? $h{key} : "nokey"; print "\n", Dumper (\%h);
    See? The hash stays emtpy.

    Update: exists, defined and the check for truth do different things. Consider:
    #!/usr/bin/perl use strict; use warnings; my %h = ( a => "", b => undef, c => "true"); for ( qw(a b c d) ) { print "key: $_ is " . ($h{$_} ? "" : "not ") . "true\n"; print "key: $_ is " . (defined $h{$_} ? "" : "not ") . "defined\n" +; print "key: $_ is " . (exists $h{$_} ? "" : "not ") . "existing\n +"; } #key: a is not true #key: a is defined #key: a is existing #key: b is not true #key: b is not defined #key: b is existing #key: c is true #key: c is defined #key: c is existing #key: d is not true #key: d is not defined #key: d is not existing


    holli, /regexed monk/
      That update is really very interesting, and very helpful! Thanks for posting it...


      -- Ian Stuart
      A man depriving some poor village, somewhere, of a first-class idiot.