Aeternitas Posted November 12, 2010 Share Posted November 12, 2010 This is probably less of a language-specific question and more one related to logic, but since it's written in PHP and it's been vexing me for a few hours now, I figured I'd take a shot asking about here! Simple problem I'm trying to figure out for a class; I have a simple string of characters in the form "111-AAA" (three digits, hyphen, three letters) and I'm supposed to increment the string in a particular order. I increment the digits first, and after they've passed 999, I reset them to 000, increment the right-most column on the letters' side, and start again. I assumed this would be pretty simple, but the code I've written executes with some fairly strange results. With the file posted below, eight iterations into the outermost for() loop ought to echo "119-AAA", but I get "129-AAA" instead. And that's just the beginning - I started off with a thousand iterations so I could see just how drastically this little issue snowballs down the line. I see what's happening: before the value in the column being incremented is reset to 0 or A, the next column to be changed jumps the gun and gets incremented early. But no matter where I stick the increment expression, the same thing happens. I'm sure the problem is right here under my very nose, but I'm completely missing it...could anyone possibly show me what I'm not seeing? <html> <title>Test</title> <body> <?php $oldString = "111-AAA"; echo "Below this line is the string to be modified:<p>"; echo $oldString; echo "<p>And below this line is the modified string:<p>"; $newString = array($oldString[2], $oldString[1], $oldString[0], $oldString[6], $oldString[5], $oldString[4]); for($i=0; $i<1000; $i++) { for ($count = 0; $count < 6; $count++) { $newString[$count]++; if (($newString[$count] > 9) && ($count < 3)) { $newString[$count] = 0; break; } if (($newString[$count] == 'AA') && ($count > 2)) { $newString[$count] = 'A'; break; } if (($newString[$count] != 'AA') && ($newString[$count] != 9)) { break; } } echo $newString[2] . $newString[1] . $newString[0] . '-' . $newString[5] . $newString[4] . $newString[3] . "<br>"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/218461-issue-with-my-programming-logic/ Share on other sites More sharing options...
requinix Posted November 12, 2010 Share Posted November 12, 2010 Treat the two parts like two numbers: the first three characters form a base 10 (0-9) number and the last three form a base 26 (A-Z) number. If you can convert between the decimal forms and the base X forms then you're done. As in, 1. Convert 111 to a number (spoiler: it already is) 2. Add 1 and modulo 1000. 3. If you end up with 0 (ie, it wrapped around) then a) Convert AAA to a number i- A=0, B=1, C=2... Y=24, Z=25 ii- "AAA" = 26^2 * "A" + 26^1 * "A" + 26^0 * "A" = 0 b) Add 1 and (presumably) modulo BAAA (=17576) 4. Add the two parts together again, adding 0-padding to the first and A-padding to the second Quote Link to comment https://forums.phpfreaks.com/topic/218461-issue-with-my-programming-logic/#findComment-1133352 Share on other sites More sharing options...
Aeternitas Posted November 14, 2010 Author Share Posted November 14, 2010 Ah, I like this. I'd thought about conceiving of the letters as base 26 numbers some time ago, but abandoned the idea, got lost in the woods somewhere, and wound up where I was when I originally posed my question. But now I'm back to where I ought to be. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/218461-issue-with-my-programming-logic/#findComment-1134037 Share on other sites More sharing options...
PFMaBiSmAd Posted November 14, 2010 Share Posted November 14, 2010 You can actually increment strings in php and it will work, except when you increment 'ZZZ', it becomes 'AAAA' and you would need to account for that 'roll over' value in your logic. Quote Link to comment https://forums.phpfreaks.com/topic/218461-issue-with-my-programming-logic/#findComment-1134041 Share on other sites More sharing options...
Aeternitas Posted November 14, 2010 Author Share Posted November 14, 2010 Absolutely, and that's another example of complete tunnel vision on my part, since my code already increments characters...and it is my understanding that the difference between a string and a character in php is a semantic one. I should have been able to see that, but no: under stress, I get mired in singular details. :-P Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/218461-issue-with-my-programming-logic/#findComment-1134042 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.