H
Harald
HI!
I am a perl newbie and have a problem which can be reduced to the following:
I have a hash-reference of a hash, which values are references to
arrays. Now I want to know the length of the first value. I try it with:
use strict;
my $hash = {"A"=>[1,2,3,4], "B"=>[5,6,7,8]};
my $length = $#{values(%{$hash})[0]};
print $length;
But if I run it, perl says:
##############
Global symbol "$length" requires explicit package name at t.pl line 5.
Execution of t.pl aborted due to compilation errors.
##############
what is absolutely strange to me.
If I comment out line 5 (which is "print ..."), I receive the error:
###############
syntax error at t.pl line 4, near ")["
Execution of t.pl aborted due to compilation errors.
###############
Which is nearly as strange to me than the first error-message.
As far as I understand, $hash is dereferenced by %{$hash}.
Then I get a list of all its values by values( %{$hash} ).
Then I get its first value by values( %{$hash} )[0].
Then I dereference it and get its size minus 1 by $#{values(%{$hash})[0]}
Could anyone tell me where my mistake is?
Regards and thanks in advance,
Harald
I am a perl newbie and have a problem which can be reduced to the following:
I have a hash-reference of a hash, which values are references to
arrays. Now I want to know the length of the first value. I try it with:
use strict;
my $hash = {"A"=>[1,2,3,4], "B"=>[5,6,7,8]};
my $length = $#{values(%{$hash})[0]};
print $length;
But if I run it, perl says:
##############
Global symbol "$length" requires explicit package name at t.pl line 5.
Execution of t.pl aborted due to compilation errors.
##############
what is absolutely strange to me.
If I comment out line 5 (which is "print ..."), I receive the error:
###############
syntax error at t.pl line 4, near ")["
Execution of t.pl aborted due to compilation errors.
###############
Which is nearly as strange to me than the first error-message.
As far as I understand, $hash is dereferenced by %{$hash}.
Then I get a list of all its values by values( %{$hash} ).
Then I get its first value by values( %{$hash} )[0].
Then I dereference it and get its size minus 1 by $#{values(%{$hash})[0]}
Could anyone tell me where my mistake is?
Regards and thanks in advance,
Harald