Size of list pointed by key, in a hash

B

bravetanveer

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

John W. Krahn

bravetanveer said:
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
 
A

A. Sinan Unur

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
 
B

bravetanveer

Thanks John an Sinan.

Good to learn lot from you people. Next time , i will make sure, i
don't make those formatting mistakes.
 
B

bravetanveer

Thanks John an Sinan.

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

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top