Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

No: of values in a hashtable

by Anonymous Monk
on Sep 17, 2012 at 12:13 UTC ( #994025=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

push @{ $weekly_hash{$i} },"mon","ret","tue","retik","wed","true"; $value_count=grep {defined} values %weekly_hash; print $value_count;

Hi, In the above code, i am checking the number of values corresponding to key 1 .For key 1 i have 6 values.But as per the code its returning the count as 1.Please help.For my requirement i need to dynamically create the array.So have used push statement.

Replies are listed 'Best First'.
Re: No: of values in a hashtable
by choroba (Chancellor) on Sep 17, 2012 at 12:17 UTC
    The value is a reference to an array. If you want to count its members, you have to dereference it first. What you are after is rather something like the following:
    $value_count += @$_ for values %weekly_hash;
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: No: of values in a hashtable
by tobyink (Abbot) on Sep 17, 2012 at 12:27 UTC
    $value_count = grep(defined, @{ $weekly_hash[$i] });
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: No: of values in a hashtable
by AnomalousMonk (Canon) on Sep 17, 2012 at 20:19 UTC

    Sometimes it's very helpful to write a small example (as you have done in your OP) and see the result. Data::Dumper and Data::Dump are very useful for this.

    >perl -wMstrict -le "use Data::Dumper; ;; my %hash; ;; push @{ $hash{'one'} }, qw(fee fie foe fum), undef, 'oops'; print Dumper \%hash; ;; my $defined_pushed = grep defined, @{ $hash{'one'} }; print $defined_pushed; " $VAR1 = { 'one' => [ 'fee', 'fie', 'foe', 'fum', undef, 'oops' ] }; 5

    See also Basic debugging checklist.

Re: No: of values in a hashtable
by kcott (Chancellor) on Sep 18, 2012 at 06:15 UTC
    From perldata:
    "If you evaluate an array in scalar context, it returns the length of the array."
    From scalar:
    "Forces EXPR to be interpreted in scalar context ..."

    Accordingly, scalar @{ $weekly_hash{$i} } will return the number of values:

    $ perl -Mstrict -Mwarnings -E ' my %weekly_hash; my $i = 1; push @{ $weekly_hash{$i} },"mon","ret","tue","retik","wed","true"; say scalar @{ $weekly_hash{$i} }; ' 6

    Your code shows no undefined values. Your text has "... the number of values ...". An undefined value is still a value:

    $ perl -Mstrict -Mwarnings -E ' my %weekly_hash; my $i = 1; push @{ $weekly_hash{$i} },"mon","ret","tue","retik","wed","true", und +ef; say scalar @{ $weekly_hash{$i} }; ' 7

    If you really meant that you only wanted a count of defined values, you can do this:

    $ perl -Mstrict -Mwarnings -E ' my %weekly_hash; my $i = 1; push @{ $weekly_hash{$i} },"mon","ret","tue","retik","wed","true", und +ef; say scalar @{[ grep { defined } @{ $weekly_hash{$i} } ]}; ' 6

    The count of elements in an array is always available via scalar; there's no need to introduce temporary count variables.

    -- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994025]
Approved by BrowserUk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2017-05-29 06:36 GMT
Find Nodes?
    Voting Booth?