in reply to Need a better way to break out a range of addresses...

This is a good job for an iterator.
Mark Jason Dominius talks about this in his book Higher Order Perl, which is supposed to be eventually be available on line, but does not seem to be ready yet. You can look in his code examples, and full text search the book, but you can't just peruse it. Basically you need to implement a closure, which preserves the last value it was called with, and build in a test to end at your desired end value. You may also need to implement a small pattern to let you know where to iterate. Sorry I can't explain more, as I am pressed for time, but here is a working example: put the following in a file called 'Iterator_Utils.pm'
package Iterator_Utils; use base Exporter; use strict; @EXPORT_OK =qw(NEXTVAL Iterator append imap igrep iterate_function filehandle_iterator list_iterator); %EXPORT_TAGS = ('all' => \@EXPORT_OK); sub Iterator (&) { return $_[0] } # 'syntactic sugar' to allow using ' +Iterator { ... }' vice # 'Iterator { sub( ...) }' see Domin +ius, Higher Order Perl, p. 123 sub NEXTVAL { $_[0]->()
keep it in the same dir whence you run the following example code:
#!/perl/bin/perl use strict; use Iterator_Utils('NEXTVAL'); $| = 1; my $bLogToStdout = 1; my $iVerbose = 5; $bLogToStdout = 1; # This example shows how one can generate a range of # URL's with fuskurled numeric arguments. my @aURLsToFuskurl = @ARGV; if ( not @aURLsToFuskurl) { @aURLsToFuskurl = ( "\nEach page of stores index: " , " http://www.hmmm.com/hmmmhmm/cc/main/a_z_index/act/<-{(a..z, +'-','~')}->%2C0%2C0/ccsyn/260" , "\nSome URL forced to display GSL's corresponding to sourceid 1- +20: " , " http://www.hmmm.com/op/~Mens_Mock_Neck_Chevron_Sweater" . ",_Big_Sizes-prod-39296606?slkd=<-{1..20}->" , "\nSame URL, different cluster: " , " http://<-{ qw{www staging marketing} }->.hmmm.com/cc.hmm?ma +in=aprod&act=k24," . "g1,~tea+for+one+teapot+and+cup+set,nover,i1" ); } my @aURLsFuskurled; for (@aURLsToFuskurl) { my $rcFuskurler = fuskurl ( $_ ); while (my $sNextFuskurledURL = NEXTVAL($rcFuskurler)) # # 'kick' the iterator. { push @aURLsFuskurled, $sNextFuskurledURL } } print join "\n", @aURLsFuskurled; sub fuskurl { my $pat = shift; my @tokens = split /(?:\<\-\{|\}\-\>)/, $pat; for (my $i = 1; $i < @tokens; $i += 2) { #$tokens[$i] = [0, split(/,/, $tokens[$i])]; #print "\n" . $tokens[$i] . "\n"; my @restOfArray = eval $tokens[$i] ; #print join "\n", @restOfArray; $tokens[$i] = [0, @restOfArray ]; } my $FINISHED = 0; return sub { return if $FINISHED; my $finished_incrementing = 0; my $result = ''; for my $token ( reverse @tokens) # reverse to work from right + to left (typically best) { if (ref $token eq '') { $result = $token . $result; #suits reversing @toke +ns } else { my ($n, @c) = @{$token}; $result = $c[$n] . $result; #suits reversing @tokens unless ($finished_incrementing) { if ($n == $#c) {$token->[0] = 0 } else {$token->[0]++; $finished_incrementing = 1 } } } } $FINISHED = 1 unless $finished_incrementing; return $result; }

Replies are listed 'Best First'.
Re^2: Need a better way to break out a range of addresses...
by sailortailorson (Scribe) on Mar 28, 2007 at 20:17 UTC
    So, if you put the example code in a file called Iteratedriver.pl, then invoke it with a couple of your examples (my version of Windows requires me to invoke with 'perl' leading any command with args:

    C:\Documents and Settings\martyg\My Documents\my scripts\Perl>perl Ite +ratedriver.pl "172.<-{19..21}->.254.<-{2..3}->" "192.168.1.<-{1..3}-> +" 172.19.254.2 172.19.254.3 172.20.254.2 172.20.254.3 172.21.254.2 172.21.254.3 192.168.1.1 192.168.1.2 192.168.1.3 C:\Documents and Settings\martyg\My Documents\my scripts\Perl>