But it does not do exactly what I need to do.

Yeah, that's what I pointed out in my Update. But as I said there, I think it might be a bad idea to keep the actual data in two places. Even using references, there's the chance that the references get stale and you're pointing to different data. I would strongly suggest picking either the hash, or the array, and keep the data there. Then, in the other one, store nothing but indexes (unique keys) that show where to get the data.

Perhaps an example will make it more obvious. Here I will store the actual data in arrays, but have a lookup table to map hash keys to array indexes:

my %index = (firstname => 0, lastname => 1, address => 2, town => 3, zip => 4); my @array1 = ("Bob", "Smith", "1234 Main St", "Anytown", "20500"); my @array2 = ("Joe", "Blow", "4321 Street Ln", "Notown", "20050"); print "First name (from array1): $array1[ $index{firstname} ]\n"; print "Address (from array2): $array2[ $index{address} ]\n";

Notice that we can fairly easily access the data via key name, but we don't have to worry about making sure two disperate structures stay in sync. The only thing we have to worry about is the keys-to-indexes map getting out of date -- but hopefully that is a rare problem.

Another approach that you might find interesting is to just use an array, and have constants to store the array indexes. This would give the advantage of not quite as verbose a syntax. Here's an example of that:

use constant { FIRSTNAME => 0, LASTNAME => 1, ADDRESS => 2, TOWN => 3, ZIP => 4 }; my @array1 = ("Bob", "Smith", "1234 Main St", "Anytown", "20500"); my @array2 = ("Joe", "Blow", "4321 Street Ln", "Notown", "20050"); print "First name (from array1): $array1[ FIRSTNAME ]\n"; print "Address (from array2): $array2[ ADDRESS ]\n";

In reply to Re: Re: Re: Re: Re: Link a hash and an array by revdiablo
in thread Link a hash and an array by SkipHuffman

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.