in reply to Re: hash substrings
in thread hash substrings

foreach my $k (@k) { $s =~ m/(.)$k(.)/ ; if (($1 ne "\0") || ($2 ne "\0")) { delete $h{$k} ; } ; }

You shouldn't use the variables $1 and $2 unless the match was successful because they retain their previous value if the match failed and so after the first successful match every key will be deleted.

for my $k ( @k ) { if ( $s =~ /\0$k\0/ ) { delete $h{ $k }; } }

Replies are listed 'Best First'.
Re^3: hash substrings
by gone2015 (Deacon) on Jan 27, 2009 at 10:54 UTC

    Good general advice... in this case, however, every match will succeed -- the test is required to spot the key matching itself. For the paranoid one could write:

    foreach my $k (@k) { next unless $s =~ m/(.)$k(.)/ ; next if (($1 eq "\0") && ($2 eq "\0")) ; delete $h{$k} ; } ;
    but the next unless is redundant.