Re: Need Hash Help



raymond@xxxxxxxxxxx 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
.



Relevant Pages

  • Duck Typed Concepts for Ruby (was Re: A use case for an ordered hash)
    ... An Sequencable mixin can be defined that implements all sorts of operations such as append, concat, splice, sort, etc. ... extending an instance of Array with Sorted if the array is known to be sorted. ... Now returning to the thread at hand we can see that the difference between the associative array and hash hierarchies is that the hash hierarchy depends upon Hashable keys. ...
    (comp.lang.ruby)
  • Re: Suggestions for double-hashing scheme
    ... >> The items that are being moved are the items in the hash table itself, ... >> which are of fixed size (they are in an array, ... > typedef struct { ... One "uchar" aka 'unsigned char' is plenty to hold a probe ...
    (comp.programming)
  • [SUMMARY] Index and Query (#54)
    ... This was a fun quiz for me because I really don't know anything about indexing ... We see in initializethat the index is just a Hash. ... an Array of symbolic document names where the word can be found ). ...
    (comp.lang.ruby)
  • Re: Comment on how to uniquely track your objects in C# / hash table / get hash code
    ... Array, correct? ... This is largely irrelevant to the issue of performance, since hash ... where both insertions and lookups happen frequently, ... about fast lookups for balanced red/black binary trees. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: problem with hash & sort array
    ... Then I put the hash into an array with the sort ... The twist is I have to identify the duplicate ... a real data structure, and in the next place I make the exact opposite ...
    (comp.lang.perl.misc)