# perlancar

Recent Actions from perlancar

• Aristotle commented on A small puzzle for you
``````my ( \$i, %index ) = 0;
\$index{ \$_ } //= \$i++ for @a, @b, @c;
for ( \( @a, @b, @c ) ) {
my @rearranged;
@rearranged[ @index{ @\$_ } ] = @\$_;
\$#rearranged = keys %index;
@\$_ = @\$rearranged;
}``````

Untested. Downside is the double copy.

• Aristotle commented on A small puzzle for you

I believe it takes linear time in the length of the inputs, while they take order (n log n).

That would be nice if this were C, but it’s Perl. So the algorithm may be linear, but due to huge amounts of op dispatch, sub call overhead etc, its implementation in Perl likely has a humongous constant factor, which a big-O analysis hides. In order to actually see it outrun the algorithms that are worse in big-O terms, you’ll probably have to feed it inputs on the order of six digits, maybe even seven or more.

Optimising Perl code usually means finding a…

• tlrrd commented on A small puzzle for you
```use warnings;
use List::MoreUtils qw(uniq);

my @in = (
[ 'b', 'c', 'f' ],
[ 'a', 'd'      ],
[ 'c', 'd', 'e' ],
);
my \$i =0;
my %indexes = map {\$_ => \$i++} sort uniq map @\$_, @in;

foreach \$r (@in) {
my @new;
@new[\$i-1,@indexes{@\$r}] = (undef,@\$r);
\$r = \@new
}

use Data::Dumper;
print Dumper \@in;
```

Not sure if it had to be in place or sorted, but I believe this works.

With apologies to Moritz, whose framework I stole. I would have used Aristotle's uniq replacement, but \$work perl is only 5.10.1

• pru-mike commented on A small puzzle for you

Yet another solution http://pastebin.com/7HSB8YTW

• Leon Timmermans commented on Day 6: Think globally, act localizably (File::umask, Locale::Tie, Unix::setuid)
Thanks for your input. I'm not sure what you meant by "saved IDs".

You may want to read Setuid Demystified first before writing any kind of setuid wrapper.