Re: fwp Digest 19 Oct 2004 13:50:49 -0000 Issue 310

[email protected] (Gaal Yahas)
Newsgroups perl.fwp
Message-ID <[email protected]>
Jason Foster <[email protected]> wrote:

This is probably not the place for these kind of questions, but here
goes anyway.

> Can anyone help me to understand why this code refuses to compile?                                      
> Even better, can anyone help fix it :)                                                                  
>                                                                                                         
>     %hash = qw( fred filntstone barney rubble tom delong );                                             
>     print( keys( reverse( %hash ) ) );                                                                  
>                                                                                                         
> The error message...                                                                                    
>                                                                                                         
>     Type of arg 1 to keys must be hash (not reverse) at ./killme.pl                                     
> line 4, near ") ) "                                                                                     
>                                                                                                         
> ... was pretty confusing since it implies that "reverse" is a type?!                                    

The implication was meant to just indicate what you were saying instead
of what perl was expecting. reverse is not a type :)

But the type of what reverse returns is a list, not a hash. I wondered
about this once too. Your attempt at a solution:

>     print( keys( %{ reverse( %hash ) } ) );                                                             

was almost right, but not quite. You can't dereference a list, but you
can deref a reference to an anonymous hash:

     print keys %{ { reverse %hash } };

Note, by the way, that if you know your data contains no duplicate
values, you could simply have done this:

     print values %hash;

-- 
Gaal Yahas <[email protected]>
http://gaal.livejournal.com/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.