Here's the elegant solution I was looking for! The following results:

Original Array: 14 11 10 13 11 New Positions: 4 1 0 3 2 Ordered Array: 10 11 11 13 14 Original Positions: 2 1 4 3 0

Can be obtained from:

#!/usr/bin/env perl ## script to practice creating numeric orderings use strict ; use warnings ; ## we want to find the POSITION of each value before and after orderin +g my @values = ( 14 , 11 , 10 , 13 , 11 ) ; ## now let's do some ordering! my %orderings = order( @values ) ; ## ## print original array and positions after ordering print "\n Original Array: " ; foreach my $value ( @values ) { print sprintf( "% 4d" , $value ) ; } print "\n New Positions: " ; foreach my $ordering ( @{ $orderings{ "new_pos" } } ) { print sprintf( "% 4d" , $ordering ) ; } print "\n" ; ## ## print ordered array and original positions print "\n Ordered Array: " ; foreach my $ordering ( @{ $orderings{ "orig_pos" } } ) { print sprintf( "% 4d" , $values[ $ordering ] ) ; } print "\n Original Positions: " ; foreach my $ordering ( @{ $orderings{ "orig_pos" } } ) { print sprintf( "% 4d" , $ordering ) ; } print "\n\n" ; ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## sub order { my @inarray = @_ ; ## elements of @inarray MUST be numeric foreach my $inval (@inarray) { if ( ! is_float( $inval ) ) { die "Fatal error. All elements of the \"order\" subroutine mus +t be numeric." ; } } ## get the original positions my %pos_orig ; for my $i (0..$#inarray) { push( @{ $pos_orig{ $inarray[$i] }} , $i ) ; } ## put original positions into an array my @orig ; foreach my $key ( sort { $a <=> $b } keys %pos_orig ) { push( @orig , @{ $pos_orig{ $key }} ) ; } ## get the new positions my @sorted = sort { $a <=> $b } @inarray ; my %pos_new ; for my $i (0..$#sorted) { push( @{ $pos_new{ $sorted[$i] }} , $i ) ; } ## put new positions into an array my @new ; foreach my $key ( @inarray ) { push( @new , shift( @{ $pos_new{ $key }} )) ; } ## return the original and positions -- as hash of arrays my %orderings ; @{ $orderings{ "orig_pos" }} = @orig ; @{ $orderings{ "new_pos" }} = @new ; return %orderings ; } sub is_float { my $inval = $_[0] ; defined $inval && $inval =~ /^[+-]?\d+(\.\d+)?$/; }

Originally posted as a Categorized Answer.


In reply to Re: How do I sort into an index? by Soul Singin'
in thread How do I sort into an index? by Monolith-0

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.