finding the number of keys in hash

Discussion in 'Perl Misc' started by DnaDude, Nov 19, 2004.

  1. DnaDude

    DnaDude Guest

    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.
    DnaDude, Nov 19, 2004
    #1
    1. Advertising

  2. DnaDude

    Uri Guttman Guest

    >>>>> "D" == DnaDude <> 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
    Uri Guttman, Nov 19, 2004
    #2
    1. Advertising

  3. DnaDude

    DnaDude Guest

    Uri Guttman wrote:
    >>>>>>"D" == DnaDude <> 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.
    DnaDude, Nov 19, 2004
    #3
  4. DnaDude

    Uri Guttman Guest

    >>>>> "D" == DnaDude <> 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
    Uri Guttman, Nov 19, 2004
    #4
  5. DnaDude

    Paul Lalli Guest

    "DnaDude" <> wrote in message
    news:cnlgqh$l4h$...
    > 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
    Paul Lalli, Nov 19, 2004
    #5
  6. DnaDude

    Alan Mead Guest

    On Fri, 19 Nov 2004 14:25:06 -0500, DnaDude wrote:

    > Uri Guttman wrote:
    >>>>>>>"D" == DnaDude <> 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
    Alan Mead, Nov 19, 2004
    #6
  7. DnaDude

    Paul Lalli Guest

    "DnaDude" <> wrote in message
    news:cnlhai$l4h$...
    > Uri Guttman wrote:
    > >>>>>>"D" == DnaDude <> 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
    Paul Lalli, Nov 19, 2004
    #7
  8. DnaDude

    Joe Smith Guest

    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
    Joe Smith, Nov 19, 2004
    #8
  9. 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
    Jürgen Exner, Nov 19, 2004
    #9
  10. On Fri, 19 Nov 2004 14:16:31 -0500, 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
    > @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;
    Kjetil Skotheim, Nov 21, 2004
    #10
  11. 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
    Jürgen Exner, Nov 21, 2004
    #11
  12. DnaDude

    gbh Guest

    In article <cnlgqh$l4h$>,
    says...
    > 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.
    --
    gbh
    gbh04 is a spamtrap
    all post is deleted
    gbh, Nov 22, 2004
    #12
  13. DnaDude

    Paul Lalli Guest

    "Jürgen Exner" <> wrote in message
    news:nEVnd.517$Rs2.132@trnddc03...
    > 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.
    Paul Lalli, Nov 22, 2004
    #13
  14. DnaDude

    Anno Siegel Guest

    Alan Mead <> wrote in comp.lang.perl.misc:

    > 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
    Anno Siegel, Nov 23, 2004
    #14
  15. DnaDude

    Uri Guttman Guest

    >>>>> "AS" == Anno Siegel <-berlin.de> writes:

    AS> Alan Mead <> wrote in comp.lang.perl.misc:
    >> Here's what you want, I think.. but why?
    >>
    >> for (my $i=0; $i< scalar keys %$A; $i++) { print "$i\n"; }

    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Nov 23, 2004
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    499
    red floyd
    Nov 10, 2011
  2. Xeno Campanoli
    Replies:
    16
    Views:
    257
    Martin DeMello
    Aug 25, 2005
  3. Alex Fenton

    Hash#values and Hash#keys order

    Alex Fenton, Apr 7, 2006, in forum: Ruby
    Replies:
    1
    Views:
    132
    George Ogata
    Apr 15, 2006
  4. Mage

    hash.keys and hash.values

    Mage, Aug 13, 2006, in forum: Ruby
    Replies:
    14
    Views:
    170
  5. Ronald Fischer

    Hash#keys, Hash#values order question

    Ronald Fischer, Aug 23, 2007, in forum: Ruby
    Replies:
    0
    Views:
    145
    Ronald Fischer
    Aug 23, 2007
Loading...

Share This Page