ok, I'm at my work place so cannot submit the code right now but I'll post my algorithm at least because my boss is not around and this post got me ticking... ;).
I'll upload my rendition of the code once I get home.

A Sparse Matrix

A matrix populated mostly with 0s (or null for objects) is called a sparse matrix. Refer this. Keeping this in mind and re-reading your question, I can represent your data as a sparse matrix (critical analysis of this assumption is welcome).

To parse a sparse matrix, my algorithm is as follows:

# ALGORITHM: parseSparse (M) // M is the given matrix //first, we "incrementally" parse the matrix //this enables us to capture only non-zero entries //of each row. set sum = 0; foreach $row{ foreach $col{ if M($row, $col) != 0 $key = concatenate ($row, $col); $hash{$key} = M($row, $col); } } //now that my hash is ready, I can do random access faster /* implement what you want to implement, I will just sum all the n +on zero elements of M */ foreach $key (keys %hash) { sum += $hash{$key}; }

The algorithm essentially creates a 3-tuple (row, column, value) and using a hash makes random access faster as compared to arrays. The wiki link above elucidates more algorithms to represent a sparse matrix.


In reply to Re: Table shuffling challenge by code-ninja
in thread Table shuffling challenge by glow_gene

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.