Debian Codegraph @

range

raw ◀ ▶
Source reconstructed at patch state 0 of 5 — not the installed (fully patched) source.
5 Debian patches touch this file
1 #!/usr/bin/perl -w
2
3 # range: Print out a range of numbers for use in for loops and such.
4 #
5 # Copyright (C) 2002-2004 Suso Banderas
6
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #
21 # You may contact the author at <[email protected]>.
22
23 #######################
24 # VARIABLES AND SETUP #
25 #######################
26
27 use Getopt::Std;
28 use strict;
29 use vars qw/ %opts $verbose /;
30
31 my ($expression, @output_array, $number);
32
33 getopts('e:n:Np:s:hdV', \%opts);
34
35 my $prefix = "";
36 my $suffix = "";
37
38 if ($opts{'h'}) {
39 &help();
40 exit(0);
41 }
42
43 if ($opts{'d'}) {
44 $verbose = 3;
45 print STDERR "Debug mode\n";
46 } elsif ($opts{'V'}) {
47 $verbose = 2;
48 print STDERR "Verbose mode\n";
49 } elsif ($opts{'q'}) {
50 $verbose = 0; # Nothing except the final answer
51 } else {
52 $verbose = 1; # Normal warnings and such.
53 }
54
55 my $range_expression = shift || die "You must specify a range expression\n";
56
57 # A shortcut for specifying a prefix and suffix.
58 $range_expression =~ m/^([^\/]+)\//;
59 $prefix = $1;
60 $range_expression =~ s/^[^\/]+\//\//;
61 $range_expression =~ m/\/([^\/]+)$/;
62 $suffix = $1;
63 $range_expression =~ s/\/[^\/]+$/\//;
64
65 $range_expression =~ s/^\///;
66 $range_expression =~ s/\/$//;
67 my @range_expressions = split(/,/, $range_expression);
68
69 my $separator = $opts{'n'} || " ";
70 $separator =~ s/\\n/\n/g;
71 $separator = "\n" if ($opts{'N'});
72
73 if ($opts{'p'}) {
74 $prefix = $opts{'p'};
75 }
76 if ($opts{'s'}) {
77 $suffix = $opts{'s'};
78 }
79
80 # Make the array of excluded numbers
81 my @excludes;
82 if ($opts{'e'}) {
83 @excludes = split(/,/, $opts{'e'});
84 }
85
86 ################
87 # MAIN PROGRAM #
88 ################
89
90 my $number_exp = "-?[0-9]*\\.?[0-9]+";
91
92 foreach $expression (@range_expressions) {
93 print "expression: $expression\n" if ($verbose >= 3);
94 if ($expression =~ /^($number_exp)$/) {
95 push @output_array, $expression;
96 } elsif ($expression =~ /^($number_exp)\.\.($number_exp)$/) {
97 print "1: $1\n2: $2\n" if ($verbose >= 3);
98 if ($1 > $2) {
99 my $number = $1;
100 while ($number >= $2) {
101 next if grep(/^$number$/, @excludes);
102 push (@output_array, $number);
103 $number--;
104 }
105 } else {
106 my $number;
107 foreach $number ($1..$2) {
108 next if grep(/^$number$/, @excludes);
109 push (@output_array, $number);
110 }
111 }
112 } elsif ($expression =~ /^($number_exp)\.\.($number_exp)i($number_exp)$/) {
113 print "1: $1\n2: $2\n3: $3\n" if ($verbose >= 3);
114 if ($1 > $2) {
115 my $number = $1;
116 while ($number >= $2) {
117 next if grep(/^$number$/, @excludes);
118 $number = eval($number); # This fixes a stupid computer math problem.
119 push (@output_array, $number);
120 $number = $number - $3;
121 }
122 } else {
123 my $number = $1;
124 while ($number <= $2) {
125 next if grep(/^$number$/, @excludes);
126 $number = eval($number); # This fixes a stupid computer math problem.
127 push (@output_array, $number);
128 $number = $number + $3;
129 }
130 }
131 } else {
132 print STDERR "Invalid expression: $expression\n" if ($verbose >= 1);
133 }
134 }
135
136 my $i = 0;
137 my $max = @output_array;
138 while ($i < $max) {
139 if ($i == ($max - 1)) {
140 print $prefix . $output_array[$i] . $suffix; # No need to put a separator for the last element.
141 } else {
142 print $prefix . $output_array[$i] . $suffix . $separator;
143 }
144 $i++;
145 }
146
147 print "\n";
148
149 exit(0);
150
151 ###############
152 # SUBROUTINES #
153 ###############
154
155 sub help {
156 print <<"EOF";
157 --------------------------------------------------------------------
158 range : Print out a range of numbers for use in for loops and such.
159 --------------------------------------------------------------------
160 Usage:
161
162 range [options] /<expression>/
163
164 Options:
165 -e <set> Exclude the <set> of numbers from the range output. <set>
166 is a set of numbers separated by commas.
167 -n <n> Use <n> as a separator between numbers. By default, it
168 is a space, use '\n' or \\n for newline or the -N option.
169 -N Just a quick option for using a newline as the separator.
170
171 -p <string> Specify a prefix to use for every number output.
172 -s <string> Specify a suffix to use for every number output.
173
174 -d Debug. For developers only.
175 -h Help: You're looking at it.
176 -V Increase verbosity.
177
178 Expressions:
179
180 /1..100/ All numbers from 1 to 100.
181 /1..50,60..75/ Multiple ranges.
182 /0..100i2/ All even numbers from 0 to 100.
183 /1..100i2/ All odd numbers from 1 to 100.
184 /3..100i3/ Factors of 3.
185 /1.1..4.5i0.1/ Decimal ranges.
186
187 EOF
188 }
189
190
191
192 # Lay down some of that perl pod action.
193 =pod
194
195 =head1 NAME
196
197 range - Print out a range of numbers for use in for loops and such.
198
199 =head1 SYNOPSIS
200
201 B<range> [-dhV] /<expression>/
202
203 =head1 DESCRIPTION
204
205 B<range> is a program that is part of the numeric utilities package. B<range>
206 will print out a list of numbers based on an expression that you specify. This
207 is useful for making a list of numbers for use in for loops and so on. Ranges of
208 numbers are specified using the .. operator, like this /20..50/, which means all
209 integers from 20 and 50. More complex expressions can be generated using the commas
210 and the 'i' increment operator.
211
212 =head1 OPTIONS
213
214 -e <set> Exclude the <set> of numbers from the range output. <set>
215 is a set of numbers separated by commas.
216 -n <n> Use <n> as the separator between numbers. By default, it
217 will use a space. Use '\n' or \\n for a newline character or
218 use the -N option.
219 -N Just a quick option for using a newline as the separator.
220
221 -h Help: You're looking at it.
222 -V Increase verbosity.
223 -d Debug mode. For developers
224
225 =head1 EXAMPLES
226
227 All numbers from 1 to 10.
228 $ range /1..10/
229 1 2 3 4 5 6 7 8 9 10
230
231 From 10 to 1. Counting down.
232 $ range /10..1/
233 10 9 8 7 6 5 4 3 2 1
234
235 From 1 to 10 and from 15 to 20.
236 $ range /1..10,15..20/
237 1 2 3 4 5 6 7 8 9 10 15 16 17 18 19 20
238
239 Even numbers from 0 to 10
240 $ range /0..10i2/
241 0 2 4 6 8 10
242
243 Odd numbers. Notice the starting number in the range expression.
244 $ range /1..10i2/
245 1 3 5 7 9
246
247 Factors of 3 between 99 and 120.
248 $ range /99..120i3/
249 99 102 105 108 111 114 117 120
250
251 Decimal numbers
252 $ range /1.1..2.5i0.1/
253 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 2.1 2.2 2.3 2.4 2.5
254
255 And negative numbers too.
256 $ range /1.0..-2.0i0.3/
257 1 0.7 0.4 0.1 -0.2 -0.5 -0.8 -1.1 -1.4 -1.7 -2
258
259 You can also pad numbers when you are counting up. This is
260 a trick of how the Perl programming language deals with ranges:
261
262 $ range /01..15/
263 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
264
265 =head1 BUGS
266
267 Even though you can do zero padding on simple ranges, like 001..100,
268 it will not pad zeros on complex ranges like 001..100i2, or for counting
269 downwards.
270
271 =head1 SEE ALSO
272
273 seq(1), average(1), bound(1), interval(1), normalize(1), numgrep(1), numprocess(1), numsum(1), random(1), round(1)
274
275 =head1 COPYRIGHT
276
277 range is part of the num-utils package, which is copyrighted by
278 Suso Banderas and released under the GPL license. Please read
279 the COPYING and LICENSE files that came with the num-utils package
280
281 Developers can read the GOALS file and contact me about providing
282 submitions or help for the project.
283
284 =head1 MORE INFO
285
286 More info on range can be found at:
287
288 =over 1
289
290 =item B<http://suso.suso.org/programs/num-utils/>
291
292 =back
293
294 =cut
295