#### Do you have a question? Post it now! No Registration Necessary. Now with pictures!

**posted on**

November 19, 2004, 8:16 pm

keys in a hash, as in below, but without

using a temporary variable.

#this works

use strict;

@foo = keys %$A;

$number = $#;

($A is a reference to a hash). So why does

$number = $#{keys %$A};

or

$number = $#{@{keys %$A}};

not work? I get the error

"Can't use string ("357") as an ARRAY ref while "strict refs" in use"

Many thanks,

Cev.

## Re: finding the number of keys in hash

D> I would like to compute the number of

D> keys in a hash, as in below, but without

D> using a temporary variable.

D> #this works

D> use strict;

D> @foo = keys %$A;

D> $number = $#;

perldoc -f keys.

you are using it but there is more than one way to use it.

uri

## Re: finding the number of keys in hash

> D> I would like to compute the number of

> D> keys in a hash, as in below, but without

> D> using a temporary variable.

>

> D> #this works

> D> use strict;

> D> @foo = keys %$A;

> D> $number = $#;

>

> perldoc -f keys.

I've read that documentation but it doesn't seem to answer my

problem: I'd like to do something like

for(my $i=0; $i < $#{keys %$A}; $i++){

}

without having to define an auxiliary variable.

Cev.

## Re: finding the number of keys in hash

D> Uri Guttman wrote:

>> perldoc -f keys.

D> I've read that documentation but it doesn't seem to answer my

D> problem: I'd like to do something like

reread it again.

D> for(my $i=0; $i < $#{keys %$A}; $i++){

D> }

D> without having to define an auxiliary variable.

well that doesn't use an auxiliary variable but rather an anon hash. in

any case the docs for keys has your answer and it is very early in

there.

and why would you do such a loop anyhow? looping over the keys makes

sense but looping over the number of keys without accessing them makes

little sense. and c style for loops are not popular in perl so you may

have an XY problem here as well.

uri

## Re: finding the number of keys in hash

>

> > D> I would like to compute the number of

> > D> keys in a hash

> >

> > perldoc -f keys.

>

> I've read that documentation but it doesn't seem to answer my

> problem

Uh. It doesn't? Can you tell me what the 2nd sentence of that document

says? (The one in parentheses)?

Paul Lalli

## Re: finding the number of keys in hash

> I would like to compute the number of

> keys in a hash, as in below, but without

> using a temporary variable.

>

> #this works

> use strict;

> @foo = keys %$A;

> $number = $#;

Frankly, I find that surprising. I'm not disagreeing that it works,

because I have no interest in trying it. I'm just surprised.

In any case, you're making it harder than it is. 'keys' can be used in

scalar context:

$number = keys %$A;

Paul Lalli

> keys in a hash, as in below, but without

> using a temporary variable.

>

> #this works

> use strict;

> @foo = keys %$A;

> $number = $#;

Frankly, I find that surprising. I'm not disagreeing that it works,

because I have no interest in trying it. I'm just surprised.

In any case, you're making it harder than it is. 'keys' can be used in

scalar context:

$number = keys %$A;

Paul Lalli

## Re: finding the number of keys in hash

DnaDude wrote:

> I would like to compute the number of

> keys in a hash, as in below, but without

> using a temporary variable.

>

> #this works

> use strict;

> @foo = keys %$A;

> $number = $#;

That is an error; it's off by one.

$number = $# + 1;

$number = $#foo + 1;

$number = @foo;

-Joe

> I would like to compute the number of

> keys in a hash, as in below, but without

> using a temporary variable.

>

> #this works

> use strict;

> @foo = keys %$A;

> $number = $#;

That is an error; it's off by one.

$number = $# + 1;

$number = $#foo + 1;

$number = @foo;

-Joe

## Re: finding the number of keys in hash

> I would like to compute the number of

> keys in a hash, as in below, but without

> using a temporary variable.

>

> #this works

