baxy77bax:

You might consider simply using a hash. You don't specify any requirements on how the mapping is done, so it's possible you don't actually care about the actual number you're mapping to. If so, then there's little difference between:

my ($idx, @array, $a_big_num, $some_data); # Store the data $idx = map_func($a_big_num); $array[$idx] = $some_data; # Fetch the data $idx = map_func($a_big_num); $some_data = $array[$idx];

and

my (%hash, $a_big_num, $some_data); # Store $hash{$a_big_num} = $some_data; # Fetch $some_data = $hash{$a_big_num};

The only real differences between them pop up if (a) you need to retain the order of insertion/deletion or (b) if you actually need to know the slot number ($idx) somewhere in your code. I'm guessing you don't need either, though, because you have absolutely no stated requirements on your mapping function that indicate it.

In fact, you have *so* *few) requirements specified on your mapping function, that this one would meet all stated requirements:

sub map_func { return 7 }

Before you complain about the fact that there are collisions, you'd have to deal with that in any case because when you map a billion different integers into a thousand, your mapping function will always return collisions unless it's something like:

sub map_func { state %indexes; state $idx; my $a_big_num = shift; return $indexes{$a_big_num} if exists $indexes{$a_big_num}; die "Ran out of slots!" if keys %indexes > 1000; return $indexes{$a_big_num}=$idx++; }

But in this case, we're using a hash anyway, so there's little to be gained over just using the hash directly. (Again, assuming you don't need the aforementioned conditions (a) or (b).)

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Encoding function needed by roboticus
in thread Encoding function needed by baxy77bax

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.