Debian Codegraph @

range

raw ◀ ▶
Source reconstructed at patch state 2 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. Ranges of
207 numbers are specified using the .. operator, like this /20..50/, which means all
208 integers from 20 and 50. More complex expressions can be generated using the commas
209 and the 'i' increment operator.
210
211 =head1 OPTIONS
212
213 -e <set> Exclude the <set> of numbers from the range output. <set>
214 is a set of numbers separated by commas.
215 -n <n> Use <n> as the separator between numbers. By default, it
216 will use a space. Use '\n' or \\n for a newline character or
217 use the -N option.
218 -N Just a quick option for using a newline as the separator.
219
220 -h Help: You're looking at it.
221 -V Increase verbosity.
222 -d Debug mode. For developers
223
224 =head1 EXAMPLES
225
226 All numbers from 1 to 10.
227 $ numrange /1..10/
228 1 2 3 4 5 6 7 8 9 10
229
230 From 10 to 1. Counting down.
231 $ numrange /10..1/
232 10 9 8 7 6 5 4 3 2 1
233
234 From 1 to 10 and from 15 to 20.
235 $ numrange /1..10,15..20/
236 1 2 3 4 5 6 7 8 9 10 15 16 17 18 19 20
237
238 Even numbers from 0 to 10
239 $ numrange /0..10i2/
240 0 2 4 6 8 10
241
242 Odd numbers. Notice the starting number in the range expression.
243 $ numrange /1..10i2/
244 1 3 5 7 9
245
246 Factors of 3 between 99 and 120.
247 $ numrange /99..120i3/
248 99 102 105 108 111 114 117 120
249
250 Decimal numbers
251 $ numrange /1.1..2.5i0.1/
252 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
253
254 And negative numbers too.
255 $ numrange /1.0..-2.0i0.3/
256 1 0.7 0.4 0.1 -0.2 -0.5 -0.8 -1.1 -1.4 -1.7 -2
257
258 You can also pad numbers when you are counting up. This is
259 a trick of how the Perl programming language deals with ranges:
260
261 $ numrange /01..15/
262 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
263
264 =head1 BUGS
265
266 Even though you can do zero padding on simple ranges, like 001..100,
267 it will not pad zeros on complex ranges like 001..100i2, or for counting
268 downwards.
269
270 =head1 SEE ALSO
271
272 seq(1), numaverage(1), numbound(1), numinterval(1), numnormalize(1), numgrep(1), numprocess(1), numsum(1), numrandom(1), numround(1)
273
274 =head1 COPYRIGHT
275
276 numrange is part of the num-utils package, which is copyrighted by
277 Suso Banderas and released under the GPL license. Please read
278 the COPYING and LICENSE files that came with the num-utils package
279
280 Developers can read the GOALS file and contact me about providing
281 submitions or help for the project.
282
283 =head1 MORE INFO
284
285 More info on numrange can be found at:
286
287 =over 1
288
289 =item B<http://suso.suso.org/programs/num-utils/>
290
291 =back
292
293 =cut
294