Accessing Hash of hash of arrays

L

Lynn

Hi All

I am having problems accessing a data structure created from an Ingres
database. I have
a hash of hashes where the value is an array. What I am trying to do is
increment the
value of the first entry in the array. I have provided a little test script
that shows my
problem.


#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use diagnostics;


my $people = {
'robert' => {
'TUSHYATI MAUDGALYA' => [
0,
0,
0
],
'JESSICA LEE' => [
0,
0,
0
],
'JOHN HUSTON' => [
0,
0,
0
]
},
'christie' => {
'LARRY KRUGER' => [
0,
0,
0
],
'JEFFREY SIMONSON' => [
0,
0,
0
]
}
};



my $manager = {
'LARRY KRUGER' =>'christie',
'JEFFREY SIMONSON' =>'christie',
'TUSHYATI MAUDGALYA' => 'robert',
'JESSICA LEE' => 'robert',
'JOHN HUSTON' => 'robert'
};

my @tmp = ('LARRY KRUGER', 'JEFFREY SIMONSON', 'TUSHYATI MAUDGALYA',
'JESSICA LEE', 'JOHN HUSTON');


foreach my $person(@tmp) {

${$people->${$manager->{$person} } }{$person}[0]++;
}

print Dumper($people);


Can't use string ("christie") as a SCALAR ref while "strict refs" in use at
G:\hillr\test1.pl line 55 (#1)
(F) Only hard references are allowed by "strict refs". Symbolic
references are disallowed. See perlref.

Uncaught exception from user code:
Can't use string ("christie") as a SCALAR ref while "strict refs" in
use

I looked at the perlref as suggested in the error message and found this:

Anywhere you'd put an identifier (or chain of identifiers) as part
of a variable or subroutine name, you can replace the identifier
with a BLOCK returning a reference of the correct type. In other
words, the previous examples could be written like this:

$bar = ${$scalarref};
push(@{$arrayref}, $filename);
${$arrayref}[0] = "January";
${$hashref}{"KEY"} = "VALUE";
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
&{$coderef}(1,2,3);
$globref->print("output\n"); # iff IO::Handle is loaded

I'm trying that one to access the hash but still come up with the error.
What am I doing wrong?

Thanks !!!
 
P

phaylon

Lynn said:
${$people->${$manager->{$person} } }{$person}[0]++;

$people->{ $manager->{ $person } }->{ $person }->[0]++;

Or wrote more readable:

$people->{
$manager->{ $person }
}->{ $person }->[0]++;

hth,phay
 
X

xhoster

Lynn said:
Hi All

I am having problems accessing a data structure created from an Ingres
database.

Does this really have anything to do with Ingres?
I have
a hash of hashes where the value is an array. What I am trying to do is
increment the
value of the first entry in the array. I have provided a little test
script that shows my
problem.

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use diagnostics;

my $people = {
'robert' => {
'TUSHYATI MAUDGALYA' => [
0,
0,
0
],
'JESSICA LEE' => [
0,
0,
0
],
'JOHN HUSTON' => [
0,
0,
0
]
},
'christie' => {
'LARRY KRUGER' => [
0,
0,
0
],
'JEFFREY SIMONSON' => [
0,
0,
0
]
}
};

my $manager = {
'LARRY KRUGER' =>'christie',
'JEFFREY SIMONSON' =>'christie',
'TUSHYATI MAUDGALYA' => 'robert',
'JESSICA LEE' => 'robert',
'JOHN HUSTON' => 'robert'
};

my @tmp = ('LARRY KRUGER', 'JEFFREY SIMONSON', 'TUSHYATI MAUDGALYA',
'JESSICA LEE', 'JOHN HUSTON');

foreach my $person(@tmp) {

${$people->${$manager->{$person} } }{$person}[0]++;

You seem to be randomly mixing the two forms of dereferencing. You can
do that, but it is generally not a good idea and I don't feel like
convoluting exactly what you actually were doing. I think the root of the
problem is that $people->${...} looks like an invokation of a code ref.

use arrows:

$people->{$manager->{$person}}->{$person}->[0]++;

Or use the other way (which is ugly, so use arrows):

${${${$people}{${manger}{$person}}{$person}}[0]++;

Some of those sets of curlies may be unnecessary.

Xho
 
P

Paul Lalli

Lynn said:
Hi All

I am having problems accessing a data structure created from an Ingres
database. I have
a hash of hashes where the value is an array.

The value is an arrayref. The distinction is important.
What I am trying to do is increment the
value of the first entry in the array. I have provided a little test script
that shows my problem.

Excellent! Thank you for that!!
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use diagnostics;


my $people = {
'robert' => {
'TUSHYATI MAUDGALYA' => [
0,
0,
0
],
'JESSICA LEE' => [
0,
0,
0
],
'JOHN HUSTON' => [
0,
0,
0
]
},
'christie' => {
'LARRY KRUGER' => [
0,
0,
0
],
'JEFFREY SIMONSON' => [
0,
0,
0
]
}
};

The keys of the hash referenced by $people are ('robert', 'christie')
my $manager = {
'LARRY KRUGER' =>'christie',
'JEFFREY SIMONSON' =>'christie',
'TUSHYATI MAUDGALYA' => 'robert',
'JESSICA LEE' => 'robert',
'JOHN HUSTON' => 'robert'
};

The values of the hash referenced by $manager are ('robert', 'christie')
my @tmp = ('LARRY KRUGER', 'JEFFREY SIMONSON', 'TUSHYATI MAUDGALYA',
'JESSICA LEE', 'JOHN HUSTON');


foreach my $person(@tmp) {

${$people->${$manager->{$person} } }{$person}[0]++;

$manager->($person) is the value of %$manager whose key is $person. As
we already said, this value is either 'robert' or 'christie'. It is
that value that should be the key to the %$people hash. Putting the $
before {$manager->{$person}} tries to use the word 'robert' as a
symbolic reference. You want to use the actual string 'robert'. Remove
this $ (the third one in the above statement, reading left to right).

This entire statement can actually be reduced to:
$people->{ $manager->{$person} }{$person}[0]++;


Hope this helps!
Paul Lalli
 
L

Lynn

phaylon said:
Lynn said:
${$people->${$manager->{$person} } }{$person}[0]++;

$people->{ $manager->{ $person } }->{ $person }->[0]++;

Or wrote more readable:

$people->{
$manager->{ $person }
}->{ $person }->[0]++;

hth,phay
Yep!! that works Thanks phay

Lynn
 
L

Lynn

Does this really have anything to do with Ingres?

Not really, Just providing more information that was nessary. I guess next
time I will keep it brief (sorry)
You seem to be randomly mixing the two forms of dereferencing. You can
do that, but it is generally not a good idea and I don't feel like
convoluting exactly what you actually were doing. I think the root of the
problem is that $people->${...} looks like an invokation of a code ref.

use arrows:

$people->{$manager->{$person}}->{$person}->[0]++;

That's it ;-)
Or use the other way (which is ugly, so use arrows):

${${${$people}{${manger}{$person}}{$person}}[0]++;

Some of those sets of curlies may be unnecessary.
Thanks for all of your help :)

Lynn
 
L

Lynn

Paul Lalli said:
(snipped)

$manager->($person) is the value of %$manager whose key is $person. As
we already said, this value is either 'robert' or 'christie'. It is
that value that should be the key to the %$people hash. Putting the $
before {$manager->{$person}} tries to use the word 'robert' as a
symbolic reference. You want to use the actual string 'robert'. Remove
this $ (the third one in the above statement, reading left to right).

Thanks for that explanation. It really helps :)
This entire statement can actually be reduced to:
$people->{ $manager->{$person} }{$person}[0]++;


Hope this helps!
Paul Lalli

Thanks for all of your help :) It's nice to know that people like you spend
your time
helping out new perl programmers. I have found that the regulares here at
CLPM are always very helpful.

Thanks All

Lynn
 
E

Eric Bohlman

Lynn said:
Thanks for all of your help :) It's nice to know that people like you
spend your time
helping out new perl programmers. I have found that the regulares here
at CLPM are always very helpful.

I think it would be very instructive to point out *why* you had such a
good experience while many other Perl newbies seem to perceive this group
as hostile. Your initial post was practically a textbook example of how
to ask a "newbie" question the *right* way.

1) You put the subject of your post in the Subject of your post :)
Anyone just skimming the headers (which, in a high-volume group like
this, is just about everyone) could immediately tell exactly what your
post was about. You described the issue using the fewest possible words
(making every word count) that could adequately describe it, with no
superfluous verbiage like "newbie" or "help."

