-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextra.pl
More file actions
60 lines (52 loc) · 1.14 KB
/
extra.pl
File metadata and controls
60 lines (52 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use strict;
# generates a random shuffle of an array
# from perl cookbook
sub fisher_yates_shuffle {
my $array = shift;
my $i;
for ($i = @$array; --$i;) {
my $j = int rand ($i+1);
next if ($i == $j);
@$array[$i,$j] = @$array[$j,$i];
}
}
sub cmp_ip {
# my ($a, $b) = @_;
my @aElements = ();
if ($a =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/) {
$aElements[0] = $1;
$aElements[1] = $2;
$aElements[2] = $3;
$aElements[3] = $4;
} else {
die ("Invalid IP address passed into cmp_ip: $a");
}
my @bElements = ();
if ($b =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/) {
$bElements[0] = $1;
$bElements[1] = $2;
$bElements[2] = $3;
$bElements[3] = $4;
} else {
die ("Invalid IP address passed into cmp_ip: $b");
}
for (my $i = 0; $i < 4; $i++) {
if ($aElements[$i] > $bElements[$i]) {
return 1;
} elsif ($bElements[$i] > $aElements[$i]) {
return -1;
}
}
return 0;
}
sub substitute {
my ($template, $filler) = @_;
my $tmpl = $template;
# print "$filler \n";
foreach my $key (keys %$filler) {
# print "k $key\n";
$tmpl =~ s/$key/$filler->{$key}/g;
}
return $tmpl;
}
1;