Size of list pointed by key, in a hash

Discussion in 'Perl Misc' started by bravetanveer, Mar 3, 2006.

  1. bravetanveer

    bravetanveer Guest

    Hi friends,

    I am facing a problem with hashes::

    My hash is declared as below::


    %HashTestSuites_TestCases = (

    "ts_Apu" => [ "tc_Apu2LevelForNegIndexTernary",
    "tc_Apu2LevelForsobelEDBenchmark_16_16",
    tc_Apu2LevelForsobelEDBenchmark_88", "tc_ApunoNest",
    "tc_ApuUgdOnceReservZeroReserv"],

    "ts_DB_LoopFuse" => ["tc_DL_1LevelForDwt",
    "tc_DL_2LevelFor2FilesMacro", "tc_DL_2LevelForNegIndexTernary",
    "tc_DL_2LevelForSobelBenchMark", "tc_DL_2LevelForStructIntArray",
    "tc_DL_2LevelForStructShortArray", "tc_DL_4LevelForNoHB_LaplaceED",
    "tc_DL_forInSwitchCase", "tc_DL_noNest", "tc_DL_ugdMultReuse"]

    );

    foreach $testSuite ( keys %HashTestSuites_TestCases )
    {

    my $temp = scalar ( $HashTestSuites_TestCases{$testSuite} );

    print "Size is $temp ";
    print "Size is $#HashTestSuites_TestCases{$testSuite} ";

    }


    both of the print statements do not give me size. As far as i know
    these are the only ways to get size of array .

    Why am i not getting the size printed of the list to which teh key is
    pointing to ???
     
    bravetanveer, Mar 3, 2006
    #1
    1. Advertising

  2. bravetanveer wrote:
    >
    > I am facing a problem with hashes::
    >
    > My hash is declared as below::
    >
    >
    > %HashTestSuites_TestCases = (
    >
    > "ts_Apu" => [ "tc_Apu2LevelForNegIndexTernary",
    > "tc_Apu2LevelForsobelEDBenchmark_16_16",
    > tc_Apu2LevelForsobelEDBenchmark_88", "tc_ApunoNest",
    > "tc_ApuUgdOnceReservZeroReserv"],
    >
    > "ts_DB_LoopFuse" => ["tc_DL_1LevelForDwt",
    > "tc_DL_2LevelFor2FilesMacro", "tc_DL_2LevelForNegIndexTernary",
    > "tc_DL_2LevelForSobelBenchMark", "tc_DL_2LevelForStructIntArray",
    > "tc_DL_2LevelForStructShortArray", "tc_DL_4LevelForNoHB_LaplaceED",
    > "tc_DL_forInSwitchCase", "tc_DL_noNest", "tc_DL_ugdMultReuse"]
    >
    > );
    >
    > foreach $testSuite ( keys %HashTestSuites_TestCases )
    > {
    >
    > my $temp = scalar ( $HashTestSuites_TestCases{$testSuite} );


    You have to dereference the array to get its size:

    my $temp = @{ $HashTestSuites_TestCases{$testSuite} }


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Mar 3, 2006
    #2
    1. Advertising

  3. "bravetanveer" <> wrote in
    news::

    > My hash is declared as below::
    >
    >
    > %HashTestSuites_TestCases = (
    >
    > "ts_Apu" => [ "tc_Apu2LevelForNegIndexTernary",
    > "tc_Apu2LevelForsobelEDBenchmark_16_16",
    > tc_Apu2LevelForsobelEDBenchmark_88", "tc_ApunoNest",
    > "tc_ApuUgdOnceReservZeroReserv"],
    >
    > "ts_DB_LoopFuse" => ["tc_DL_1LevelForDwt",
    > "tc_DL_2LevelFor2FilesMacro", "tc_DL_2LevelForNegIndexTernary",
    > "tc_DL_2LevelForSobelBenchMark", "tc_DL_2LevelForStructIntArray",
    > "tc_DL_2LevelForStructShortArray", "tc_DL_4LevelForNoHB_LaplaceED",
    > "tc_DL_forInSwitchCase", "tc_DL_noNest", "tc_DL_ugdMultReuse"]
    >
    > );


    Please format your code in a way that makes it easier for others to read
    (you would benefit from this as well). By the way, have you read the
    posting guidelines for this group?

    You should not prefix the name of you variable with the word 'Hash'.
    After all, that is why we use %: to denote that a variable is a hash
    table.

    use strict;
    use warnings;

    my %Suites = (
    ts_Apu => [ qw(
    tc_Apu2LevelForNegIndexTernary
    tc_Apu2LevelForsobelEDBenchmark_16_16
    tc_Apu2LevelForsobelEDBenchmark_88
    tc_ApunoNest
    tc_ApuUgdOnceReservZeroReserv
    )],
    ts_DB_LoopFuse => [ qw(
    tc_DL_1LevelForDwt
    tc_DL_2LevelFor2FilesMacro
    tc_DL_2LevelForNegIndexTernary
    tc_DL_2LevelForSobelBenchMark
    tc_DL_2LevelForStructIntArray
    tc_DL_2LevelForStructShortArray
    tc_DL_4LevelForNoHB_LaplaceED
    tc_DL_forInSwitchCase
    tc_DL_noNest
    tc_DL_ugdMultReuse
    )]
    );

    > foreach $testSuite ( keys %HashTestSuites_TestCases )


    for my $suite ( keys %Suites )

    > {
    >
    > my $temp = scalar ( $HashTestSuites_TestCases{$testSuite} );
    >
    > print "Size is $temp ";
    > print "Size is $#HashTestSuites_TestCases{$testSuite} ";
    >
    > }
    >
    >
    > both of the print statements do not give me size.


    Of course not. There is a simple reason for that: You can't make stuff
    up and expect it to work.


    > As far as i know these are the only ways to get size of array .


    But you never dereferenced those array references!

    for my $a_suite ( keys %Suites ) {
    print "Suite $a_suite test cases: ",
    scalar @{ $Suites{$a_suite} }, "\n";
    }

    See perldoc perlreftut.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Mar 3, 2006
    #3
  4. bravetanveer

    bravetanveer Guest

    Re: Size of list pointed by key, in a hash

    Thanks John an Sinan.

    Good to learn lot from you people. Next time , i will make sure, i
    don't make those formatting mistakes.
     
    bravetanveer, Mar 3, 2006
    #4
  5. bravetanveer

    bravetanveer Guest

    Re: Size of list pointed by key, in a hash

    Thanks John an Sinan.

    Good to learn lot from you people. Next time , i will make sure, i
    don't make those formatting mistakes.
     
    bravetanveer, Mar 3, 2006
    #5
  6. Re: Size of list pointed by key, in a hash

    "bravetanveer" <> wrote in
    news::

    > Thanks John an Sinan.
    >
    > Good to learn lot from you people. Next time , i will make sure, i
    > don't make those formatting mistakes.


    You are welcome. I will ask that you please read the posting guidelines
    for this group as well as

    http://www.safalra.com/special/googlegroupsreply/

    and

    http://cfaj.freeshell.org/google/

    for future reference.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Mar 3, 2006
    #6
    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. Frodo Baggins
    Replies:
    12
    Views:
    666
    dbtid
    Jan 3, 2007
  2. Winsk

    Size of memory Pointed-to

    Winsk, Oct 7, 2007, in forum: C Programming
    Replies:
    4
    Views:
    396
    Keith Thompson
    Oct 8, 2007
  3. Paul
    Replies:
    6
    Views:
    862
    Remo D.
    Nov 29, 2007
  4. rp
    Replies:
    1
    Views:
    589
    red floyd
    Nov 10, 2011
  5. Une bévue
    Replies:
    5
    Views:
    176
    Une bévue
    Aug 10, 2006
Loading...

Share This Page