2) Your first paragraph succinctly described what you were trying to do.
You included a tiny bit of irrelevant information, but you didn't get all
defensive when someone pointed out that it was irrelevant.

3) Then you provided a code example that anyone could cut-and-paste and
run.

3.1) You asked your machine for all the help it could give you (using
strict and warnings).

3.2) The code example was free of typos, suggesting that you cut-and-
pasted it rather than retyping it.

3.3) The code example contained its own data.

4) You described what results you wanted. It's amazing how many posts
leave the reader guessing at that.

5) You included the *exact* error message that you got, not "something
like..."

6) You went to the manual and looked up the problem. You found the
explanation a little beyond you, but that's not a problem. You asked us
to clarify what the manual said rather than asking us to read it to you.

With your permission, I think it would be helpful if the posting
guidelines regularly posted here contained a citation (via the Google
archive or someone mirroring it on their Web site) to your post as an
example of a post that follows the guidelines.
 
L

Lynn

Hi Eric,

Eric Bohlman said:
I think it would be very instructive to point out *why* you had such a
good experience while many other Perl newbies seem to perceive this group
as hostile.

Yes, unfortuntaly many do find this group hostile.
Your initial post was practically a textbook example of how
to ask a "newbie" question the *right* way.

Before I posted my message I was browsing this newsgroup and I was
astounded by the number of responses pointing to the guidelines. So
what I did was to printout the guidelines and follow them. Once I did
this I was very suprised as to how applying the guidelines has helped me
with my script. For example, orginally I was not going to ask a question
about a Hash of hashes of arrays I had a different problem that promped me
to start
a post to this newsgroup (and get the guidelines and use them)
In using the guidelines I was able to fix about 5 or 6 problems without
asking any
questions.

