File: rgind

package info (click to toggle)
saml 970418-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,204 kB
  • ctags: 1,701
  • sloc: ansic: 17,182; sh: 2,583; yacc: 497; perl: 264; makefile: 250; python: 242
file content (44 lines) | stat: -rwxr-xr-x 1,037 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl
#
# Expands ranges of indices. The arguments are given on the command line
# and the expanded expression is written on standard output. For instance,
# "rgind foo[1..2,bar,13..15]" would yield
#
#    foo[1,bar,13] foo[1,bar,14] foo[1,bar,15]
#    foo[2,bar,13] foo[2,bar,14] foo[2,bar,15]
#
# Ranges can have negative limits as well. If no argument is given,
# the standard input is used.
#
# Written by T. Bousch, and placed in the Public Domain.

$sep = "";
if ($#ARGV >= 0) {
	# Expand the arguments on the command line
	foreach $_ (@ARGV) {
		&expand_name($_);
		print "\n";
	}
}
else {
	# No arguments, read expressions on standard input, one per line
	select STDIN;  $|=1; 
	select STDOUT; $|=1;
	while (<STDIN>) {
		chop;
		&expand_name($_);
		print "\n";
	}
}
exit 0;

sub expand_name {
	local($name) = @_;
	local($i, $before, $after);
	if ($name =~ /(-?\d+)\.\.(-?\d+)/) {
		$before = $`;
		$after  = $';
		foreach $i ($1 .. $2) { &expand_name($before.$i.$after); }
	}
	else { print "$sep$name"; $sep = " "; }
}