Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Incrementing Strings

Posted on 1998-07-15
17
280 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 100 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 84

Expert Comment

by:ozo
ID: 1208442
 for( $STR='a'; length($STR) <= 1; $STR++ ){ print "$STR\n"; }
#or
  for( 'a' .. 'z' ){ print "$_\n" }
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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 84

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

789 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