> use strict;

> @foo = keys %$A;

> $number = $#;

This gives you the last index in the array, which (unless you fooled around

with $[ ) will be one less then the number of elements.

Why not a simple

$number = keys %$A;

Or if you do want the minus-one number just substract 1:

$number = (keys %$A) -1 ;

jue

## Re: finding the number of keys in hash

> I would like to compute the number of keys in a hash,

> as in below, but without using a temporary variable.

>

> #this works

> @foo = keys %$A;

> $number = $#;

>

> ($A is a reference to a hash). So why does

>

> $number = $#{keys %$A}; or

> $number = $#{@{keys %$A}};

>

> not work?

Try this:

print "The number of keys is: ".(0+keys%$A)."\n";

The secret is forcing scalar context upon keys%$A, this works as well:

$num

___of___keys=keys%$A;

## Re: finding the number of keys in hash

Kjetil Skotheim wrote:

> print "The number of keys is: ".(0+keys%$A)."\n";

>

> The secret is forcing scalar context upon keys%$A,

True, but you got a little bit confused.

- You add 0 if you want to ensure numerical treatment of your values

- You use scalar() to enforce scalar context (big surprise there)

If you would add zero to enforce scalar context then you would get wrong

results in case your value is a string, not a number.

jue

> print "The number of keys is: ".(0+keys%$A)."\n";

>

> The secret is forcing scalar context upon keys%$A,

True, but you got a little bit confused.

- You add 0 if you want to ensure numerical treatment of your values

- You use scalar() to enforce scalar context (big surprise there)

If you would add zero to enforce scalar context then you would get wrong

results in case your value is a string, not a number.

jue

## Re: finding the number of keys in hash

> Kjetil Skotheim wrote:

> > print "The number of keys is: ".(0+keys%$A)."\n";

> >

> > The secret is forcing scalar context upon keys%$A,

>

> True, but you got a little bit confused.

> - You add 0 if you want to ensure numerical treatment of your values

> - You use scalar() to enforce scalar context (big surprise there)

Slight clarification - scalar() is used when the expression would

produce a list context without it. In the above example, the .

operators force scalar context as they are.

print "The number of keys is: " . (keys %$A) . "\n";

Of course, nothing will be harmed by adding scalar() to this example,

but it is redundant.

> > print "The number of keys is: ".(0+keys%$A)."\n";

> >

> > The secret is forcing scalar context upon keys%$A,

>

> True, but you got a little bit confused.

> - You add 0 if you want to ensure numerical treatment of your values

> - You use scalar() to enforce scalar context (big surprise there)

Slight clarification - scalar() is used when the expression would

produce a list context without it. In the above example, the .

operators force scalar context as they are.

print "The number of keys is: " . (keys %$A) . "\n";

Of course, nothing will be harmed by adding scalar() to this example,

but it is redundant.

## Re: finding the number of keys in hash

> I would like to compute the number of

> keys in a hash, as in below, but without

> using a temporary variable.

>

> #this works

> use strict;

> @foo = keys %$A;

> $number = $#;

>

> ($A is a reference to a hash). So why does

>

> $number = $#{keys %$A};

> or

> $number = $#{@{keys %$A}};

>

> not work? I get the error

>

> "Can't use string ("357") as an ARRAY ref while "strict refs" in use"

>

> Many thanks,

>

> Cev.

>

Whats wrong with "$n = scalar(keys %hash);" ?

Just add package names with strict.

--

gbh

gbh04 is a spamtrap

all post is deleted

#### Site Timeline

- » FAQ 1.3 Which version of Perl should I use?
- — Next thread in » PERL Discussions

- » Configurable timeout value of Cyrus::IMAP::Admin module
- — Previous thread in » PERL Discussions

- » s suffix question
- — Newest thread in » PERL Discussions

- » HTML 5 validation software?
- — The site's Newest Thread. Posted in » HTML Authoring Forum