-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathch-2.pl
executable file
·78 lines (62 loc) · 1.85 KB
/
ch-2.pl
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/perl
use strict;
use warnings;
use English;
################################################################################
# Begin main execution
################################################################################
my @lists = (
{
list => [ 1, 4, 2, 3, 5 ],
value => 3
},
{
list => [ 9, 0, 6, 2, 3, 8, 5 ],
value => 4
}
);
print("\n");
for my $i (0 .. $#lists){
printf(
"Input: \@n = (%s) and \$i = %d\n",
join(",", @{$lists[$i]->{list}}),
$lists[$i]->{value}
);
trim_list_by_value(
$lists[$i]->{list},
$lists[$i]->{value}
);
printf(
"Output: (%s)\n\n",
join(",", @{$lists[$i]->{list}})
);
}
exit(0);
################################################################################
# End main execution; subroutines follow
################################################################################
################################################################################
# Remove from a list of numbers those that are less than or equal to a
# specified integer
# Takes two arguments:
# * A ref to the list to trim; THIS LIST WILL BE MODIFIED as specified above
# * The integer to compare with members of the list for removal purposes
# Returns no meaningful value
################################################################################
sub trim_list_by_value{
my $list = shift();
my $n = int(shift());
my $i = 0;
# Loop while $i is within the array
while($i <= $#$list){
if($list->[$i] > $n){
# This value doesn't meet search criteria;
# move on (increment $i)
$i++;
} else{
# This value meets search criteria; remove
# it (and shrink the list)
splice(@{$list}, $i, 1);
}
}
}