[ create a new paste ] login | about

Link: http://codepad.org/4NkQSQ4x    [ raw code | fork ]

Perl, pasted on Jul 28:
#!/usr/bin/perl

use strict;
use warnings;

use Benchmark;

use constant RGBINDEX => qw/RGB GRB null GBR RBG null BRG BGR/;

my @rgbindex = qw/RGB GRB null GBR RBG null BRG BGR/;

my $values = "70,20,50";
my %subs = (
	hk => sub {
		my %h;
		@h{qw/R G B/} = split ",", $values;
		my $result = join "", sort { $h{$b} <=> $h{$a} } keys %h;
	},
	hv => sub {
		my @values = split(/,/,$values);
		my %h;
		@h{@values[0 .. 2]} = qw(R G B);
		my $result = join "", map { $h{$_} } sort { $b <=> $a } @values;
	},
	arr => sub {
		my @color = split /,/, $values;
		my @order = sort { $color[$b] <=> $color[$a] } 0..2;
		my $result = join "", qw(R G B)[@order];
	},
	if => sub {
		my ($r, $g, $b) = split /,/, $values;
		$rgbindex[
			(($r < $g) ? 1 : 0) +
			(($r < $b) ? 2 : 0) +
			(($g < $b) ? 4 : 0)
		]
	},
	"*" => sub {
		my ($r, $g, $b) = split /,/, $values;
		$rgbindex[ ($r < $g) + 2*($r < $b) + 4*($g < $b) ];
	},
	"if const" => sub {
		my ($r, $g, $b) = split /,/, $values;
		(RGBINDEX)[ ($r < $g) + 2*($r < $b) + 4*($g < $b) ];
	},
	"* const" => sub {
		my ($r, $g, $b) = split /,/, $values;
		(RGBINDEX)[ ($r < $g) + 2*($r < $b) + 4*($g < $b) ];
	},
);

print "$_: ", $subs{$_}(), "\n" for keys %subs;

Benchmark::cmpthese -1, \%subs;


Create a new paste based on this one


Comments: