By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

Looping The Alphabet and Numbers

P: 1
Hey,

I want to create a list of letters, starting at AA1 to ZZ15.

IE,

AA1, AA2, AA3, AA4, AA5, AA6, AA7, AA8, AA9, AA10, AA11, AA12, AA13, AA14, AA15, AB1, AB2 etc etc.

I am familiar with loop processes but can't get my head around the logic to this one.

Help appreciated. Thanks.
Mar 2 '08 #1
Share this Question
Share on Google+
6 Replies


hsriat
Expert 100+
P: 1,654
[php]<?php
$abc = array ("A","B","C","D","E",.../*upto*/.....,"X","Y","Z");
$output = array();
for ($i=0; $i<26; $i++)
{
for ($j=0; $j<26; $j++)
{
for ($k=1; $k<=15; $k++)
{
array_push($output, $abc[$i].$abc[$j].$k);
}
}
}
?>[/php]

See if the $output array give you the right thing.
Mar 2 '08 #2

dlite922
Expert 100+
P: 1,584
[PHP]
<?php
$number = 15;
$result = array();
$alpha = array('A','B','C','D','E','F','G','H','I','J','K', 'L','M','N','O','P','Q','R','S','T','U','V','W','X ','Y','Z');
for ($first = 0; $first < count($alpha); $first++) {
for ($second = 0; $second < count($alpha); $second++) {
for ($k = 1; $k <= $number; $k++) {
array_push($result,$alpha[$first].$alpha[$second].$k);
}
}
}
print_r($result); // here's the result, do what you want with it. 10139 Results!!!
?>

[/PHP]

let me know if any questions.
Mar 2 '08 #3

dlite922
Expert 100+
P: 1,584
damn. you beat me to it!

:P mines better!!

/joke, its almost exactly the same.

then again, i don't know how else you can do this.
Mar 2 '08 #4

hsriat
Expert 100+
P: 1,654
damn. you beat me to it!

:P mines better!!

/joke, its almost exactly the same.

then again, i don't know how else you can do this.
Out of two code doing the same thing, the one having less number of lines is better. Going to the depth, you will find mine will consume less memory and machine cycles. :p
(j/k)

But this used to be thought many years ago, when memory and CPU speed were limited. Now, no one cares!, neither its required to care, unless its Embedded C or J2ME.

:)

Harpreet
Mar 2 '08 #5

dlite922
Expert 100+
P: 1,584
Out of two code doing the same thing, the one having less number of lines is better. Going to the depth, you will find mine will consume less memory and machine cycles. :p
(j/k)

But this used to be thought many years ago, when memory and CPU speed were limited. Now, no one cares!, neither its required to care, unless its Embedded C or J2ME.

:)

Harpreet
good point! but I have a better one.

(Sorry for being so competitive, its 1:00 AM and i'm bored)

Better was in the definition that it was easier for him to understand, and even more importantly that a code should be extensible.

As you can see in yours if i choose to drop out letters, the 25 is hardcoded as well as the 15 where in mine you can change those values in their declaration and my code does not have to be changed.

PS: Also i'm trying to knock "dafodil" off the board ;) ....sshhh!! don't tell him I said that.
Mar 2 '08 #6

hsriat
Expert 100+
P: 1,654
I hard coded it to 26 (later edited form 25), coz there are 26 English alphabets. :D

Ok, your is better. Happy now?

(It's 1:00 PM here and I have many other things to do) :p

Regards,
Harpreet :)
Mar 2 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.