On Jul 5, 2012, at 7:57 PM, xiyoulaoyuanjia wrote:

>   - 1.first do you meaning  values(%{$a})  as same as values($a) which $a
>   is  references starting from perl-5.14 .

If $a is a reference to a hash, then %{$a} and %$a represent that hash. 

For Perls earlier than 5.12, the argument to values() must be a hash. Starting 
with 5.12, the argument may also be an array. Starting with 5.14, the argument 
may be a reference to an unblessed hash or array. In all of the cases, values() 
returns a list of appropriate values.

> 
> 2. i did not know why  I compare two lists ? values($a) return the value of
> hash which has only one element. so i just think  values($a)  return a list
> just when $a has more than one element.
> and i modify  this to below
> ------------------------code
> begin-----------------------------------------------------------
> perl -e '
>  @AoH = (
>    (
>       rsh => "0.4",
>    ),
>    (
>       telnet=> "0.022",
>    ),
> 
>    (
>       ssh => "0.3",
>    ),
>  );

That is not a hash, nor an array of hashes, but an array of 6 elements. Your 
original program had {} around the inner elements, not (), which made it an 
array of hash references.

>  print values($_) for sort my_sort @AoH; sub my_sort {
> values($a)<=>values($b)}
> '
> ------------------------code
> end-----------------------------------------------------------
> the error is ” Type of argument to values on reference must be unblessed
> hashref or arrayref at -e line 15.“
> i did not know why .

The argument to the sort() function is an array of scalars. Therefore, the $a 
and $b variables will hold scalars. Therefore, attempting to call values() with 
those scalars as arguments is a runtime error.

> IMO  $a is a hash not a hash reference。it  Iteration values   ( rsh =>
> "0.4",) which values will return 0.4, and another.
> can you tell me the reason !

$a cannot be a hash, because is has a '$' in its name. Therefore, it can only 
hold a scalar value. In your original program, the elements of @AoH were hash 
references, so you could fetch the values of the hash with values(%$a) or (in 
5.14 or later) values($a). Since there was only one pair of values in each 
hash, values will return a list of one element. However, it is still a list, 
and in a scalar context the value of the list is the number of elements (1 in 
this case). So your original program was comparing the lengths of the hashes, 
not the hash values.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to