the concept of the hash I am trying to have is,
A => B, C, D, E B => A, C, D, E C => A, B, D, E D => A, B, C, E E => A, B, C, D

It strikes me as a bit wasteful to have so many copies of all the names. And it would be worthwhile to figure out an easy way to initialize the complete structure from the simplest possible listing of name sets. Here's what I would propose:

#!/usr/bin/perl use strict; use warnings; # start with an array of arrays (list of lists, which can come from a +data file): # each row contains all the "synonymous" names in a set, and # each element in the row becomes a hash key whose value is # a reference to the whole row/set of synonymous names: my %altnames; # will be a HoA while (<DATA>) { my @nameset = split; for my $name ( @nameset ) { if ( ! exists( $altnames{$name} )) { $altnames{$name} = \@nameset; } else { my $newname = $name; $newname .= " " while ( exists( $altnames{$newname} )); $altnames{$newname} = \@nameset; } } } for my $name ( qw/Allen Bill Chuck Dave Edward Jan/ ) { my $modifier = ''; for ( grep /^$name\s*$/, keys %altnames ) { print "The name $_ is $modifier a member of the set @{$altname +s{$_}}\n"; $modifier = 'also'; } } __DATA__ Allen Al Charles Chuck Chas David Dave Edward Eddie Ed Janet Jan Janice Jan William Will Willie Bill Billie
That plays a little game with the name strings to make sure that you can keep track of different name sets containing the same nickname: just add spaces at the end of a previously seen nickname until it becomes a unique key in the altnames hash. Then when searching the hash, make sure to look for the target name optionally followed by spaces. (If the results are going to a web page, the extra spaces won't affect the display.)

Note that by storing multiple references to the same array in the hash, you are not using extra memory to store copies of the names -- each nameset is stored exactly once.


In reply to Re^5: 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.