# Reg Hash of Hash

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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 = 10;
\$Hoh = 1;
\$Hoh = 1;

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

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

However if i use the below program it works.
\$Hoh = 10;
\$Hoh = 1;
\$Hoh = 1;

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

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

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

Thanks
Shashank

## Re: Reg Hash of Hash

> 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 = 10;
> \$Hoh = 1;
> \$Hoh = 1;
>
> foreach \$n1 (keys %Hoh) {
>     my \$deg = keys(%});
> #       print "deg[\$n1]  = \$deg\n";
>
>          if (\$deg eq 1){
>              \$n2 = %};

\$n2 = (keys %})[0];

>              print "(\$n1, \$n2) = \$aa\n";
>          }
>      }
>
>
> However if i use the below program it works.
> \$Hoh = 10;
> \$Hoh = 1;
> \$Hoh = 1;
>
> foreach \$n1 (keys %Hoh) {
>     my \$deg = keys(%});
> #       print "deg[\$n1]  = \$deg\n";
>
>          if (\$deg eq 1){
>             foreach \$n2 (sort keys %}) {
>              print "(\$n1, \$n2) = \$aa\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

## Re: Reg Hash of Hash

Bill Smith wrote:

>>             \$n2 = %};
>
>                \$n2 = (keys %})[0];

Or

(\$n2) = %};

Or more likely

my (\$n2) = %};

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.

## Re: Reg Hash of Hash

> 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 = 10;
> \$Hoh = 1;
> \$Hoh = 1;
>
> foreach \$n1 (keys %Hoh) {
>     my \$deg = keys(%});
> #       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 = %};

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\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 = 10;
> \$Hoh = 1;
> \$Hoh = 1;
>
> foreach \$n1 (keys %Hoh) {
>     my \$deg = keys(%});
> #       print "deg[\$n1]  = \$deg\n";
>
>          if (\$deg eq 1){
>             foreach \$n2 (sort keys %}) {
>              print "(\$n1, \$n2) = \$aa\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

>
> Thanks
> Shashank