Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a hash (which varies in size) and am using it in the following code.
foreach my $key (sort {$a cmp $b} keys %$hash) { if (scalar %{$$hash{$key}} > 2) { #do something } else { #do something else } }
Which worked fine during my initial test cases. However, I just loaded a large sample of our real data into the system to run more tests and now I am getting the following error every 16th or 17th key:

Argument "2/8" isn't numeric in numeric gt (>)

sometimes the "2/8" is replaced by "1/8" .. and the error (warning?) repeats approximately 20 times (didn't count) before it continues.

No data is dropped/lost, it just displays that to stdout every 16th or 17th key.

what I am doing wrong?

Replies are listed 'Best First'.
Re: Scaler of a hash, odd problem
by Elian (Parson) on Dec 12, 2002 at 16:47 UTC
    Your code's broken. Hashes in scalar context don't return the number of keys in the hash, they return a bit of debugging info.

    If you want the number of entries in the hash, use keys in scalar context -- scalar(keys %foo) for example.

      duh.

      thanks
Re: Scaler of a hash, odd problem
by fruiture (Curate) on Dec 12, 2002 at 16:53 UTC

    A hash in scalar context returns such a string, as pointed out in perldata: "the number of used buckets and the number of allocated buckets"

    If you want the number of elements, use keys in scalar context.

    --
    http://fruiture.de