Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

length or array in a hash

by ongaku (Initiate)
on Nov 09, 2012 at 19:31 UTC ( #1003200=perlquestion: print w/replies, xml ) Need Help??
ongaku has asked for the wisdom of the Perl Monks concerning the following question:

Here is a short program that shows that I can get to an array stored in a hash. But how do I find out the length of said array?
# the program my %hash; my @arr; %hash = ( 'one' => [1], 'two' => [2,2], 'three' => [3,3,3], ); print "$hash{one}[0]\n"; print "$hash{two}[0]\n"; print "$hash{three}[0]\n"; $size = length $hash{one}; print ("size=$size\n"); $size = length $hash{two}; print ("size=$size\n"); print "all done\n"; % perl 1 2 3 size=16 size=16 all done

Replies are listed 'Best First'.
Re: length or array in a hash
by SuicideJunkie (Vicar) on Nov 09, 2012 at 19:46 UTC

    You're printing the length of a string that goes something like "ARRAY(0xDEADBEEF)"

    You need to dereference the arrayref: my $itemCount = @{ $hash{three} };  my $lastIndex = $#{ $hash{three}  }

Re: length or array in a hash
by brap (Pilgrim) on Nov 09, 2012 at 19:45 UTC

    Hi ongaku,

    perldoc -f length says that length isn't the right thing to use to find the number of elements in the array (I am assuming here that you're looking for the number of elements in the array). I think something along the lines of scalar @{$hash{one}} might be what you're looking for.

Re: length or array in a hash
by 2teez (Vicar) on Nov 09, 2012 at 19:54 UTC

    Hi, ongaku,
    You can do like so:

    %hash = ( 'one' => [1], 'two' => [2,2], 'three' => [3,3,3], ); foreach my $array (keys %hash){ print $array," :", scalar @{$hash{$array}},$/; }
    three :3 one :1 two :2
    You might want to check the function scalar

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: length or array in a hash
by Cristoforo (Curate) on Nov 09, 2012 at 19:57 UTC
    Your answer is here. Just look for the (second) listed type of variable, arrays.

    Here is the bit you want to see (about arrays in scalar context).

    You might be tempted to use $#array + 1 to tell you how many items there are in an array. Don't bother. As it happens, using @array where Perl expects to find a scalar value ("in scalar context") will give you the number of elements in the array: 1. if (@animals < 5) { ... }
    Here is your program using those features.
    #!/usr/bin/perl use strict; use warnings; # the program my %hash = ( 'one' => [1], 'two' => [2,2], 'three' => [3,3,3], ); print "$hash{one}[0]\n"; print "$hash{two}[0]\n"; print "$hash{three}[0]\n"; while (my ($key, $aref) = each %hash) { # dereference array_reference # (and the @$aref array is in scalar context) printf "key=%s size=%d\n", $key, @$aref; }
    1 2 3 key=three size=3 key=one size=1 key=two size=2
      #!/usr/bin/perl + + use strict; use warnings; my %hash = ( one => [1], two => [ 2, 2 ], three => [ 3, 3, 3 ], ); for ( sort { $hash{$a} <=> $hash{$b} } keys %hash ) { print qq($_\t) . scalar( @{ $hash{$_} } ) . qq(\n); } print qq(\nVariatio delectat.\n);

      Regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

        Thanks to all who replied and helped me out.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1003200]
Approved by davido
[davido]: jdporter : Is there a specific issue, or are we all just getting up there?

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2018-06-21 16:40 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.