473,320 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Need Hash Help

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 '07 #1
6 2328
On 2007-07-16, ra*****@new.orleans <ra*****@new.orleanswrote:
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";
}
Jul 16 '07 #2

On 16-Jul-2007, Azazel <az****@azazel.netwrote:
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 16 '07 #3
ra*****@new.orleans 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
Jul 17 '07 #4
>>>>"Azazel" == Azazel <az****@azazel.netwrites:

AzazelIn 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
<me****@stonehenge.com<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

Jul 17 '07 #5
>>>>"raymond" == raymond <ra*****@new.orleanswrites:

raymondNow if you don't mind, I'm trying to figure out why this worked and mine
raymonddidn'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
<me****@stonehenge.com<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

Jul 17 '07 #6

On 17-Jul-2007, Joe Smith <jo*@inwap.comwrote:
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 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
2
by: bryan | last post by:
I have a situation that's pretty delicate that I need some help on. I've been stumped for awhile I just need some advice on the best possible solution. The problem: I have a site I'm making...
0
by: Miguel Dias Moura | last post by:
Hello, I am working on an Asp.Net 2.0 / SQL 2005 web site. I am using profile to save the users info on the database. For example, I have the following structure: Public Structure Name...
1
by: Anthony Esochaghi | last post by:
I need some constructive input to be able to finish my program. I have a field called (1) DateRec, another (2) Municipality and another (3) Type. These three fields invariably will...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
2
by: lovesehuang | last post by:
need some help for source codes about Keep Data Secret Management System baseed on C/S Architecture.The other thnics we need is SQL,C++,VC++,can u tell me where can i get them or download them.it's...
0
by: alaa123 | last post by:
please i really need help to make a start I need to write an XSLT script that takes a file as its input and produces bargraph as its output the file (input) svg:svg...
0
by: [Cool staff!||Hi! I think this need for || Help me | last post by:
http://con-cern.org/files/2007/5/xenical-21024312.html cheap xenical http://con-cern.org/files/2007/5/auto-21024411.html auto loan refinance ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.