Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Rearranging Numbers

by quidity (Pilgrim)
on Nov 23, 2000 at 08:53 UTC ( #43086=note: print w/ replies, xml ) Need Help??


in reply to Rearranging Numbers

This is quite simple:

$order = 4213; $before = 9876; @ordered[split(//, $order)] = split(//, $before); $after = join '', @ordered;

This turns both strings of digits into lists of single digits, then uses the list defining the order to fill an array slice with the number in need of ordering. When this is printed, you get them out in the right order, the first element of @ordered is empty though, but in this case that doesn't matter too much. Remember also that this will break with more than nine digits, or ten if you allow for '0' to be used when ordering (it comes before 1).


Comment on Re: Rearranging Numbers
Download Code
Re: Re: Rearranging Numbers
by a (Friar) on Nov 23, 2000 at 11:22 UTC
    So (w/ a little help from my friends ;-):
    @order = qw(4 2 1 3 0 5); @before = qw(9 8 7 6 5 11); @ordered[@order] = @before; $after = join ', ', @ordered; print join ", ", @order, " order\n"; print join ", ", @before, " before\n"; print "$after\n";
    gives you the above w/ real numbers, er, integers, not just the string/chars. FWIW
    (waiting for dinner roles to bake)

    a

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://43086]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2014-08-02 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (53 votes), past polls