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; }

In reply to Re: Need a better way to break out a range of addresses... by sailortailorson
in thread Need a better way to break out a range of addresses... by RMaxwell

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.