Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Size of list pointed by key, in a hash (http://www.velocityreviews.com/forums/t896824-size-of-list-pointed-by-key-in-a-hash.html)

bravetanveer 03-03-2006 02:20 AM

Size of list pointed by key, in a hash
 
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 ???


John W. Krahn 03-03-2006 02:33 AM

Re: Size of list pointed by key, in a hash
 
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

A. Sinan Unur 03-03-2006 02:38 AM

Re: Size of list pointed by key, in a hash
 
"bravetanveer" <pankajgode@gmail.com> wrote in
news:1141352449.570458.182210@i40g2000cwc.googlegr oups.com:

> 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 <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html


bravetanveer 03-03-2006 03:16 AM

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 03-03-2006 03:16 AM

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.


A. Sinan Unur 03-03-2006 03:20 AM

Re: Size of list pointed by key, in a hash
 
"bravetanveer" <pankajgode@gmail.com> wrote in
news:1141355779.329312.62370@t39g2000cwt.googlegro ups.com:

> 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 <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html



All times are GMT. The time now is 01:25 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.