I was wandering through some code on the project getting ready to move on: documenting, cleaning up debugging routines, etc.. and ran across a FIXME note.

This is a memory optimization problem, and at the time I wasn't terribly inspired to fix it. I'm still not, but thought this might provide some mental breakfast for someone at PerlMonks. Difficulty level: intermediate.

The code below can serve as a working model -- it does work -- it just needs to be optimized. Enjoy!

#!c:\perl\bin\perl.exe -w # Nothing interesting here, just setup for the example. use strict; { local $/; $_=<DATA>; } my %columns=%{eval $_}; # Story: # The arrayrefs in %columns represent an entire record. # So in the example below "John, Jay, Aurora" in an entire # record. We produce @si, which tells us the order in which # the records should actually be in. This sort # is actually a lot more complicated than this but the # net result is that @si contains a list of "row" numbers # in the proper order. # Don't mess with this, PerlMonks. my @si=sort { $columns{field1}->[$a] cmp $columns{field1}->[$b] } (0..@{$columns{field1}}-1); # The part I'm feeling lazy about is here. I want to # rebuild the structure so that %columns has the # arrayrefs arranged in the proper order. (Bill, Bob, # John, Sue; then Raye, Apple, Jay, Shell; then Clio, # Calumet, Aurora, Elgin) It works now, but isn't terribly # efficient since %columns is enormous in Real Life. # i.e. do this without a temp hash. :) Extra points # for cleverness. Keep it self-contained: no fair # monkeying with the sort above. You have @si and %columns # to play with. my %foo; foreach(@si) { for my $k (keys %columns) { push(@{$foo{$k}}, $columns{$k}->[$_]); } } %columns=%foo; undef %foo; __DATA__ { field1 => [ 'John','Sue','Bill','Bob'], field2 => [ 'Jay','Shell','Raye','Apple'], field3 => [ 'Aurora','Elgin','Clio','Calumet'], }

In reply to Structure slogging: do my homework! :) by clintp

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.