Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > finding the number of keys in hash

Reply
Thread Tools

finding the number of keys in hash

 
 
DnaDude
Guest
Posts: n/a
 
      11-19-2004
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.
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      11-19-2004
>>>>> "D" == DnaDude <(E-Mail Removed)> writes:

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
 
Reply With Quote
 
 
 
 
DnaDude
Guest
Posts: n/a
 
      11-19-2004
Uri Guttman wrote:
>>>>>>"D" == DnaDude <(E-Mail Removed)> writes:


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

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      11-19-2004
>>>>> "D" == DnaDude <(E-Mail Removed)> writes:

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
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      11-19-2004
"DnaDude" <(E-Mail Removed)> wrote in message
news:cnlgqh$l4h$(E-Mail Removed)...
> 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

 
Reply With Quote
 
Alan Mead
Guest
Posts: n/a
 
      11-19-2004
On Fri, 19 Nov 2004 14:25:06 -0500, DnaDude wrote:

> Uri Guttman wrote:
>>>>>>>"D" == DnaDude <(E-Mail Removed)> writes:

>
>> D> I would like to compute the number of
>> D> keys in a hash, as in below, but without
>> D> using a temporary variable.
>>
>> perldoc -f keys.

>
> 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
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      11-19-2004
"DnaDude" <(E-Mail Removed)> wrote in message
news:cnlhai$l4h$(E-Mail Removed)...
> Uri Guttman wrote:
> >>>>>>"D" == DnaDude <(E-Mail Removed)> writes:

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

 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      11-19-2004
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 = $#{@foo};


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

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

-Joe
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      11-19-2004
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 = $#{@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



 
Reply With Quote
 
Kjetil Skotheim
Guest
Posts: n/a
 
      11-21-2004
On Fri, 19 Nov 2004 14:16:31 -0500, DnaDude <(E-Mail Removed)> wrote:

> 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;

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#keys, Hash#values order question Ronald Fischer Ruby 0 08-23-2007 09:34 AM
hash.keys and hash.values Mage Ruby 14 08-15-2006 08:44 PM
Hash#values and Hash#keys order Alex Fenton Ruby 1 04-15-2006 05:45 AM
No Keys, nor other hash methods on multidimensional hash Xeno Campanoli Ruby 16 08-25-2005 07:04 AM



Advertisments