Reg Hash of Hash

  • Thread starter Shashank Khanvilkar
  • Start date
S

Shashank Khanvilkar

Hi,
I have a program as shown below, which should print out all keys which
have a degree of one. For example the below program should print
(e, c) = 10
since "e" has a a degree of one. But it does not work//

$Hoh{"e"}{"c"} = 10;
$Hoh{"c"}{"f"} = 1;
$Hoh{"c"}{"d"} = 1;

foreach $n1 (keys %Hoh) {
my $deg = keys(%{$Hoh{$n1}});
# print "deg[$n1] = $deg\n";

if ($deg eq 1){
$n2 = %{$aa{$n1}};
print "($n1, $n2) = $aa{$n1}{$n2}\n";
}
}


However if i use the below program it works.
$Hoh{"e"}{"c"} = 10;
$Hoh{"c"}{"f"} = 1;
$Hoh{"c"}{"d"} = 1;

foreach $n1 (keys %Hoh) {
my $deg = keys(%{$Hoh{$n1}});
# print "deg[$n1] = $deg\n";

if ($deg eq 1){
foreach $n2 (sort keys %{$Hoh{$n1}}) {
print "($n1, $n2) = $aa{$n1}{$n2}\n";
}
}
}


I will appreciate if someone can correct my first program. This way, I
will avoid using an extra for loop.

Thanks
Shashank
 
P

Paul Lalli

Shashank Khanvilkar said:
I have a program as shown below, which should print out all keys which
have a degree of one.

What do you mean by "degree"?
For example the below program should print
(e, c) = 10
since "e" has a a degree of one. But it does not work//


"does not work" is a red flag. What - exactly - should happen, what -
exactly - *does* happen, and how do the two differ?
$Hoh{"e"}{"c"} = 10;
$Hoh{"c"}{"f"} = 1;
$Hoh{"c"}{"d"} = 1;

foreach $n1 (keys %Hoh) {
my $deg = keys(%{$Hoh{$n1}});
# print "deg[$n1] = $deg\n";

if ($deg eq 1){

You almost certainly want == instead of eq
If you don't know why, read:
perldoc perlop
$n2 = %{$aa{$n1}};

What exactly do you think this is doing? I'm about 98% sure it's not
doing what you think it is.
print "($n1, $n2) = $aa{$n1}{$n2}\n";
}
}


However if i use the below program it works.

You have an odd definition of "works":

Name "main::aa" used only once: possible typo at - line 11.
Use of uninitialized value in concatenation (.) or string at - line
11.
(e, c) =
$Hoh{"e"}{"c"} = 10;
$Hoh{"c"}{"f"} = 1;
$Hoh{"c"}{"d"} = 1;

foreach $n1 (keys %Hoh) {
my $deg = keys(%{$Hoh{$n1}});
# print "deg[$n1] = $deg\n";

if ($deg eq 1){
foreach $n2 (sort keys %{$Hoh{$n1}}) {
print "($n1, $n2) = $aa{$n1}{$n2}\n";
}
}
}
I will appreciate if someone can correct my first program. This way, I
will avoid using an extra for loop.

Correct the ambiguity in your stated goal.
Correct your description of the problem statement.
Ask perl for help, by using strict and warnings.
Figure out what you think each line of your code is doing.

Once you've fixed those four problems, feel free to ask for more help.

Paul Lalli
 
B

Bill Smith

Shashank Khanvilkar said:
Hi,
I have a program as shown below, which should print out all keys which
have a degree of one. For example the below program should print
(e, c) = 10
since "e" has a a degree of one. But it does not work//

$Hoh{"e"}{"c"} = 10;
$Hoh{"c"}{"f"} = 1;
$Hoh{"c"}{"d"} = 1;

foreach $n1 (keys %Hoh) {
my $deg = keys(%{$Hoh{$n1}});
# print "deg[$n1] = $deg\n";

if ($deg eq 1){
$n2 = %{$aa{$n1}};



$n2 = (keys %{$aa{$n1}})[0];


print "($n1, $n2) = $aa{$n1}{$n2}\n";
}
}


However if i use the below program it works.
$Hoh{"e"}{"c"} = 10;
$Hoh{"c"}{"f"} = 1;
$Hoh{"c"}{"d"} = 1;

foreach $n1 (keys %Hoh) {
my $deg = keys(%{$Hoh{$n1}});
# print "deg[$n1] = $deg\n";

if ($deg eq 1){
foreach $n2 (sort keys %{$Hoh{$n1}}) {
print "($n1, $n2) = $aa{$n1}{$n2}\n";
}
}
}


I am not sure what the value of $n2 is in you first case. You have
assigned a hash to a scalar. In the second case, the value is a key of
that hash. My fix selects the first key of the hash. (if %aa has the
same structure as %Hoh, it is the only key.)

Bill
 
B

Brian McCauley

Bill said:
Shashank Khanvilkar said:
$n2 = %{$aa{$n1}};

$n2 = (keys %{$aa{$n1}})[0];

Or

($n2) = %{$aa{$n1}};

Or more likely

my ($n2) = %{$aa{$n1}};

Remember: you should always declare all varibles as lexically scoped in
the smallest applicable scope unless you have a positive reason to do
otherwise. You should aquire this habit sooner rather than later - the
later you leave it the more painfull it will be. Some people never
aquire this habit - they mutate into bitter and twisted trolls.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top