0

how can I get a loop to stop when I've finally reached every possibility. how do I know when it's reached that level? any ideas?

Background: so I have this script I am trying to come up with for a means of a storage issue I am having revolving around files in directories and there being to many in a given directory. that its putting excess load on the machine and render times overall when scripts check to see if a file exists. so I am overall trying to come up with a method of storage to break it down some which involves directories inside of directories inside of directories that literally have all the possible letter number combinations of a string that's currently 6 char large maybe more but I have to crunch some numbers first, anyway I'm trying to think of a fast sane way to provide a given length to something in this case 6 letters or numbers and have it generate an array of every combination a-z and 0-9 of that length problem is I can't think of away to generate that array so I'm hoping someone can help me out here.

I've come up with a method of generating img the random string of what ever length I choose but and putting them into an array isn't so tough but coming up with a method for the stopping point has me.

hakre
  • 193,403
  • 52
  • 435
  • 836
chris
  • 36,115
  • 52
  • 143
  • 252
  • 1
    maybe it would help to post your current code to clarify the problem – Chronial Sep 29 '12 at 08:50
  • Would this be related? [all string combinations in a fixed length, based on a charset](http://stackoverflow.com/questions/7863781/all-string-combinations-in-a-fixed-length-based-on-a-charset) – hakre Sep 29 '12 at 09:24
  • home work no.. lol.. amd all I've ended up with was essentially an infinite loop unless I make it count up or down from a number. which still doesn't give me a definitive stopping point as that's ultimately what I'm trying to factor in.. – chris Sep 29 '12 at 10:16
  • hakra and mr d.. the notion of that yes.. but not having a string as a starting point. only having a set length as a starting point. creating random strings based on length I've figured out, adding them to an array via a loop I've figured out.. making that loop stop when all possible combinations based on length using a-z 0-9 as the possible strings for that length is the tricky part as depending on the given length I could easily be looking at tens of thousands of results.. – chris Sep 29 '12 at 10:21

3 Answers3

4

Here’s a function that calculates the next iteration of a string based on a certain character set:

function next_iteration($str, $charset) {
    // last character in charset that requires a carry-over
    $copos = strlen($charset)-1;
    // starting with the least significant digit
    $i = strlen($str)-1;
    do {
        // reset carry-over flag
        $co = false;
        // find position of digit in charset
        $pos = strpos($charset, $str[$i]);
        if ($pos === false) {
            // invalid input char at position $i
            return false;
        }
        // check whether it’s the last character in the charset
        if ($pos === $copos) {
            // we need a carry-over to the next higher digit
            $co = true;
            // check whether we’ve already reached the highest digit
            if ($i === 0) {
                // no next iteration possible due to fixed string length
                return false;
            }
            // set current digit to lowest charset digit
            $str[$i] = $charset[0];
        } else {
            // if no carry-over is required, simply use the next higher digit
            // from the charset
            $str[$i] = $charset[$pos+1];
        }
        // repeat for each digit until there is no carry-over
        $i--;
    } while ($co);
    return $str;
}

$str = 'aaa';
$charset = 'abc';
do {
    var_dump($str);
} while (($str = next_iteration($str, $charset)) !== false);
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • Not sure what the difference between charset an str in this scenario. Would I be able to put all the letters of the US alphabet and then all numbers 0 through nine as the charset then start my str as I dunno aaaaaa? and ill get all possible 6 char combinations a through z including 0 through 9? essentially Im looking at taking a 36 charset and wanting a 6 (or more or less but in this case 6) char legth string that will and up in an array for later use – chris Sep 29 '12 at 21:28
  • this array I speak of will be all variations of the 6 char combinations, thats my ultimate goal right now. – chris Sep 29 '12 at 21:29
  • @chris Yes, `$charset` would be the set of characters the strings should consist of. The function will calculate the next combination of a given string with a certain length. To get all combinations of a six character long alphanumeric strings, you can use the latter `do … while` loop. Simply set `$charset = 'a…z0…9'` and `$str = 'aaaaaa'` to start with first combination and instead of just dumping the string put it into your array. – Gumbo Sep 29 '12 at 21:58
  • Very cool, Thank you, I have been banging my head against a wall, putting everything into a central loop, running a random char generater I had made some time back for passwords. And yea it was brutally ugly and not quite optimized to say the least, this will hopefully get me where I need to be., thanks again – chris Sep 29 '12 at 22:01
2
$Number= base_convert(mt_rand(1, 9) . intval(microtime(true) * 1000), 10, 36);
echo $Number;
Man Programmer
  • 5,300
  • 2
  • 21
  • 21
1

You can exit any loop at a certain point with the break statement.

For your possibilities I suggest you implement them with an iterator and offer a stack so that you can test if you have consumed all possibilities:

$stackOfAllPossibilities = $possibilities->getStack();

foreach ($possibilities as $posibility)
{
    ...
    $stackOfAllPossibilities->remove($posibility);
    if ($stackOfAllPossibilities->isEmpty()) {
       break;
    }
}
hakre
  • 193,403
  • 52
  • 435
  • 836