943,568 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 513
  • PHP RSS
Sep 1st, 2009
0

Itterating - "a" through "zzzz"

Expand Post »
Hi all. I'm attempting to output all of the possible combination from "a" to "zzzz" and I'm wondering if theres an easy way of doing that. As an example output would be ...

a
b
c
....
z
aa
ab
ac
....
az
ba
bb
...

and so on..

Is there a way to just have a binary or hex value, cast it as an ascii to get your first value, then increment it and cast it again.. and so on?

Any help would be much appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Sep 2nd, 2009
1

Re: Itterating - "a" through "zzzz"

This is how I would do it:
PHP Syntax (Toggle Plain Text)
  1. $lower = true; //if you want lowercase letters
  2. $t = 4;
  3. $s = 1;
  4. while( $s <= $t ) {
  5. $i = ( $lower ? 97 : 65 );
  6. while( $i <= ( $lower ? 122 : 90 ) ) {
  7. echo str_repeat( chr( $i ),$s ) . '<br />';
  8. $i++;
  9. }
  10. $s++;
  11. }

That what you are talking about?
Last edited by kkeith29; Sep 2nd, 2009 at 2:00 am.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Sep 2nd, 2009
0

Re: Itterating - "a" through "zzzz"

Click to Expand / Collapse  Quote originally posted by kkeith29 ...
This is how I would do it:
PHP Syntax (Toggle Plain Text)
  1. $lower = true; //if you want lowercase letters
  2. $t = 4;
  3. $s = 1;
  4. while( $s <= $t ) {
  5. $i = ( $lower ? 97 : 65 );
  6. while( $i <= ( $lower ? 122 : 90 ) ) {
  7. echo str_repeat( chr( $i ),$s ) . '<br />';
  8. $i++;
  9. }
  10. $s++;
  11. }

That what you are talking about?

Well sorta.. It would basically generate all teh combinations of a set containing 'a' through 'z'. i.e. {a,b,c,...,z}. So it owuldnt just generate aa, bb, cc.. it would go through all the possible combinations with a as the first character then b as the second character and so on.. sorry if I was not specific enough in my initial post.
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Sep 2nd, 2009
0

Re: Itterating - "a" through "zzzz"

This code below will do the trick, but it's going to use a lot of memory to run this.
You are making 26 x 26 x 26 x 26 = 456976 combinations. Are you sure you want to generate all of these combinations? What are you trying to achieve this for?
php Syntax (Toggle Plain Text)
  1. <?
  2. for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
  3. {
  4. for($b = 97; $b <= 122; $b++)
  5. {
  6. for($c = 97; $c <= 122; $c++)
  7. {
  8. for($d = 97; $d <= 122; $d++)
  9. {
  10. echo chr($a).chr($b).chr($c).chr($d)."<br/>";
  11. }
  12. }
  13. }
  14. }
  15. ?>
Reputation Points: 14
Solved Threads: 10
Junior Poster in Training
SimonMayer is offline Offline
53 posts
since Apr 2008
Sep 2nd, 2009
0

Re: Itterating - "a" through "zzzz"

In fact, I realise that the above code will not produce your single, double and triple character strings.
There are more elegant ways of doing it, but I am not able to put the time into devising one at the moment. One way would be just to repeat the code like so:
php Syntax (Toggle Plain Text)
  1. <?
  2. for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
  3. {
  4. echo chr($a)."<br/>";
  5. }
  6.  
  7.  
  8.  
  9. for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
  10. {
  11. for($b = 97; $b <= 122; $b++)
  12. {
  13. echo chr($a).chr($b)."<br/>";
  14. }
  15. }
  16.  
  17.  
  18.  
  19. for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
  20. {
  21. for($b = 97; $b <= 122; $b++)
  22. {
  23. for($c = 97; $c <= 122; $c++)
  24. {
  25. echo chr($a).chr($b).chr($c)."<br/>";
  26. }
  27. }
  28. }
  29.  
  30.  
  31. for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
  32. {
  33. for($b = 97; $b <= 122; $b++)
  34. {
  35. for($c = 97; $c <= 122; $c++)
  36. {
  37. for($d = 97; $d <= 122; $d++)
  38. {
  39. echo chr($a).chr($b).chr($c).chr($d)."<br/>";
  40. }
  41. }
  42. }
  43. }
  44. ?>
Reputation Points: 14
Solved Threads: 10
Junior Poster in Training
SimonMayer is offline Offline
53 posts
since Apr 2008
Sep 16th, 2009
0

Re: Itterating - "a" through "zzzz"

Click to Expand / Collapse  Quote originally posted by SimonMayer ...
This code below will do the trick, but it's going to use a lot of memory to run this.
You are making 26 x 26 x 26 x 26 = 456976 combinations. Are you sure you want to generate all of these combinations? What are you trying to achieve this for?
Thanks so much. Thats exactly what I was looking for. I am sorta unfamiliar with PHPs type casting syntax. I'm trying to stress test a script i'm preparing and just thought this would be a cool way to do it as well as a way to test myself and learn more.

Thanks again!
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: updating multiple rows with one form
Next Thread in PHP Forum Timeline: Freak include issue!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC