Nobody did mention array slices, so I naturally couldn't resist fixing the omission. In truth, the whole setup feels like a self-study exercise in slicing and permutations.

In realistic program code, once you do have indices at hand (in other words, a mapping), there's usually no need to rearrange the actual data. Indirection can be used instead: $data[ $mapping[$i] ]. Nevertheless, it's been a somewhat fun exercise.

#! /usr/bin/perl use strict; use warnings; my @words = qw(this array is an); my @order = (0, 3, 1, 2); print "words = @words \n"; print "order = @order \n"; # First of all, keep in mind that keys does not only work on hashes: # using it on arrays will yield their list of indices. my @indices = keys @words; # (0 .. 3) print "indices = @indices \n"; # Now, let us introduce the array slice. An array slice # is used to extract any number of array elements. # Here, we use the slice to get at a specific full reordering # (permutation) of the words: my @aslice = @words[ @order ]; print "aslice = @aslice \n"; # Oh, but this did not work! The @order gave word positions in order, # instead of giving the order of positions. # We need to find the inverse mapping of @order. my @inverse; @inverse[@order] = keys @order; # (0, 3, 1, 2) => (0, 1, 2, 3) print "inverse = @inverse \n"; # Just to check out that it checks out: foreach my $i (keys @order) { die if $inverse[ $order[$i] ] != $i; die if $order[ $inverse[$i] ] != $i; } # Now, slicing either @inverse[@order] or @order[@inverse] will yield # the indices (0..3). When used to shuffle the @words, they "undo" # or reverse each others action. my @proper = @words[ @inverse ]; print "proper = @proper \n"; # There we have it, ordered per request. And back again: print "back again = @proper[ @order ] \n"; # Alright. The simplest way to randomly shuffle a list # is to use the List::Util module. use List::Util qw( shuffle ); # To create a new puzzle, we use the shuffle() to shuffle the shuffle. my @tantrum = shuffle keys @words; @words = @words[ @tantrum ]; @order = @order[ @tantrum ]; @inverse[@order] = keys @order; print "new words = @words \n"; print "new order = @order \n"; print "new inverse = @inverse \n"; print "new proper = @words[ @inverse ] \n"; # That's it, thanks for reading.


In reply to Re: keep track of array indexes by oiskuu
in thread keep track of array indexes 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.