Re: hash keys

[email protected] (Rob Dixon)
Newsgroups perl.beginners
Message-ID <[email protected]>
beast wrote:
> Hi All,
> 
> 
> Why this following code has not working as expected?
> 
>     print "Number of element(s) : " . sprintf("%10d", keys(%hash) ) . "\n";

keys(%hash) returns the hash keys as a list, so

  sprintf("%10d", keys(%hash) )

is like

  sprintf("%10d", 'key3', 'key4', 'key1', 'key2', 'key5');

so the first key will be formatted as %10d and returned as a string.

I suggest you write

  printf "Number of element(s) : %10d\n", scalar keys %hash;

HTH,

Rob
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.