Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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 arrays.pl 1 2 3 size=16 size=16 all done

Comment on length or array in a hash
Download Code
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 SuicideJunkie (Priest) 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 2teez (Priest) 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}},$/; }
    Output
    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 (Deacon) 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; }
    Prints:
    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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1003200]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-09-17 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (55 votes), past polls