For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2007 > how to print dual key hash..









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to print dual key hash..
newBee

2007-10-26, 7:59 am

I have the fallowing code segment in my code. I want to print my

foreach $document(@documents){
$valueOfTermD = 0;
$powerOfTermD = 0;
foreach $term(keys %termWeightHash){
if($termWeightHash{$term}{$document}){
$powerOfTermD = $powerOfTermD + $termWeightHash{$term}{$document}*
$termWeightHash{$term}{$document};
}
}
$valueOfTermD = sqrt($powerOfTermD);
$hashTermD{$document} = $valueOfTermD;
}

mAyur

2007-10-27, 7:59 am

On Oct 26, 6:07 am, auduw...@gmail.com (newBee) wrote:
> I have the fallowing code segment in my code. I want to print my
>
> foreach $document(@documents){
> $valueOfTermD = 0;
> $powerOfTermD = 0;
> foreach $term(keys %termWeightHash){
> if($termWeightHash{$term}{$document}){
> $powerOfTermD = $powerOfTermD + $termWeightHash{$term}{$document}*
> $termWeightHash{$term}{$document};
> }
> }
> $valueOfTermD = sqrt($powerOfTermD);
> $hashTermD{$document} = $valueOfTermD;
>
> }


Hey Jeff, thanks for pointing to Data::Dumper, this looks interesting.

By the way, newBee, I have doubt abt ur code is it working? I am just
printing ur code again, with line numbers

1: foreach $document(@documents){
2: $valueOfTermD = 0;
3: $powerOfTermD = 0;
4: foreach $term(keys %termWeightHash){
5: if($termWeightHash{$term}{$document}){
6: $powerOfTermD = $powerOfTermD +
$termWeightHash{$term}{$document}*
$termWeightHash{$term}{$document};
7: }
8: }
9: $valueOfTermD = sqrt($powerOfTermD);
10: $hashTermD{$document} = $valueOfTermD;
11:
12: }

On line 4 u r getting hash reference in $term variable, line 5: u have
a if condition which is checking the $document key of inner hash
(reffered by $term). [Hope i am rt till now?]
so ur condition on line 5 shud be ${$termWeightHash{$term}}
{$document} [u have not dereferenced it properly] ...
same mistake is repeated in line 6, what i feel is that ur line 6 shud
look like this

" $powerOfTermD = $powerOfTermD + ${$termWeightHash{$term}}
{$document}* ${$termWeightHash{$term}}
{$document}; "


let me know if i am rt.

Jenda Krynicky

2007-10-27, 7:59 am

From: mAyur <Mayur.Thakare@gmail.com>
> By the way, newBee, I have doubt abt ur code is it working? I am just
> printing ur code again, with line numbers


Are you trying to be kewl or just can't spell? "abt"? "ur"?

> 1: foreach $document(@documents){
> 2: $valueOfTermD = 0;
> 3: $powerOfTermD = 0;
> 4: foreach $term(keys %termWeightHash){
> 5: if($termWeightHash{$term}{$document}){
> 6: $powerOfTermD = $powerOfTermD +
> $termWeightHash{$term}{$document}*
> $termWeightHash{$term}{$document};
> 7: }
> 8: }
> 9: $valueOfTermD = sqrt($powerOfTermD);
> 10: $hashTermD{$document} = $valueOfTermD;
> 11:
> 12: }
>
> On line 4 u r getting hash reference in $term variable, line 5: u have


On line 4 you are getting a hash reference in the $term variable, on
line 5 you have ...

> a if condition which is checking the $document key of inner hash
> (reffered by $term). [Hope i am rt till now?]


Hope I am right till now.

> so ur condition on line 5 shud be ${$termWeightHash{$term}}


so your condition on line 5 should be

> {$document} [u have not dereferenced it properly] ...
> same mistake is repeated in line 6, what i feel is that ur line 6 shud
> look like this


.... what I feel is that your line 6 should look like this

>
> " $powerOfTermD = $powerOfTermD + ${$termWeightHash{$term}}
> {$document}* ${$termWeightHash{$term}}
> {$document}; "
>
>
> let me know if i am rt.


Let me know if I am right.

There is no "u", "ur", "shud", "rt" in English! I don't mind typoes,
I can understand (and make) stylistic mistakes, I can understand some
spelling mistakes (is it "successfull" or "successful"?), but please
don't use l33t speak.

And by the way you are wrong. First, the $term is a string, a key in
the %termWeightHash hash, not a hash reference.

and second,
${$termWeightHash{$term}}{$document}
is equivalent to
$termWeightHash{$term}->{$document}
is equivalent to
$termWeightHash{$term}{$document}

Thanks, Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com