I've got two strings of numbers, both containing a certain number of digits. Just for simplicity, lets assume that both variables contain four numbers. One variable has digits 1 through 4 in a specified order. The other variable has any digits 0 through 9. What is the best way to arrange the numbers in the second variable in accordance to the first variable? I'm sure that made no sense, so I'll try to make a visual representation using pretty tables :)

$digits = 4; $order = 4213; $rearrangeme = 9876;

4213
9876
Would Lead To..
17
28
36
49

7869 would be the final result.

After trying to acheive this with an elaborate series of split, s///, and substr, I spent about 2 hours trying to debug it, but to no avail. I'm sure theres probably some really simple way of doing this using a hash or something that I am just overlooking.

Thanks a lot for any help you could offer.


In reply to Rearranging Numbers by Anonymous Monk

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.