Perl Christmas Quiz

Torsten Knorr create-soft at freenet.de
Mon Dec 15 19:28:05 GMT 2008


On Mon, 15 Dec 2008, at 10:04:17, "James Laver" <james.laver at gmail.com> wrote:

>Using other languages is really cheating.

 Let them use other languages.
 We can improving it with Perl.
 In addition we are more flexible.

#!/usr/bin/perl -w
#*** compare_int_arrays_v002.pl ***#
#-------------------------------------------------
 use strict;
#-------------------------------------------------
 my (@i_list1, @i_list2);
 push(@i_list1, int(rand(100))) for(1..200);
 push(@i_list2, int(rand(20))) for(1..10);
 push(@i_list1, ('A'..'Z'));
 push(@i_list2, ('A'..'Z'));
 print("value:\tlist 1:\tlist 2:\n");
 print("$_->[0]\t$_->[1]\t$_->[2]\n") for(@{Intersect(\@i_list1, \@i_list2, 1, qr/^\d+$/)});
#-------------------------------------------------
 sub Intersect
 	{
 	my ($rai_1, $rai_2, $sort_mode, $regex_search_after) = @_;
 	return [] unless(ref($rai_1) eq 'ARRAY' && ref($rai_2) eq 'ARRAY');
 	my (%hi_seen, @ai_ret);
# 0 = sort by value
# 1 = sort by count list 1
# 2 = sort by count list 2
 	$sort_mode = 0 unless($sort_mode &&($sort_mode == 1 || $sort_mode == 2));
 	$hi_seen{$_}[0]++ for(@$rai_1);
 	$hi_seen{$_}[1]++ for(@$rai_2);
 	@ai_ret = map [$_, $hi_seen{$_}[0], $hi_seen{$_}[1]],
 	grep $hi_seen{$_}[0] && $hi_seen{$_}[1] && m/$regex_search_after/,
 	keys(%hi_seen);
 	return [sort({ $a->[$sort_mode] <=> $b->[$sort_mode]} @ai_ret)];
 	}
#-------------------------------------------------

 Torsten



More information about the london.pm mailing list