Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advanced counter

Status
Not open for further replies.

Masali

Programmer
Jun 19, 2002
143
SE
Hello!

I would like to make a routine that uses:
Code:
my $alf="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
my $start="0d9X";
my $end="0ed69";

Using above data it should print out a list like this:

0d9X
0d9Y
0d9Z
0da0
0da1
...
ZZZW
ZZZX
ZZZY
ZZZZ
00000
00001
00002
...
0ed67
0ed68
0ed69


Best regards
Masali
 
I'm not normally a fan of code handouts, but this one sounded fun. :)
Code:
my $start="0d9X";
my $end="0ed69";

my $i = $start;
print "$i\n";
until($i eq $end) {
	$i = incr($i,length($i)-1);
	print "$i\n";
}

sub incr {
	my($str,$pos) = @_;
	return "0$str" if($pos < 0);
	my $c = substr($str,$pos,1);
	$c =~ tr/0-9a-zA-Z/1-9a-zA-Z0/;
	substr($str,$pos,1) = $c;
	
	if($c eq '0') {
		return incr($str,$pos-1);
	} else {
		return $str;
	}
}
Use tr/// to rotate through your alphabet, and make it recursive to count up more significant digits. Plus, don't forget the base case of adding significant digits.

There's a certain amount of magic that the ++ operator can do, but I don't think it's quite enough for this.

________________________________________
Andrew

I work for a gift card company!
 
Code:
BEGIN {
  ALF=\
  "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  start="d9X" ; end="ed69"
  while ( start != end )
  { start = incstr( start )
    print start
  }
}

function incstr( s    ,p,n,chr )
{ p = length(s)
  n = 1
  while ( n && p )
  { chr = incchr( substr(s,p,1) )
    s = substr(s,1,p-1) chr substr(s,p+1)
    p--
    n = ("0"==chr) ? 1 : 0
  }
  if ( n )   s = "1" s
  return s
}

function incchr( chr )
{ chr = substr( ALF, index( ALF, chr ) + 1, 1)
  if (!chr)  chr = "0"
  return chr
}
For nawk or awk.
 
@alfs=split//,$alfs

would give you all the elements of the $alfs in an array which you could loop through, using ++, starting from the given start point, and at the end of each iteration, reset the counter (++), and check to see if you need to increment the length of the string if the last character is Z, but it's gonna be a bloody long stream of output, and about 10^64^4 lines (I'm not sure that sounds right ...?)


What is the application?

--Paul

Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
If you don't mind using only lowercase letters and starting at 1, how about converting the number into base 36 (lists 0-z) and then incrementing the number, converting back, etc.? I have a sample Java program (short!) that explains what I mean if necessary...

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top