Debian Codegraph @

range

raw ◀ ▶
Source reconstructed at patch state 3 of 5 — not the installed (fully patched) source.
5 Debian patches touch this file
1 #!/usr/bin/perl -w
2
3 # numrange: 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 $prefix = $1 if( $range_expression =~ m/^([^\/]+)\// );
59 $range_expression =~ s/^[^\/]+\//\//;
60 $suffix = $1 if( $range_expression =~ m/\/([^\/]+)$/ );
61 $range_expression =~ s/\/[^\/]+$/\//;
62
63 $range_expression =~ s/^\///;
64 $range_expression =~ s/\/$//;
65 my @range_expressions = split(/,/, $range_expression);
66
67 my $separator = $opts{'n'} || " ";
68 $separator =~ s/\\n/\n/g;
69 $separator = "\n" if ($opts{'N'});
70
71 if ($opts{'p'}) {
72 $prefix = $opts{'p'};
73 }
74 if ($opts{'s'}) {
75 $suffix = $opts{'s'};
76 }
77
78 # Make the array of excluded numbers
79 my @excludes;
80 if ($opts{'e'}) {
81 @excludes = split(/,/, $opts{'e'});
82 }
83
84 ################
85 # MAIN PROGRAM #
86 ################
87
88 my $number_exp = "-?[0-9]*\\.?[0-9]+";
89
90 foreach $expression (@range_expressions) {
91 print "expression: $expression\n" if ($verbose >= 3);
92 if ($expression =~ /^($number_exp)$/) {
93 push @output_array, $expression;
94 } elsif ($expression =~ /^($number_exp)\.\.($number_exp)$/) {
95 print "1: $1\n2: $2\n" if ($verbose >= 3);
96 if ($1 > $2) {
97 my $number = $1;
98 while ($number >= $2) {
99 next if grep(/^$number$/, @excludes);
100 push (@output_array, $number);
101 $number--;
102 }
103 } else {
104 my $number;
105 foreach $number ($1..$2) {
106 next if grep(/^$number$/, @excludes);
107 push (@output_array, $number);
108 }
109 }
110 } elsif ($expression =~ /^($number_exp)\.\.($number_exp)i($number_exp)$/) {
111 print "1: $1\n2: $2\n3: $3\n" if ($verbose >= 3);
112 if ($1 > $2) {
113 my $number = $1;
114 while ($number >= $2) {
115 next if grep(/^$number$/, @excludes);
116 $number = eval($number); # This fixes a stupid computer math problem.
117 push (@output_array, $number);
118 $number = $number - $3;
119 }
120 } else {
121 my $number = $1;
122 while ($number <= $2) {
123 next if grep(/^$number$/, @excludes);
124 $number = eval($number); # This fixes a stupid computer math problem.
125 push (@output_array, $number);
126 $number = $number + $3;
127 }
128 }
129 } else {
130 die "Invalid expression: $expression\n";
131 print STDERR "Invalid expression: $expression\n" if ($verbose >= 1);
132 }
133 }
134
135 my $i = 0;
136 my $max = @output_array;
137 while ($i < $max) {
138 if ($i == ($max - 1)) {
139 print $prefix . $output_array[$i] . $suffix; # No need to put a separator for the last element.
140 } else {
141 print $prefix . $output_array[$i] . $suffix . $separator;
142 }
143 $i++;
144 }
145
146 print "\n";
147
148 exit(0);
149
150 ###############
151 # SUBROUTINES #
152 ###############
153
154 sub help {
155 print <<"EOF";
156 --------------------------------------------------------------------
157 numrange : Print out a range of numbers for use in for loops and such.
158 --------------------------------------------------------------------
159 Usage:
160
161 numrange [options] /<expression>/
162
163 Options:
164 -e <set> Exclude the <set> of numbers from the range output. <set>
165 is a set of numbers separated by commas.
166 -n <n> Use <n> as a separator between numbers. By default, it
167 is a space, use '\n' or \\n for newline or the -N option.
168 -N Just a quick option for using a newline as the separator.
169
170 -p <string> Specify a prefix to use for every number output.
171 -s <string> Specify a suffix to use for every number output.
172
173 -d Debug. For developers only.
174 -h Help: You're looking at it.
175 -V Increase verbosity.
176
177 Expressions:
178
179 /1..100/ All numbers from 1 to 100.
180 /1..50,60..75/ Multiple ranges.
181 /0..100i2/ All even numbers from 0 to 100.
182 /1..100i2/ All odd numbers from 1 to 100.
183 /3..100i3/ Factors of 3.
184 /1.1..4.5i0.1/ Decimal ranges.
185
186 EOF
187 }
188
189
190
191 # Lay down some of that perl pod action.
192 =pod
193
194 =head1 NAME
195
196 numrange - Print out a range of numbers for use in for loops and such.
197
198 =head1 SYNOPSIS
199
200 B<numrange> [-dhV] /<expression>/
201
202 =head1 DESCRIPTION
203
204 B<numrange>
205 will print out a list of numbers based on an expression that you specify. This
206 is useful for making a list of numbers for use in for loops and so on.
207 Ranges are inclusive.
208 Ranges of
209 numbers are specified using the .. operator, like this /20..50/, which means all
210 integers from 20 to 50 inclusive. More complex expressions can be generated using the commas
211 and the 'i' increment operator.
212
213 =head1 OPTIONS
214
215 -e <set> Exclude the <set> of numbers from the range output. <set>
216 is a set of numbers separated by commas.
217 -n <n> Use <n> as the separator between numbers. By default, it
218 will use a space. Use '\n' or \\n for a newline character or
219 use the -N option.
220 -N Just a quick option for using a newline as the separator.
221
222 -h Help: You're looking at it.
223 -V Increase verbosity.
224 -d Debug mode. For developers
225
226 =head1 EXAMPLES
227
228 All numbers from 1 to 10.
229 $ numrange /1..10/
230 1 2 3 4 5 6 7 8 9 10
231
232 From 10 to 1. Counting down.
233 $ numrange /10..1/
234 10 9 8 7 6 5 4 3 2 1
235
236 From 1 to 10 and from 15 to 20.
237 $ numrange /1..10,15..20/
238 1 2 3 4 5 6 7 8 9 10 15 16 17 18 19 20
239
240 Even numbers from 0 to 10
241 $ numrange /0..10i2/
242 0 2 4 6 8 10
243
244 Odd numbers. Notice the starting number in the range expression.
245 $ numrange /1..10i2/
246 1 3 5 7 9
247
248 Factors of 3 between 99 and 120.
249 $ numrange /99..120i3/
250 99 102 105 108 111 114 117 120
251
252 Decimal numbers
253 $ numrange /1.1..2.5i0.1/
254 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
255
256 And negative numbers too.
257 $ numrange /1.0..-2.0i0.3/
258 1 0.7 0.4 0.1 -0.2 -0.5 -0.8 -1.1 -1.4 -1.7 -2
259
260 You can also pad numbers when you are counting up. This is
261 a trick of how the Perl programming language deals with ranges:
262
263 $ numrange /01..15/
264 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
265
266 =head1 BUGS
267
268 Even though you can do zero padding on simple ranges, like 001..100,
269 it will not pad zeros on complex ranges like 001..100i2, or for counting
270 downwards.
271
272 =head1 SEE ALSO
273
274 seq(1), numaverage(1), numbound(1), numinterval(1), numnormalize(1), numgrep(1), numprocess(1), numsum(1), numrandom(1), numround(1)
275
276 =head1 COPYRIGHT
277
278 numrange is part of the num-utils package, which is copyrighted by
279 Suso Banderas and released under the GPL license. Please read
280 the COPYING and LICENSE files that came with the num-utils package
281
282 Developers can read the GOALS file and contact me about providing
283 submitions or help for the project.
284
285 =head1 MORE INFO
286
287 More info on numrange can be found at:
288
289 =over 1
290
291 =item B<http://suso.suso.org/programs/num-utils/>
292
293 =back
294
295 =cut
296