You can't use strings to index arrays. But you could use a 2D hash instead.

The problem is, a 8000x8000 array is going to consume close to 2GB of ram, and a 2D hash of the same dimensions considerably more.

However, since you are only doing a boolean test, there is no need to actually store any values in the hash. Hashes have a very nice exists test.

So, instead of storing 0 or 1 for each pair, you could make a sparse 2D hash by only creating keys (but assigning no value!) for those that are true. Then your test simple becomes if( exists $hash{ $first }{ $second } ){ ... which if the ratio of true to false is less than ~50%, will save you memory relative to an array(~1.7GB .v. ~2GB)

But, for best speed and minimal memory consumption, whenever I see "0 or 1" I think bit vectors. An array of 8000 bitstrings, each with 8000 bits (1000 bytes) requires just 8MB:

@vecs = ('')x8000; for my $_1 ( 0 .. 7999 ) { vec( $vecs[ $_1 ], $_, 1 ) = (rand() > 0.5 ) for 0 .. 7999 };; print total_size \@vecs;; 8494224

And lookups are very fast:

$t = time; for my $_1 ( 0 .. 7999 ) { vec( $vecs[ $_1 ], $_, 1 ) ? ++$true : ++$false for 0 .. 7999 }; print "$true $false", time() - $t;; 31998302 32001698 15.6879999637604

At 4 milllion lookups/second, it beats hashes and probably arrays too. Definitely, if their size pushes you into swapping.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

In reply to Re: how can i use characters in 2 dimensional array brackects by BrowserUk
in thread how can i use characters in 2 dimensional array brackects by siskos1

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.