Monks,

I have a somewhat complex data structure that I want to convert to another somewhat complex data structure. I have already figure out how to do it, in the long non-Perl unlazy way. However, I think there must be some faster way that escapes me. It might be using map, which I still don't really understand (hence I didn't use it here). So without further ado, here goes...the data structures:

# Current data structure $ADDRESS{Last1,First1}{Account} = ""; # Blank $ADDRESS{Last1,First1}{First} = "First1"; $ADDRESS{Last1,First1}{Last} = "Last1"; $ADDRESS{Last1,First1}{Address} = "123 Home St"; $ADDRESS{Last1,First1}{Phone} = "(222) 333-4444"; $ADDRESS{Last1,First1}{Email} = "flast1@this.com"; $ADDRESS{Last1,First1}{Description} = "This\nThat\n"; $ADDRESS{Last2,First2}{First} = "First2"; ... $ADDRESS{Last999,First999}{Account} = ""; # Blank $ADDRESS{Last999,First999}{First} = "First999"; # Desired data structure $ADDRESS[0][0] = ""; # Blank $ADDRESS[0][1] = "First1"; $ADDRESS[0][2] = "Last1"; $ADDRESS[0][3] = "123 Home St"; $ADDRESS[0][4] = "(222) 333-4444"; $ADDRESS[0][5] = "flast1@this.com"; $ADDRESS[0][6] = "This\nThat\n"; $ADDRESS[1][0] = ""; # Blank $ADDRESS[1][1] = "First2"; ... $ADDRESS[998][0] = ""; # Blank $ADDRESS[998][1] = "First999"; ...

My current code maps everything the long way:

my $count = 0; foreach my $name (keys %ADDRESS) { $row[$count][0] = ""; $row[$count][1] = $ADDRESS{$name}{First}; $row[$count][2] = $ADDRESS{$name}{Last}; $row[$count][3] = $ADDRESS{$name}{Address}; $row[$count][4] = $ADDRESS{$name}{Phone}; $row[$count][5] = $ADDRESS{$name}{Email}; $row[$count][6] = $ADDRESS{$name}{Description}; $count++; }

There must be a prettier/easier/more Perlish way of doing this. The ultimate goal is to get it to be an AoA because I have an existing script that enters data into an SQL database using the AoA structure used above. Aside from using ORM in the future, any thoughts?


In reply to Converting a HoH to an AoA by madbombX

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.