Need Hash Help

Discussion in 'Perl' started by raymond@new.orleans, Jul 16, 2007.

  1. Guest

    Hi, I'm going out of my mind trying to figure this problem out. To save it,
    I figured I'd ask for some help :)

    I'm trying to create a hash with name/answer pairs.

    I retrieve a person's userid ($name) and their response ($answer) to a
    multiple choice question in a poll.

    I'm trying to store these into a simple hash:

    $responses{ $name } = $answer;

    I haven't used perl in quite some time and I THOUGHT this was the proper way
    to store these hash pairs but it's not working.

    Once I complete getting all the different people's names and answers in one
    question, I sort them by answer then print out a single answer with all the
    names who chose that answer.

    I'd appreciate any help anyone can give me.

    Thanks in advance!
    , Jul 16, 2007
    #1
    1. Advertising

  2. Azazel Guest

    On 2007-07-16, ans <> wrote:
    > Hi, I'm going out of my mind trying to figure this problem out. To save it,
    > I figured I'd ask for some help :)
    >
    > I'm trying to create a hash with name/answer pairs.
    >
    > I retrieve a person's userid ($name) and their response ($answer) to a
    > multiple choice question in a poll.
    >
    > I'm trying to store these into a simple hash:
    >
    > $responses{ $name } = $answer;
    >
    > I haven't used perl in quite some time and I THOUGHT this was the proper way
    > to store these hash pairs but it's not working.
    >
    > Once I complete getting all the different people's names and answers in one
    > question, I sort them by answer then print out a single answer with all the
    > names who chose that answer.
    >
    > I'd appreciate any help anyone can give me.


    In that case you want the keys and values the other way round:

    if (!$responses{$answer})
    {
    $responses{$answer} = [ $name ];
    }
    else
    {
    push @{$responses{$answer}}, $name;
    }

    foreach $answer (sort keys %responses)
    {
    print "$answer: ", join (", ", @{$responses{$answer}}), "\n";
    }
    Azazel, Jul 16, 2007
    #2
    1. Advertising

  3. >>>>> "Azazel" == Azazel <> writes:

    Azazel> In that case you want the keys and values the other way round:

    Azazel> if (!$responses{$answer})
    Azazel> {
    Azazel> $responses{$answer} = [ $name ];
    Azazel> }
    Azazel> else
    Azazel> {
    Azazel> push @{$responses{$answer}}, $name;
    Azazel> }

    Or just:

    push @{$responses{$answer}}, $name;

    since autovivification will Do The Right Thing when $responses{$answer} is
    undef.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

    --
    Posted via a free Usenet account from http://www.teranews.com
    Randal L. Schwartz, Jul 16, 2007
    #3
  4. Guest

    On 16-Jul-2007, Azazel <> wrote:

    > In that case you want the keys and values the other way round:
    >
    > if (!$responses{$answer})
    > {
    > $responses{$answer} = [ $name ];
    > }
    > else
    > {
    > push @{$responses{$answer}}, $name;
    > }
    >
    > foreach $answer (sort keys %responses)
    > {
    > print "$answer: ", join (", ", @{$responses{$answer}}), "\n";
    > }



    Hey, thanks SO MUCH! That worked like a charm!

    Now if you don't mind, I'm trying to figure out why this worked and mine
    didn't.

    It seems the statement: $responses{$answer] = [ $name ] is a hash of arrays
    (very clever!) but why didn't my original attempt work:

    $responses{$name} = $answer;

    After assigning the values I tried to print them out. $name printed out ok
    but the corresponding $answer was a null value.

    Thank you!
    , Jul 17, 2007
    #4
  5. Joe Smith Guest

    ans wrote:

    > I'm trying to create a hash with name/answer pairs.
    >
    > I retrieve a person's userid ($name) and their response ($answer) to a
    > multiple choice question in a poll.
    >
    > I'm trying to store these into a simple hash:
    >
    > $responses{ $name } = $answer;


    If you have the name already and are looking to store or retrieve their
    one-and-only-answer, that is the way to do it. If you are having problems,
    it is somewhere else in your program not in that statement.

    > Once I complete getting all the different people's names and answers in one
    > question, I sort them by answer


    perldoc -q 'by value'

    > then print out a single answer with all the names who chose that answer.


    You say "single answer" which implies a hash keyed by answer, not by name.
    You say "all the names" which implies the hash value will be an array of names.

    So, by your own words, you are looking for a hash of arrays where the hash
    is indexed by $answer and the value is is an anonymous array containing
    multiple $name entries.

    Any further questions should be accompanied by a short but functional program
    showing what you've got so far, and should be posted to the comp.lang.perl.misc
    newsgroup (not comp.lang.perl since this newsgroup is defunct).

    -Joe
    Joe Smith, Jul 17, 2007
    #5
  6. >>>>> "raymond" == raymond <> writes:

    raymond> Now if you don't mind, I'm trying to figure out why this worked and mine
    raymond> didn't.

    STOP POSTING HERE. THIS IS A DEAD GROUP.

    The fact that *your* news server (and a few thousand others) still carry
    it is not justification for posting in a *dead* group.

    The group you want is comp.lang.perl.misc.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

    --
    Posted via a free Usenet account from http://www.teranews.com
    Randal L. Schwartz, Jul 17, 2007
    #6
  7. Guest

    On 17-Jul-2007, Joe Smith <> wrote:

    > and should be posted to the
    > comp.lang.perl.misc
    > newsgroup (not comp.lang.perl since this newsgroup is defunct)


    Ok thanks, I didn't know that. I do now.
    , Jul 18, 2007
    #7
    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. Red Orchid
    Replies:
    3
    Views:
    1,019
  2. Pieter Claassen
    Replies:
    1
    Views:
    1,088
    CBFalconer
    Aug 4, 2004
  3. Bo Peng
    Replies:
    4
    Views:
    772
  4. rp
    Replies:
    1
    Views:
    478
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    581
    David A. Black
    Jul 2, 2008
Loading...

Share This Page