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

What if I have to remove a particular hash entry. For example, from the below code I want to remove the hash having the keys {'2'}, {'0'}, and so on. I don't want the hash entries where single digit numbers are keys

%hash = { abc => { def => 1, ghi => 2, jkl => 3, }, mno => { pqr => 4, stu => 3, vwx => 5, }, 2 => { def => 5, stu => 2, }, 0 => { def => 3, stu => 1, }, };

Replies are listed 'Best First'.
Re: Deleting hash entry
by haukex (Archbishop) on May 12, 2017 at 07:09 UTC
    I don't want the hash entries where single digit numbers are keys

    You can delete a hash slice:

    delete @hash{0..9};

    Note that because your question isn't directly related to the OP, and the OP is ~13 years old, it would have been better if this was posted as a new question, which is why I've considered your node. Update: Node has since been reparented, and I added link to the original parent node.

Re: Deleting hash entry
by Laurent_R (Canon) on May 12, 2017 at 22:15 UTC
    This may probably be what you are looking for:
    use strict; use warnings; use Data::Dumper; my $hash = { 'abc' => { def => 1, ghi => 2, jkl => 3, }, 'mno' => { pqr => 4, stu => 3, vwx => 5, }, 2 => { def => 5, stu => 2, }, 0 => { def => 3, stu => 1, }, }; print Dumper $hash; for my $key (keys %$hash) { delete $$hash{$key} if $key =~ /^\d+$/ } print Dumper $hash;
    Output:
    $ perl delete_hash.pl $VAR1 = { 'mno' => { 'pqr' => 4, 'vwx' => 5, 'stu' => 3 }, '0' => { 'def' => 3, 'stu' => 1 }, 'abc' => { 'def' => 1, 'jkl' => 3, 'ghi' => 2 }, '2' => { 'def' => 5, 'stu' => 2 } }; $VAR1 = { 'mno' => { 'pqr' => 4, 'vwx' => 5, 'stu' => 3 }, 'abc' => { 'def' => 1, 'jkl' => 3, 'ghi' => 2 } };
      I like your solution. The regex could be different if OP really only wants to delete keys with single digits instead of multiple digits. I suggest using the arrow notion when using a hash reference.
      use strict; use warnings; use Data::Dumper; my $hash = { 'abc' => { def => 1, ghi => 2, jkl => 3, }, 'mno' => { pqr => 4, stu => 3, vwx => 5, }, 2 => { def => 5, stu => 2, }, 0 => { def => 3, stu => 1, }, 123 => { xyzzy => "and nothing happens" }, }; #print Dumper $hash; for my $key (keys %$hash) { delete $hash->{$key} if $key =~ /^\d$/ } print Dumper $hash; __END__ $VAR1 = { '123' => { 'xyzzy' => 'and nothing happens' }, 'abc' => { 'def' => 1, 'jkl' => 3, 'ghi' => 2 }, 'mno' => { 'stu' => 3, 'pqr' => 4, 'vwx' => 5 } };
        I suggest using the arrow notion when using a hash reference.
        I agree that the arrow notation tends to be clearer.

        That's actually what I use most of the time, or at least what I have been using most of the time in the last four to five years, but I sometimes still use the other notation ($$hash{$key}), especially when it is very simple.

Re: Deleting hash entry
by johngg (Canon) on May 12, 2017 at 20:34 UTC

    Note that braces - { abc => 123, def => 456 } - create a reference to an anonymous hash which should be assigned to a scalar, e.g. my $hash = { abc => 123, def => 456 };. If you want an actual hash use parentheses - my %hash = ( abc => 123, def => 456 ); - instead.

    Cheers,

    JohnGG