Accessing Hash of hash of arrays

Discussion in 'Perl Misc' started by Lynn, Feb 14, 2005.

  1. Lynn

    Lynn Guest

    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 !!!
     
    Lynn, Feb 14, 2005
    #1
    1. Advertising

  2. Lynn

    phaylon Guest

    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
     
    phaylon, Feb 14, 2005
    #2
    1. Advertising

  3. Lynn

    Guest

    "Lynn" <> 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::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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Feb 14, 2005
    #3
  4. Lynn

    Paul Lalli Guest

    "Lynn" <> wrote in message
    news:4210d3b2$...
    > 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
     
    Paul Lalli, Feb 14, 2005
    #4
  5. Lynn

    Lynn Guest

    "phaylon" <> wrote in message
    news:p...
    > 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
     
    Lynn, Feb 14, 2005
    #5
  6. Lynn

    Lynn Guest

    <> wrote in message
    news:20050214115911.474$...
    > "Lynn" <> 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
     
    Lynn, Feb 14, 2005
    #6
  7. Lynn

    Lynn Guest

    "Paul Lalli" <> wrote in message
    news:JU4Qd.29000$s16.16589@trndny02...
    >
    > "Lynn" <> wrote in message
    > news:4210d3b2$...

    (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
     
    Lynn, Feb 14, 2005
    #7
  8. Lynn

    Eric Bohlman Guest

    "Lynn" <> wrote in news:4210dff3$:

    > 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.
     
    Eric Bohlman, Feb 16, 2005
    #8
  9. Lynn

    Lynn Guest

    Hi Eric,

    "Eric Bohlman" <> 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
     
    Lynn, Feb 16, 2005
    #9
  10. 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!
     
    Alan J. Flavell, Feb 16, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philipp
    Replies:
    21
    Views:
    1,186
    Philipp
    Jan 20, 2009
  2. rp
    Replies:
    1
    Views:
    587
    red floyd
    Nov 10, 2011
  3. Adam Akhtar
    Replies:
    5
    Views:
    677
    Adam Akhtar
    Mar 25, 2008
  4. Harald

    error when accessing hash of arrays

    Harald, Mar 14, 2006, in forum: Perl Misc
    Replies:
    8
    Views:
    153
    Tad McClellan
    Mar 14, 2006
  5. shashi
    Replies:
    17
    Views:
    486
    Guest
    Apr 13, 2006
Loading...

Share This Page