Tyke has asked for the wisdom of the Perl Monks concerning the following question:
my %a=('a',1,'b',2,'c',3);
print for @a{'a','b'};
work fine. However
my $a={'a',1,'b',2,'c',3};
print for @a->{'a','b'};
spits out the error message
Can't coerce array into hash
The quick solution is, of course, to dereference the hash ref into a temporary variable before slicing:
my $a={'a',1,'b',2,'c',3};
my %a=%$a;
print for @a{'a','b'};
Somehow this seems ugly. So does anyone know how I can use
the arrow operator to get a hash slice directly from the
reference without the need for a temporary.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Slicing Hash References
by mirod (Canon) on Mar 01, 2001 at 16:52 UTC | |
by Tyke (Pilgrim) on Mar 01, 2001 at 17:14 UTC |