Re: hash keys

[email protected] (Paul Lalli)
Newsgroups perl.beginners
Organization http://groups.google.com
Message-ID <[email protected]>
On Jun 12, 12:15 am, [email protected] (Beast) wrote:

> Why this following code has not working as expected?
>
>     print "Number of element(s) : " . sprintf("%10d", keys(%hash) ) . "\n";

The keys() function does two different things, depending on context.

In scalar context, it returns the number of key/value pairs in the
hash.

In list context, it returns a list of the keys.

You probably knew that, but didn't realize that you're using it in a
list context here.   The arguments to a function are a list, and so
Perl is expecting a list of values to be passed to sprintf().
Anywhere Perl expects a list, if it sees a function/subroutine, it
calls that function/subroutine in list context.

Paul Lalli
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.