Re: hash keys
[email protected] ("John W. Krahn")
| Newsgroups | perl.beginners |
|---|---|
| Message-ID | <[email protected]> |
Paul Lalli wrote: > 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, $ perl -le'print prototype "CORE::sprintf"' $@ The *second* argument to this *particular* function is a list. > and so > Perl is expecting a list of values to be passed to sprintf(). A scalar and a list. The first argument is interpreted in scalar context: $ perl -le'@x = "a" .. "z"; print sprintf @x' 26 $ perl -le'print sprintf "a" .. "z"' 1E0 John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall