Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Accessing Hash of hash of arrays

Reply
Thread Tools

Accessing Hash of hash of arrays

 
 
Lynn
Guest
Posts: n/a
 
      02-14-2005
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:umper;
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 !!!




 
Reply With Quote
 
 
 
 
phaylon
Guest
Posts: n/a
 
      02-14-2005
Lynn wrote:

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


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

Or wrote more readable:

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

hth,phay

--
http://www.dunkelheit.at/

The first rule of project mayhem is: you do not ask questions.
-- Fight Club

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      02-14-2005
"Lynn" <(E-Mail Removed)> wrote:
> 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:umper;
> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      02-14-2005

"Lynn" <(E-Mail Removed)> wrote in message
news:4210d3b2$(E-Mail Removed)...
> 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:umper;
> 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

 
Reply With Quote
 
Lynn
Guest
Posts: n/a
 
      02-14-2005

"phaylon" <(E-Mail Removed)> wrote in message
news(E-Mail Removed).. .
> Lynn wrote:
>
> > ${$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


 
Reply With Quote
 
Lynn
Guest
Posts: n/a
 
      02-14-2005

<(E-Mail Removed)> wrote in message
news:20050214115911.474$(E-Mail Removed)...
> "Lynn" <(E-Mail Removed)> wrote:
> > Hi All
> >
> > I am having problems accessing a data structure created from an Ingres
> > database.

>
> 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


 
Reply With Quote
 
Lynn
Guest
Posts: n/a
 
      02-14-2005

"Paul Lalli" <(E-Mail Removed)> wrote in message
news:JU4Qd.29000$s16.16589@trndny02...
>
> "Lynn" <(E-Mail Removed)> wrote in message
> news:4210d3b2$(E-Mail Removed)...

(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



 
Reply With Quote
 
Eric Bohlman
Guest
Posts: n/a
 
      02-16-2005
"Lynn" <(E-Mail Removed)> wrote in news:4210dff3$(E-Mail Removed):

> 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.
 
Reply With Quote
 
Lynn
Guest
Posts: n/a
 
      02-16-2005
Hi Eric,

"Eric Bohlman" <(E-Mail Removed)> wrote in message
news:Xns95FF753394D88ebohlmanomsdevcom@130.133.1.4 ...
>
> 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


 
Reply With Quote
 
Alan J. Flavell
Guest
Posts: n/a
 
      02-16-2005
On Wed, 16 Feb 2005, Lynn wrote:

> 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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
hash of arrays - appending to one of the arrays Adam Akhtar Ruby 5 03-25-2008 11:53 PM
Difference between accessing arrays and associative arrays using a int index shashi Perl Misc 17 04-13-2006 03:48 PM
error when accessing hash of arrays Harald Perl Misc 8 03-14-2006 05:44 PM



Advertisments