I think one of the misunderstandings new perl programmers have is that
they think the guidelines are strict rules on posting to CLPM when in fact
they are guidelines for helping you (the programmer) fix the actual perl
script.

(snipped)
With your permission, I think it would be helpful if the posting
guidelines regularly posted here contained a citation (via the Google
archive or someone mirroring it on their Web site) to your post as an
example of a post that follows the guidelines.

That's OK with me :)

Thanks

Lynn
 
A

Alan J. Flavell

Before I posted my message I was browsing this newsgroup and I was
astounded by the number of responses pointing to the guidelines. So
what I did was to printout the guidelines and follow them. Once I
did this I was very suprised as to how applying the guidelines has
helped me with my script. For example, orginally I was not going to
ask a question about a Hash of hashes of arrays I had a different
problem that promped me to start a post to this newsgroup (and get
the guidelines and use them) In using the guidelines I was able to
fix about 5 or 6 problems without asking any questions.

That's beautiful.
I think one of the misunderstandings new perl programmers have is
that they think the guidelines are strict rules on posting to CLPM
when in fact they are guidelines for helping you (the programmer)
fix the actual perl script.

Just so. The guidelines are meant to 1. help folks to help
themselves, and 2. when that's not enough, to help anyone who has
appropriate expertise, to get to the root of the problem in an
effective way. When that happens, it makes everybody happy: the
questioner, because they get a solution: the respondent, because they
can see that their solution was understood and was useful; and the
bystanders, because they learnt something from seeing it done.
Usenet perfection.

Thanks for sharing!
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top