finding the number of keys in hash

D

DnaDude

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 = $#{@foo};

($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.
 
U

Uri Guttman

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 = $#{@foo};

perldoc -f keys.

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

uri
 
D

DnaDude

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 = $#{@foo};

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.
 
U

Uri Guttman

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
 
P

Paul Lalli

DnaDude said:
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 = $#{@foo};

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
 
A

Alan Mead

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

try perldoc -f length (and NOTE the note)
I'd like to do something like

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

Here's what you want, I think.. but why?

for (my $i=0; $i< scalar keys %$A; $i++) { print "$i\n"; }

for a three-pair hash, prints:

0
1
2

-Alan
 
P

Paul Lalli

DnaDude said:
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
 
J

Joe Smith

DnaDude said:
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 = $#{@foo};

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

$number = $#{@foo} + 1;
$number = $#foo + 1;
$number = @foo;

-Joe
 
J

Jürgen Exner

DnaDude said:
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 = $#{@foo};

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
 
K

Kjetil Skotheim

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 = $#{@foo};

($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;
 
J

Jürgen Exner

Kjetil said:
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
 
G

gbh

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 = $#{@foo};

($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.
 
P

Paul Lalli

Jürgen Exner said:
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.
 
A

Anno Siegel

Alan Mead said:
Here's what you want, I think.. but why?

for (my $i=0; $i< scalar keys %$A; $i++) { print "$i\n"; }
^^^^^^
No need for "scalar", since "<" puts its arguments in scalar context.
A bit shorter is

for my $i ( 0 .. keys %$A ) { ... }

Anno
 
U

Uri Guttman

AS> ^^^^^^
AS> No need for "scalar", since "<" puts its arguments in scalar context.
AS> A bit shorter is

AS> for my $i ( 0 .. keys %$A ) { ... }

off by one error

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top