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.
cerac000Asked:
Who is Participating?
 
alamoCommented:
You could do it as:

for ($STR='a';$STR le 'z' && length($STR)==1;$STR++) {print "$STR\n";}  
or
for ($STR='a';$STR le 'z';$STR=chr(ord($STR)+1)) {print "$STR\n";}  
or a lot of other ways ways that involve using a numeric index rather than a string index...

Your original fails because ++ is special with strings and 'z' increments to 'aa', which is still less than 'zz'. Method 1 gets around this by making sure the string is still only one bytes. Method 2 gets around this by not using ++, instead converting the string to a number, adding 1, and back to a string (so the character after 'z' is '{').

Hope this helps!
0
 
notanexpertCommented:
#!/usr/bin/perl
for (a...z) {

print "$_";
}

How about that?
0
 
ozoCommented:
 for( $STR='a'; length($STR) <= 1; $STR++ ){ print "$STR\n"; }
#or
  for( 'a' .. 'z' ){ print "$_\n" }
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
alamoCommented:
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...)
0
 
notanexpertCommented:
Heehee, I'm as smart as ozo! ;) I guess I shoulda posted that as an answer and not a comment but oh well ..
0
 
cerac000Author Commented:
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
0
 
cerac000Author Commented:
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
0
 
notanexpertCommented:
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 ...

0
 
alamoCommented:
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"; }
0
 
cerac000Author Commented:
CASE CLOSED,

ALAMO YOU ARE THE MAN

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

WAS THE WINNER, THANK YOU
0
 
notanexpertCommented:
D'oh .. 'for' not 'from' I meant. Sigh. Back to the newbies section with me. ;)
0
 
notanexpertCommented:
Arg. Moments too late.
0
 
alamoCommented:
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 :-)
0
 
alamoCommented:
Thanks, cerac000! And sorry, notanexpert, sometimes it's just the luck of the timing.
0
 
cerac000Author Commented:
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
0
 
alamoCommented:
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'.
0
 
ozoCommented:
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" }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.