G'day ultranerds,
Just put braces around $foo in your hash slice. [That's not strictly correct — see Update (Clarification) below.]
#!/usr/bin/env perl use strict; use warnings; my $foo = { testing => 'bar', test => 'test2', something => 'test3' }; delete @{$foo}{qw(testing something)}; use Data::Dump; dd $foo;
Output:
{ test => "test2" }
Update (Clarification)
In your OP, you have:
"I know if it was just a simple hash, I could delete multiple fields with: delete @$foo{qw(testing something)} ..."
If that was a "simple hash" (e.g. %foo), then the detete statement would need to have "@foo", not "@$foo".
I suspect having seen "simple hash", my eyes glossed over the $ before foo and some mental confusion ensued.
So, the delete statement you showed would have worked fine for a hashref (e.g. $foo) but not for a hash (e.g. %foo); and, my statement, "Just put braces around $foo ...", wasn't strictly correct in that it didn't address your problem even though it was valid syntax which achieved the end result you were looking for.
To quickly summarise the syntax for specifying a hash slice:
While I typically write %$hashref, @$arrayref and even $#$arrayref, I do tend to use braces for anything more complex, including slices.
[If you're interested, Perl Best Practices recommends putting braces around references in all of those (i.e. %{$hashref}, @{$arrayref} and $#{$arrayref}). Obviously, I don't follow all of the Perl Best Practices. :-)]
-- Ken
In reply to Re: Remove multiple fields from hashref?
by kcott
in thread Remove multiple fields from hashref?
by ultranerds
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |