Skip to content

Instantly share code, notes, and snippets.

@showaltb
Created October 26, 2016 21:58
Show Gist options
  • Save showaltb/ab167f2d0d58027b89fca7351bd4df1f to your computer and use it in GitHub Desktop.
Save showaltb/ab167f2d0d58027b89fca7351bd4df1f to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
#
# A solution to http://blogs.perl.org/users/ovid/2014/12/a-small-puzzle-for-you.html
use Modern::Perl;
use Data::Printer multiline => 0;
my @a = ( 'b', 'c', 'f' );
my @b = ( 'a', 'd' );
my @c = ( 'c', 'd', 'e' );
# build a map of which values appear in each array
my %d;
$d{$a[$_]}{a} = $a[$_] for 0..$#a;
$d{$b[$_]}{b} = $b[$_] for 0..$#b;
$d{$c[$_]}{c} = $c[$_] for 0..$#c;
# populate (overwrite) the arrays with all the values from the original array,
# but in their proper position.
my $n = 0;
for (sort keys %d) {
$a[$n] = $d{$_}{a};
$b[$n] = $d{$_}{b};
$c[$n] = $d{$_}{c};
$n++;
}
p @a;
p @b;
p @c;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment