Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: Deleting hash entry

by Marshall (Canon)
on May 13, 2017 at 00:28 UTC ( [id://1190174]=note: print w/replies, xml ) Need Help??


in reply to Re: Deleting hash entry
in thread Deleting hash entry

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 } };

Replies are listed 'Best First'.
Re^3: Deleting hash entry
by Laurent_R (Canon) on May 13, 2017 at 15:34 UTC
    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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-25 20:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found