Debian Codegraph @

range

raw ◀ ▶
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 +$Getopt::Std::STANDARD_HELP_VERSION = 1;
31
32 my ($expression, @output_array, $number);
33
34 getopts('e:n:Np:s:hdV', \%opts);
35
36 my $prefix = "";
37 my $suffix = "";
38
39 if ($opts{'h'}) {
40 &help();
41 exit(0);
42 }
43
44 if ($opts{'d'}) {
45 $verbose = 3;
46 print STDERR "Debug mode\n";
47 } elsif ($opts{'V'}) {
48 $verbose = 2;
49 print STDERR "Verbose mode\n";
50 } elsif ($opts{'q'}) {
51 $verbose = 0; # Nothing except the final answer
52 } else {
53 $verbose = 1; # Normal warnings and such.
54 }
55
56 my $range_expression = shift || die "You must specify a range expression\n";
57
58 # A shortcut for specifying a prefix and suffix.
•59 $prefix = $1 if( $range_expression =~ m/^([^\/]+)\// );
60 $range_expression =~ s/^[^\/]+\//\//;
•61 $suffix = $1 if( $range_expression =~ m/\/([^\/]+)$/ );
62 $range_expression =~ s/\/[^\/]+$/\//;
63
64 $range_expression =~ s/^\///;
65 $range_expression =~ s/\/$//;
66 my @range_expressions = split(/,/, $range_expression);
67
68 my $separator = $opts{'n'} || " ";
69 $separator =~ s/\\n/\n/g;
70 $separator = "\n" if ($opts{'N'});
71
72 if ($opts{'p'}) {
73 $prefix = $opts{'p'};
74 }
75 if ($opts{'s'}) {
76 $suffix = $opts{'s'};
77 }
78
79 # Make the array of excluded numbers
80 my @excludes;
81 if ($opts{'e'}) {
82 @excludes = split(/,/, $opts{'e'});
83 }
84
85 ################
86 # MAIN PROGRAM #
87 ################
88
89 my $number_exp = "-?[0-9]*\\.?[0-9]+";
90
91 foreach $expression (@range_expressions) {
92 print "expression: $expression\n" if ($verbose >= 3);
93 if ($expression =~ /^($number_exp)$/) {
94 push @output_array, $expression;
95 } elsif ($expression =~ /^($number_exp)\.\.($number_exp)$/) {
96 print "1: $1\n2: $2\n" if ($verbose >= 3);
97 if ($1 > $2) {
98 my $number = $1;
99 while ($number >= $2) {
100 next if grep(/^$number$/, @excludes);
101 push (@output_array, $number);
102 $number--;
103 }
104 } else {
105 my $number;
106 foreach $number ($1..$2) {
107 next if grep(/^$number$/, @excludes);
108 push (@output_array, $number);
109 }
110 }
111 } elsif ($expression =~ /^($number_exp)\.\.($number_exp)i($number_exp)$/) {
112 print "1: $1\n2: $2\n3: $3\n" if ($verbose >= 3);
113 if ($1 > $2) {
114 my $number = $1;
115 while ($number >= $2) {
116 next if grep(/^$number$/, @excludes);
117 $number = eval($number); # This fixes a stupid computer math problem.
118 push (@output_array, $number);
119 $number = $number - $3;
120 }
121 } else {
122 my $number = $1;
123 while ($number <= $2) {
124 next if grep(/^$number$/, @excludes);
125 $number = eval($number); # This fixes a stupid computer math problem.
126 push (@output_array, $number);
127 $number = $number + $3;
128 }
129 }
130 } else {
•131 die "Invalid expression: $expression\n";
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 numrange : Print out a range of numbers for use in for loops and such.
159 --------------------------------------------------------------------
160 Usage:
161
•162 numrange [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 sub HELP_MESSAGE{
191 &help();
192 }
193
194 # Lay down some of that perl pod action.
195 =pod
196
197 =head1 NAME
198
•199 numrange - Print out a range of numbers for use in for loops and such.
200
201 =head1 SYNOPSIS
202
•203 B<numrange> [-dhV] /<expression>/
204
205 =head1 DESCRIPTION
206
•207 B<numrange>
208 will print out a list of numbers based on an expression that you specify. This
•209 is useful for making a list of numbers for use in for loops and so on.
210 Ranges are inclusive.
211 Ranges of
212 numbers are specified using the .. operator, like this /20..50/, which means all
•213 integers from 20 to 50 inclusive. More complex expressions can be generated using the commas
214 and the 'i' increment operator.
215
216 =head1 OPTIONS
217
218 -e <set> Exclude the <set> of numbers from the range output. <set>
219 is a set of numbers separated by commas.
220 -n <n> Use <n> as the separator between numbers. By default, it
221 will use a space. Use '\n' or \\n for a newline character or
222 use the -N option.
223 -N Just a quick option for using a newline as the separator.
224
225 -h Help: You're looking at it.
226 -V Increase verbosity.
227 -d Debug mode. For developers
228
229 =head1 EXAMPLES
230
231 All numbers from 1 to 10.
•232 $ numrange /1..10/
233 1 2 3 4 5 6 7 8 9 10
234
235 From 10 to 1. Counting down.
•236 $ numrange /10..1/
237 10 9 8 7 6 5 4 3 2 1
238
239 From 1 to 10 and from 15 to 20.
•240 $ numrange /1..10,15..20/
241 1 2 3 4 5 6 7 8 9 10 15 16 17 18 19 20
242
243 Even numbers from 0 to 10
•244 $ numrange /0..10i2/
245 0 2 4 6 8 10
246
247 Odd numbers. Notice the starting number in the range expression.
•248 $ numrange /1..10i2/
249 1 3 5 7 9
250
251 Factors of 3 between 99 and 120.
•252 $ numrange /99..120i3/
253 99 102 105 108 111 114 117 120
254
255 Decimal numbers
•256 $ numrange /1.1..2.5i0.1/
257 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
258
259 And negative numbers too.
•260 $ numrange /1.0..-2.0i0.3/
261 1 0.7 0.4 0.1 -0.2 -0.5 -0.8 -1.1 -1.4 -1.7 -2
262
263 You can also pad numbers when you are counting up. This is
264 a trick of how the Perl programming language deals with ranges:
265
•266 $ numrange /01..15/
267 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
268
269 =head1 BUGS
270
271 Even though you can do zero padding on simple ranges, like 001..100,
272 it will not pad zeros on complex ranges like 001..100i2, or for counting
273 downwards.
274
275 =head1 SEE ALSO
276
•277 seq(1), numaverage(1), numbound(1), numinterval(1), numnormalize(1), numgrep(1), numprocess(1), numsum(1), numrandom(1), numround(1)
278
279 =head1 COPYRIGHT
280
•281 numrange is part of the num-utils package, which is copyrighted by
282 Suso Banderas and released under the GPL license. Please read
283 the COPYING and LICENSE files that came with the num-utils package
284
285 Developers can read the GOALS file and contact me about providing
286 submitions or help for the project.
287
288 =head1 MORE INFO
289
•290 More info on numrange can be found at:
291
292 =over 1
293
•294 =item B<http://suso.suso.org/xulu/Num-utils>
295
296 =back
297
298 =cut
299