Solved

Incrementing Strings

Posted on 1998-07-15
17
264 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
Comment Utility
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
Comment Utility
#!/usr/bin/perl
for (a...z) {

print "$_";
}

How about that?
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
 for( $STR='a'; length($STR) <= 1; $STR++ ){ print "$STR\n"; }
#or
  for( 'a' .. 'z' ){ print "$_\n" }
0
 
LVL 6

Expert Comment

by:alamo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 6

Expert Comment

by:alamo
Comment Utility
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
Comment Utility
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
Comment Utility
D'oh .. 'for' not 'from' I meant. Sigh. Back to the newbies section with me. ;)
0
 
LVL 1

Expert Comment

by:notanexpert
Comment Utility
Arg. Moments too late.
0
 
LVL 6

Expert Comment

by:alamo
Comment Utility
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
Comment Utility
Thanks, cerac000! And sorry, notanexpert, sometimes it's just the luck of the timing.
0
 

Author Comment

by:cerac000
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now