I don't know what you mean by "the mixture", and I don't understand how the "scenario" with "Bill" vs. "William" is different from what was handled in the earlier replies by toolic and me.

If "B,C,D,E" are all nicknames for "A", and "A,F,G,H" are all nicknames for "B", and if the purpose of this data structure is to provide a one-shot lookup for a given string (i.e. to get the "immediate nickname set" for that string), then okay, that structure makes sense. (Well, sort of, I guess... but are you saying you have cases where A is a nickname for B and B is also a nickname for A? I'm having trouble with that.)

But if the purpose is to pursue all possible "respelling" relations in a set (e.g. "A" can be respelled as any of "B,C,D,E", and for each of those, use the same structure to find all possible respellings), then you have a problem of circularity: A can be respelled as B, which can be respelled as A, which can be respelled as B, which... (infinite loop).

Actually, it's not at all clear now what you are really trying to do, so I'm not sure what advice to give about the data structure. There are two basic directions that seem to be at issue:

  1. tracking many-to-one relations: for each member in a set of N "nicknames", relate it to a specific member in a set of M "real" names, where M < N, and two or more nicknames can refer to the same real name; this involves a simple hash where each hash key points to exactly one value, but the hash values can be non-unique.

  2. tracking one-to-many relations: for each member in a set of M "real" names, list the set of one or more "nicknames" that are synonymous; this involves a hash of arrays, and if this structure is derived from the many-to-one set described above, it cannot be the case that a given array value shows up under more than one hash key (because each "nickname" relates to only one "real name").

If the structure you are looking for is not one of those two, then you need to be more clear about what kind of structure you are looking for and how you want it to organize things. You seem to be giving us simplified fake examples, and maybe they are too simple or maybe they don't accurately reflect your data or your task. What are you really trying to do?

UPDATE: There is a third direction you might be thinking about: many-to-many relations, e.g. a nickname like "Chas" might relate to both "Charles" and "Chastity", but "Chuck" is also a nickname for "Charles". This is another hash of arrays, where some array values (the "real" names in this case) can occur with two or more hash keys. In any case, the important thing is that the hash keys are one set of entities (e.g. nicknames), and the hash values, whether scalars or arrays, are a distinct set of entities (e.g. real names). Just don't get them confused.


In reply to Re^3: Help on format or better way to do..? by graff
in thread Help on format or better way to do..? by learnperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.