Link to home
Start Free TrialLog in
Avatar of cerac000
cerac000

asked on

Incrementing Strings

I want to increment a letter 'a' all the way to 'z'.   I tried something like this:
for ($STR='a';$STR le 'z';$STR++) {print "$STR\n";}  
but it did not work.  Please help.
ASKER CERTIFIED SOLUTION
Avatar of alamo
alamo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
#!/usr/bin/perl
for (a...z) {

print "$_";
}

How about that?
Avatar of ozo
 for( $STR='a'; length($STR) <= 1; $STR++ ){ print "$STR\n"; }
#or
  for( 'a' .. 'z' ){ print "$_\n" }
Avatar of alamo
alamo

ooh, very nice, I was thinking in a for-loop-index mode rather than an array mode... slightly better for the example would be

for $STR (a..z) {print "$STR\n"; }
or
for $STR ('a'..'z') {print "$STR\n"; }
just in case a or z is a defined symbol (unlikely, but...)
Heehee, I'm as smart as ozo! ;) I guess I shoulda posted that as an answer and not a comment but oh well ..
Avatar of cerac000

ASKER

The first part didn't work, but the second one did.  Now if I wanted to have it go from aa  to zz
So something like:

for ($STR = "aa"; $STR < "zz"  $STR = chr ( ord ( $STR) + 1) )
    print "$STR\n";

will print
aa
ab
ac
ad
.
ba
bb
bc

all the way to zz
The first part didn't work, but the second one did.  Now if I wanted to have it go from aa  to zz
So something like:

for ($STR = "aa"; $STR < "zz"  $STR = chr ( ord ( $STR) + 1) )
    print "$STR\n";

will print
aa
ab
ac
ad
.
ba
bb
bc

all the way to zz
from aa, bb, cc ... zz?

from (a...z) {

print "$_$_\n";

}

from aa, ab, ac ... zy, zz

from (aa...zz) {

print "$_\n";

}

Do you have to be incrementing a variable? If you know you're going from a to z everytime then you don't need to use a variable ...

Hmm, not sure why #1 didn't work...  a variation on it will do what you want (change ==1 to ==2):

for ($STR='aa'; $STR le 'zz' && length($STR)==2; $STR++) {print "$STR\n";}

(#2 won't work because it assumes $STR is one character when it converts to an integer and back to a string).

although at this point much better for aa..zz would be:
for $STR ('aa'..'zz') {print "$STR\n"; }
CASE CLOSED,

ALAMO YOU ARE THE MAN

for $STR (aa..zz) {print "$STR\n";}

WAS THE WINNER, THANK YOU
D'oh .. 'for' not 'from' I meant. Sigh. Back to the newbies section with me. ;)
Arg. Moments too late.
Many of these different ways have pluses and minuses in the way they scale... if you wanted to go 'aaaa'..'zzzz'  the $STR++ would handle it just fine, whereas ('aaaa'..'zzzz') is an array of 456,976 elements which might impact yout performance.

notanexpert - you would have been too late since I had already 'locked' the question ... (though cerac000 can certainly reject my answer and ask you to post yours as an answer if he or she wishes).

But, being as smart as ozo means you would almost never post answers anyway, only comments that incisively cut to the heart of the matter under discussion :-)
Thanks, cerac000! And sorry, notanexpert, sometimes it's just the luck of the timing.
I hate to bring the cat out of the bag again, but apparently I need something different.  The notation of (aa..zz) I don't think will work for me b/c this needs to be incremented in a for loop where the condition trying to be satisfied does not rely on the STR concatenation.

The loop looks something like

for ($j =1, $STR = "aa";  $j <= $SETS;  $j++, $STR=chr(ord($STR) + 1)) {print "$STR\n";}

Now when this prints, it prints  'aa' the first time, but after that it only prints just single letters like:

aa
b
c
d
e

I want it to go
aa
ab
ac
ad
ae
etc
etc.

Someone please help
Ok, then try:

for ($j =1, $STR = "aa";  $j <= $SETS;  $j++, $STR++) {print "$STR\n";}

Using ++ on the string will produce the behavior you want, and it seems you are using $SETS to halt the loop, not the values of $STR, so you won't have a problem with 'zz' turning into 'aaa'.
Well, it looks like my slow comment was superfluous with everyone jumping in:-)
I guess I should still try to say something useful...

 ('a'..'z')
seems the most convenient for reasonably sized lists,
(I'd want to quote it, as alamo says, in case someone writes sub z {}, or so you don't get confused about why (a..s) doesn't work)
  for( $str='a';length($STR)<=length('zzz'); $STR++ )
should save space for larger sets,
(although there are rumours that the next version of Perl may optimize for( $a...$b ) so that it doesn't need an intermediate array.)
and just for more-than-one-way-to-do-it sake, I might offer
  for( $STR='a'; !(($STR eq 'z')..0)||??; $STR++ ){ print "$STR\n";}
(then again, maybe not...)
On the last question, I might have considered someting like
  $STR='aa';
  for( 1..$SETS ){ print $STR++,"\n" }