Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Incrementing Strings

Posted on 1998-07-15
17
Medium Priority
?
303 Views
Last Modified: 2012-05-04
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.
0
Comment
Question by:cerac000
  • 6
  • 5
  • 4
  • +1
17 Comments
 
LVL 6

Accepted Solution

by:
alamo earned 300 total points
ID: 1208440
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
 
LVL 1

Expert Comment

by:notanexpert
ID: 1208441
#!/usr/bin/perl
for (a...z) {

print "$_";
}

How about that?
0
 
LVL 85

Expert Comment

by:ozo
ID: 1208442
 for( $STR='a'; length($STR) <= 1; $STR++ ){ print "$STR\n"; }
#or
  for( 'a' .. 'z' ){ print "$_\n" }
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:alamo
ID: 1208443
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
 
LVL 1

Expert Comment

by:notanexpert
ID: 1208444
Heehee, I'm as smart as ozo! ;) I guess I shoulda posted that as an answer and not a comment but oh well ..
0
 

Author Comment

by:cerac000
ID: 1208445
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
 

Author Comment

by:cerac000
ID: 1208446
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
 
LVL 1

Expert Comment

by:notanexpert
ID: 1208447
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
 
LVL 6

Expert Comment

by:alamo
ID: 1208448
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
 

Author Comment

by:cerac000
ID: 1208449
CASE CLOSED,

ALAMO YOU ARE THE MAN

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

WAS THE WINNER, THANK YOU
0
 
LVL 1

Expert Comment

by:notanexpert
ID: 1208450
D'oh .. 'for' not 'from' I meant. Sigh. Back to the newbies section with me. ;)
0
 
LVL 1

Expert Comment

by:notanexpert
ID: 1208451
Arg. Moments too late.
0
 
LVL 6

Expert Comment

by:alamo
ID: 1208452
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
 
LVL 6

Expert Comment

by:alamo
ID: 1208453
Thanks, cerac000! And sorry, notanexpert, sometimes it's just the luck of the timing.
0
 

Author Comment

by:cerac000
ID: 1208454
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
 
LVL 6

Expert Comment

by:alamo
ID: 1208455
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
 
LVL 85

Expert Comment

by:ozo
ID: 1208456
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question