www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Undefined offset problem

  1. #1
    Join Date
    May 2006
    Posts
    245

    Undefined offset problem

    Hi,

    My script is supposed to step through my data sets and
    create new data groups.

    The data is in this style:

    Set 1: {A_one|A_two|A_three|A_four|A_five}
    Set 2: {B_one|B_two}
    ...
    etc.

    What I my script needs to do is select the first data element from all the sets of data, then select the second data element from all the sets and then the third and so on. If there is no third element then the script needs to select the first element of that set again and then continue with the second in that set ( but continuing as normal with all other sets ).

    So each set continues outputting sequentially and on ending returns to beginning until the desired number of groups are created..

    In this case I have set the number of groups to 10.

    My output is:

    Notice: Undefined offset: 5 in /home/com567b/public_html/auto_change.php on line 35 Notice: Undefined offset: 6 in /home/com567b/public_html/auto_change.php on line 35
    Created New Group: 1
    A_one B-two C-three D-four E-five H-eight

    Notice: Undefined offset: 5 in /home/com567b/public_html/auto_change.php on line 35 Notice: Undefined offset: 6 in /home/com567b/public_html/auto_change.php on line 35
    Created New Group: 2
    A_one B-two C-three D-four E-five H-eight

    etc ... up to New Group: 10

    The new groups are all the same.

    I am having problems incrementing the counter
    and taking it back to the beginning.

    Any help much appreciated.

    This is my script:
    ( It includes test data )

    PHP Code:
    <?php 
    ini_set 
    ("display_errors""1");
    error_reporting(E_ALL);

    set_time_limit(10);
    function 
    inStr($needle$haystack){
        return @
    strpos($haystack$needle) !== false;
        }

    function 
    str_replaceFirst($s,$r,$str){
        
    $l strlen($str);
        
    $a strpos($str,$s);
        
    $b $a strlen($s);
        
    $temp substr($str,0,$a) . $r substr($str,$b,($l-$b));
        return 
    $temp;
        }

    function 
    sqn_select($pass){
       
    $mytext $pass;
       
    $idx 0;
       while(
    inStr("}",$mytext)){
           
    $rbracket strpos($mytext,"}",0);
           
    $tString substr($mytext,0,$rbracket);
           
    $tStringToken explode("{",$tString);
           
    $tStringCount count($tStringToken) - 1;
           
    $tString $tStringToken[$tStringCount];
           
    $tStringToken explode("|",$tString);
           
    $tStringCount count($tStringToken) - 1;
           
    $replace $tStringToken[$idx++];
           
    $tString "{".$tString."}";
           
    $mytext str_replaceFirst($tString,$replace,$mytext);
       }
       return 
    $mytext;
    }    

    $groups 10;
    $Db_data ="
    {A_one|A_two|A_three|A_four|A_five}
    {B-one|B-two|B-three|B-four|B-five|B-six|B-seven|B-eight|B-nine|B-ten|B-eleven|B-twelve}
    {C-one|C-two|C-three}
    {D-one|D-two|D-three|D-four|D-five|D-six|D-seven}
    {E-one|E-two|E-three|E-four|E-five|E-six|E-seven|E-eight|E-nine|E-ten}
    {F-one|F-two|F-three|F-four}
    {G-one|G-two}
    {H-one|H-two|H-three|H-four|H-five|H-six|H-seven|H-eight|H-nine}
    "
    ;

        for (
    $grp1$grp<= $groups  $grp++) {
        
            
    $Db_data str_replace("'","`",$Db_data);
            
            
    $new_group sqn_select($Db_data);
            
    $new_group str_replace(chr(13).chr(10),"<br />".chr(13).chr(10),$new_group);
                
            
    $wordCount explode(" ",$new_group);
            
    $wordCount count($wordCount);
            
            echo 
    "<br>Created New Group: $grp<br>$new_group<br><br>";
                            
            } 
    // end for
            
          
    echo "<br><b>Total $grp New Groups created</b><br>";
    ?>
    My desired output from the included test data is:

    A_one B-one C-one D-one E-one F-one G-one H-one
    A_two B-two C-two D-two E-two F-two G-two H-two
    A_three B-three C-three D-three E-three F-three G-one H-three ---> notice G goes back to one because there are only 2 elements
    A_four B-four C-one D-four E-four F-four G-two H-four ---> notice C goes back to one because there are only 3 elements
    A_five B-five C-two D-five E-five F-one G-one H-five
    A_one B-six C-three D-six E-six F-two G-two H-six ---> notice A goes back to one because there are only 5 elements
    A_two B-seven C-one D-seven E-seven F-three G-one H-seven
    A_three B-eight C-two D-one E-eight F-four G-two H-eight
    A_four B-nine C-three D-two E-nine F-one G-one H-nine
    A_five B-ten C-one D-three E-ten F-two G-two H-one

    Thanks for any help



    .
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    This is how I would do it:

    index.php
    PHP Code:
    <?php

        $groups  
    10;
        
    $Db_data =
            
    '{A_one|A_two|A_three|A_four|A_five}' .
            
    '{B-one|B-two|B-three|B-four|B-five|B-six|B-seven|B-eight|B-nine|B-ten|B-eleven|B-twelve}' .
            
    '{C-one|C-two|C-three}' .
            
    '{D-one|D-two|D-three|D-four|D-five|D-six|D-seven}' .
            
    '{E-one|E-two|E-three|E-four|E-five|E-six|E-seven|E-eight|E-nine|E-ten}' .
            
    '{F-one|F-two|F-three|F-four}' .
            
    '{G-one|G-two}' .
            
    '{H-one|H-two|H-three|H-four|H-five|H-six|H-seven|H-eight|H-nine}'
        
    ;

        
    //CREATE MANAGABLE SETS
        
    $sets = array();
        foreach (
    explode('}'trim($Db_data)) as $i) {
            if (
    $j trim(array_pop(explode('{'$i)))) {
                
    $sets[] = explode('|'$j);
            }
        }

        
    //STORE CURRENT SET MEMBER IN A NEW GROUP, AND ROTATE EACH SET PER CYCLE
        
    for ($i 0$i $groups; ++$i) {
            
    $group = array();
            for (
    $j 0$j count($sets); ++$j) {
                
    $group[] = current($sets[$j]);
                if (
    next($sets[$j]) === false) {reset($sets[$j]);}
            }
            echo 
    join(' '$group) . '<br />'//THE GROUP IS OUTPUTTED HERE
        
    }

    ?>
    Output

    Code:
    A_one B-one C-one D-one E-one F-one G-one H-one
    A_two B-two C-two D-two E-two F-two G-two H-two
    A_three B-three C-three D-three E-three F-three G-one H-three
    A_four B-four C-one D-four E-four F-four G-two H-four
    A_five B-five C-two D-five E-five F-one G-one H-five
    A_one B-six C-three D-six E-six F-two G-two H-six
    A_two B-seven C-one D-seven E-seven F-three G-one H-seven
    A_three B-eight C-two D-one E-eight F-four G-two H-eight
    A_four B-nine C-three D-two E-nine F-one G-one H-nine
    A_five B-ten C-one D-three E-ten F-two G-two H-one
    Last edited by bionoid; 11-07-2013 at 05:13 AM. Reason: Changed a foreach to a normal for, and added comments
    JavaScript: Learn | Validate | Compact

  3. #3
    Join Date
    May 2006
    Posts
    245
    WOW !!

    That looks good - I will check it out
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  4. #4
    Join Date
    May 2006
    Posts
    245
    Looks absolutely perfect,
    thank you so much :=)
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  5. #5
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    You're welcome. It was fun anyway.
    JavaScript: Learn | Validate | Compact

  6. #6
    Join Date
    May 2006
    Posts
    245
    Now I have looked at the results ...

    Initially I thought that my result was achieved because
    am trying to get a well-shuffled collection of almost unique groups.

    However, it only appears that way because my data sets all contain
    different numbers of elements.

    If the sets contain the same number - then the groups become less well shuffled,
    I then get pairs ( or triplets etc ) that stay in all groups.


    The reason for stepping through the sets was to try and ensure
    that every datapoint is used. i.e to systematically use all the combinations
    of the sets.

    ( I was using random but this often does not use all data points and sometimes
    uses one datapoint 3 or 4 times )

    Now I realize that all sets that contain the same number of elements will always
    be paired up..

    I changed the data sets so I can compare more easily :

    AND I have added some more rows which contain 4 data points.

    ( F , I and J all contain 4 elements )

    $Db_data =
    '{A_01|A_02|A_03|A_04|A_05}' .
    '{B-01|B-02|B-03|B-04|B-05|B-06|B-07|B-08|B-09|B-10|B-11|B-12}' .
    '{C-01|C-02|C-03}' .
    '{D-01|D-02|D-03|D-04|D-05|D-06|D-07}' .
    '{E-01|E-02|E-03|E-04|E-05|E-06|E-07|E-08|E-09|E-10}' .
    '{F-01|F-02|F-03|F-04}' .
    '{G-01|G-02}' .
    '{H-01|H-02|H-03|H-04|H-05|H-06|H-07|H-08|H-09}' .
    '{I-01|I-02|I-03|I-04}' .
    '{J-01|J-02|J-03|J-04}'
    ;

    Now when I run the script for 50 groups, I get:

    0) A_01 B-01 C-01 D-01 E-01 F-01 G-01 H-01 I-01 J-01
    1) A_02 B-02 C-02 D-02 E-02 F-02 G-02 H-02 I-02 J-02
    2) A_03 B-03 C-03 D-03 E-03 F-03 G-01 H-03 I-03 J-03
    3) A_04 B-04 C-01 D-04 E-04 F-04 G-02 H-04 I-04 J-04
    4) A_05 B-05 C-02 D-05 E-05 F-01 G-01 H-05 I-01 J-01
    5) A_01 B-06 C-03 D-06 E-06 F-02 G-02 H-06 I-02 J-02
    6) A_02 B-07 C-01 D-07 E-07 F-03 G-01 H-07 I-03 J-03
    7) A_03 B-08 C-02 D-01 E-08 F-04 G-02 H-08 I-04 J-04
    8) A_04 B-09 C-03 D-02 E-09 F-01 G-01 H-09 I-01 J-01
    9) A_05 B-10 C-01 D-03 E-10 F-02 G-02 H-01 I-02 J-02
    10) A_01 B-11 C-02 D-04 E-01 F-03 G-01 H-02 I-03 J-03
    11) A_02 B-12 C-03 D-05 E-02 F-04 G-02 H-03 I-04 J-04
    12) A_03 B-01 C-01 D-06 E-03 F-01 G-01 H-04 I-01 J-01
    13) A_04 B-02 C-02 D-07 E-04 F-02 G-02 H-05 I-02 J-02
    14) A_05 B-03 C-03 D-01 E-05 F-03 G-01 H-06 I-03 J-03
    15) A_01 B-04 C-01 D-02 E-06 F-04 G-02 H-07 I-04 J-04
    16) A_02 B-05 C-02 D-03 E-07 F-01 G-01 H-08 I-01 J-01
    17) A_03 B-06 C-03 D-04 E-08 F-02 G-02 H-09 I-02 J-02
    18) A_04 B-07 C-01 D-05 E-09 F-03 G-01 H-01 I-03 J-03
    19) A_05 B-08 C-02 D-06 E-10 F-04 G-02 H-02 I-04 J-04
    20) A_01 B-09 C-03 D-07 E-01 F-01 G-01 H-03 I-01 J-01
    21) A_02 B-10 C-01 D-01 E-02 F-02 G-02 H-04 I-02 J-02
    22) A_03 B-11 C-02 D-02 E-03 F-03 G-01 H-05 I-03 J-03
    23) A_04 B-12 C-03 D-03 E-04 F-04 G-02 H-06 I-04 J-04
    24) A_05 B-01 C-01 D-04 E-05 F-01 G-01 H-07 I-01 J-01
    25) A_01 B-02 C-02 D-05 E-06 F-02 G-02 H-08 I-02 J-02
    26) A_02 B-03 C-03 D-06 E-07 F-03 G-01 H-09 I-03 J-03
    27) A_03 B-04 C-01 D-07 E-08 F-04 G-02 H-01 I-04 J-04
    28) A_04 B-05 C-02 D-01 E-09 F-01 G-01 H-02 I-01 J-01
    29) A_05 B-06 C-03 D-02 E-10 F-02 G-02 H-03 I-02 J-02
    30) A_01 B-07 C-01 D-03 E-01 F-03 G-01 H-04 I-03 J-03
    31) A_02 B-08 C-02 D-04 E-02 F-04 G-02 H-05 I-04 J-04
    32) A_03 B-09 C-03 D-05 E-03 F-01 G-01 H-06 I-01 J-01
    33) A_04 B-10 C-01 D-06 E-04 F-02 G-02 H-07 I-02 J-02
    34) A_05 B-11 C-02 D-07 E-05 F-03 G-01 H-08 I-03 J-03
    35) A_01 B-12 C-03 D-01 E-06 F-04 G-02 H-09 I-04 J-04
    36) A_02 B-01 C-01 D-02 E-07 F-01 G-01 H-01 I-01 J-01
    37) A_03 B-02 C-02 D-03 E-08 F-02 G-02 H-02 I-02 J-02
    38) A_04 B-03 C-03 D-04 E-09 F-03 G-01 H-03 I-03 J-03
    39) A_05 B-04 C-01 D-05 E-10 F-04 G-02 H-04 I-04 J-04
    40) A_01 B-05 C-02 D-06 E-01 F-01 G-01 H-05 I-01 J-01
    41) A_02 B-06 C-03 D-07 E-02 F-02 G-02 H-06 I-02 J-02
    42) A_03 B-07 C-01 D-01 E-03 F-03 G-01 H-07 I-03 J-03
    43) A_04 B-08 C-02 D-02 E-04 F-04 G-02 H-08 I-04 J-04
    44) A_05 B-09 C-03 D-03 E-05 F-01 G-01 H-09 I-01 J-01
    45) A_01 B-10 C-01 D-04 E-06 F-02 G-02 H-01 I-02 J-02
    46) A_02 B-11 C-02 D-05 E-07 F-03 G-01 H-02 I-03 J-03
    47) A_03 B-12 C-03 D-06 E-08 F-04 G-02 H-03 I-04 J-04
    48) A_04 B-01 C-01 D-07 E-09 F-01 G-01 H-04 I-01 J-01
    49) A_05 B-02 C-02 D-01 E-10 F-02 G-02 H-05 I-02 J-02
    50) A_01 B-03 C-03 D-02 E-01 F-03 G-01 H-06 I-03 J-03

    Now we see that F, I and J are always in step.

    The idea is to systematically use all the combinations and avoid
    duplicating the patterns.


    But I am not sure how to do this

    Maybe something like ...

    0) A_01 B-01 C-01 D-01 E-01 F-01 G-01 H-01 I-01 J-01
    1) A_02 B-02 C-02 D-02 E-02 F-02 G-02 H-02 I-02 J-02
    2) A_03 B-03 C-03 D-03 E-03 F-03 G-01 H-03 I-03 J-03
    3) A_04 B-04 C-01 D-04 E-04 F-04 G-02 H-04 I-04 J-04
    4) A_05 B-05 C-02 D-05 E-05 F-01 G-01 H-05 I-02 J-03 < -- I start position increased by 1 and J by 2
    5) A_01 B-06 C-03 D-06 E-06 F-02 G-02 H-06 I-03 J-04
    6) A_02 B-07 C-01 D-07 E-07 F-03 G-01 H-07 I-04 J-01
    7) A_03 B-08 C-02 D-01 E-08 F-04 G-02 H-08 I-01 J-02
    8) A_04 B-09 C-03 D-02 E-09 F-01 G-01 H-09 I-02 J-03
    9) A_05 B-10 C-01 D-03 E-10 F-02 G-02 H-01 I-03 J-04
    10) A_01 B-11 C-02 D-04 E-01 F-03 G-01 H-02 I-04 J-01

    Probably by incrementing the start position of sets that have the
    same number of elements, the pairing could be broken up.

    Or maybe there is a better logic to use for systematically
    going through all combinations of these sets ???

    Thanks for helping.



    .
    Last edited by jeddik; 11-08-2013 at 01:12 AM.
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  7. #7
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    How I did something similar in the past was, using the data like clock pieces. You rotate the last point until it reaches its limit, then you rotate the point before it etc etc until finally you've rotated the initial point to the limit, resulting in all possible combinations being used.

    A_01 B_01 C_01
    A_01 B_01 C_02
    A_01 B_02 C_01
    A_01 B_02 C_02
    A_02 B_01 C_01
    A_02 B_01 C_02
    A_02 B_02 C_01
    A_02 B_02 C_02
    --End
    JavaScript: Learn | Validate | Compact

  8. #8
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Here is an example using a much smaller dataset, creating unique groups:

    index.php
    PHP Code:
    <?php

        $groups  
    24//ONLY 24 POSSIBLE COMBINATIONS WITH THE CURRENT DATA
        
    $Db_data =
            
    '{A_01|A_02}' .
            
    '{B_01|B_02|B_03|B_04}' .
            
    '{C_01|C_02|C_03}'
        
    ;

        
    //CREATE MANAGABLE SETS
        
    $sets = array();
        foreach (
    explode('}'trim($Db_data)) as $i) {
            if (
    $j trim(array_pop(explode('{'$i)))) {
                
    $sets[] = explode('|'$j);
            }
        }

        
    //STORE CURRENT SET MEMBER IN A NEW GROUP, ROTATE INITIAL SET AND CASCADE
        
    for ($i 0$i $groups; ++$i) {
            
    $group = array();
            for (
    $j 0$k 1$j count($sets); ++$j) {
                
    $group[] = current($sets[$j]);
                if (
    $k && next($sets[$j]) === false) {reset($sets[$j]); $k 1;} else {$k 0;}
            }
            echo 
    str_pad($i 12'0'STR_PAD_LEFT) . ') ' join(' '$group) . '<br />'//THE GROUP IS OUTPUTTED HERE
        
    }

    ?>
    Output

    Code:
    01) A_01 B_01 C_01
    02) A_02 B_01 C_01
    03) A_01 B_02 C_01
    04) A_02 B_02 C_01
    05) A_01 B_03 C_01
    06) A_02 B_03 C_01
    07) A_01 B_04 C_01
    08) A_02 B_04 C_01
    09) A_01 B_01 C_02
    10) A_02 B_01 C_02
    11) A_01 B_02 C_02
    12) A_02 B_02 C_02
    13) A_01 B_03 C_02
    14) A_02 B_03 C_02
    15) A_01 B_04 C_02
    16) A_02 B_04 C_02
    17) A_01 B_01 C_03
    18) A_02 B_01 C_03
    19) A_01 B_02 C_03
    20) A_02 B_02 C_03
    21) A_01 B_03 C_03
    22) A_02 B_03 C_03
    23) A_01 B_04 C_03
    24) A_02 B_04 C_03
    Last edited by bionoid; 11-08-2013 at 02:19 AM.
    JavaScript: Learn | Validate | Compact

  9. #9
    Join Date
    May 2006
    Posts
    245
    OK - I see what you have done !

    You are very good at this !!

    While technically each group generated by this script is unique
    because one element is different, the resulting groups are in fact
    all extremely similar.

    ( this is even more pronouced with larger data sets of course)

    The previous run produced a much better "shuffled" pack of data groups

    That was because ALL of the row elements changed.

    In fact it was great ! -
    except for where the number of elements in the sets was the same.

    The only thing we needed to change was to stagger - or somehow change -
    the looping of those sets with the same quantity of elements.

    Actually this will result in a much lower number of possible combinations
    than this new script - but the resulting data groups will be much more diverse.

    ( I know it is my fault - this statement "The idea is to systematically use
    all the combinations and avoid duplicating the patterns." made it look like
    I just wanted a technically unique groups, but what I really want is:as many
    "dis-similar" groups as possible. The first script was nearly there !! )

    Thank you VERY much again !

    Is it possible to use something from the second script inside the first script to
    effect the cycling of the sets that have the same number of elements ??


    .
    Last edited by jeddik; 11-09-2013 at 01:25 PM.
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  10. #10
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Give me a moment... I have an idea.
    JavaScript: Learn | Validate | Compact

  11. #11
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    OK, This creates completely random unique groups:

    index.php
    PHP Code:
    <?php

        $groups  
    10;
        
    $Db_data =
            
    '{A_01|A_02|A_03|A_04|A_05}' .
            
    '{B-01|B-02|B-03|B-04|B-05|B-06|B-07|B-08|B-09|B-10|B-11|B-12}' .
            
    '{C-01|C-02|C-03}' .
            
    '{D-01|D-02|D-03|D-04|D-05|D-06|D-07}' .
            
    '{E-01|E-02|E-03|E-04|E-05|E-06|E-07|E-08|E-09|E-10}' .
            
    '{F-01|F-02|F-03|F-04}' .
            
    '{G-01|G-02}' .
            
    '{H-01|H-02|H-03|H-04|H-05|H-06|H-07|H-08|H-09}' .
            
    '{I-01|I-02|I-03|I-04}' .
            
    '{J-01|J-02|J-03|J-04}'
        
    ;

        
    //CREATE MANAGABLE SETS
        
    $sets = array();
        foreach (
    explode('}'trim($Db_data)) as $i) {
            if (
    $j trim(array_pop(explode('{'$i)))) {
                
    $sets[] = explode('|'$j);
            }
        }

        
    //PERFORM INITIAL SET SHUFFLING
        
    for ($i 0$i count($sets); ++$i) {shuffle($sets[$i]);}

        
    //STORE CURRENT SET MEMBER IN A NEW GROUP, AND ROTATE EACH SET PER CYCLE
        
    $uniques = array();
        while (
    count($uniques) < $groups) {
            
    $k = array();
            for (
    $j 0$j count($sets); ++$j) {
                
    $k[] = current($sets[$j]);
                if (
    next($sets[$j]) === false) {reset($sets[$j]); shuffle($sets[$j]);}
            }
            
    $group join(' '$k);
            if (!isset(
    $uniques[$group])) {$uniques[$group] = 1;
                echo 
    str_pad(count($uniques), 2'0'STR_PAD_LEFT) . ') ' $group '<br />'//THE GROUP IS OUTPUTTED HERE
            
    }
        }

    ?>
    Output, run 1:

    Code:
    01) A_03 B-11 C-02 D-03 E-07 F-01 G-01 H-02 I-02 J-03
    02) A_01 B-08 C-01 D-05 E-05 F-03 G-02 H-05 I-03 J-04
    03) A_05 B-06 C-03 D-06 E-01 F-02 G-01 H-07 I-04 J-01
    04) A_02 B-02 C-02 D-04 E-03 F-04 G-02 H-08 I-01 J-02
    05) A_04 B-04 C-01 D-07 E-02 F-04 G-01 H-04 I-03 J-03
    06) A_03 B-01 C-03 D-01 E-08 F-03 G-02 H-06 I-01 J-02
    07) A_01 B-03 C-02 D-02 E-04 F-01 G-01 H-09 I-04 J-04
    08) A_02 B-09 C-01 D-07 E-06 F-02 G-02 H-03 I-02 J-01
    09) A_05 B-05 C-03 D-06 E-10 F-02 G-01 H-01 I-03 J-04
    10) A_04 B-07 C-02 D-02 E-09 F-03 G-02 H-08 I-02 J-02
    Output, run 2:

    Code:
    01) A_05 B-02 C-02 D-01 E-05 F-02 G-02 H-09 I-01 J-02
    02) A_03 B-06 C-03 D-05 E-02 F-03 G-01 H-02 I-04 J-04
    03) A_01 B-05 C-01 D-07 E-03 F-01 G-02 H-06 I-02 J-03
    04) A_04 B-08 C-01 D-06 E-07 F-04 G-01 H-04 I-03 J-01
    05) A_02 B-09 C-03 D-04 E-09 F-01 G-02 H-05 I-04 J-01
    06) A_03 B-11 C-02 D-02 E-08 F-04 G-01 H-08 I-03 J-02
    07) A_04 B-07 C-01 D-03 E-01 F-03 G-01 H-03 I-01 J-03
    08) A_01 B-03 C-02 D-07 E-06 F-02 G-02 H-01 I-02 J-04
    09) A_02 B-04 C-03 D-03 E-10 F-04 G-01 H-07 I-02 J-02
    10) A_05 B-12 C-02 D-05 E-04 F-02 G-02 H-07 I-03 J-01
    Last edited by bionoid; 11-09-2013 at 02:02 PM.
    JavaScript: Learn | Validate | Compact

  12. #12
    Join Date
    May 2006
    Posts
    245
    VERY interesting !

    I do have a function for random selection:

    PHP Code:
    function inStr($needle$haystack){
        return @
    strpos($haystack$needle) !== false;
        }

    function 
    str_replaceFirst($s,$r,$str){
        
    $l strlen($str);
        
    $a strpos($str,$s);
        
    $b $a strlen($s);
        
    $temp substr($str,0,$a) . $r substr($str,$b,($l-$b));
        return 
    $temp;
        }

    function 
    rand_select($pass){
        
    $mytext $pass;
        while(
    inStr("}",$mytext)){
            
    $rbracket strpos($mytext,"}",0);
            
    $tString substr($mytext,0,$rbracket);
            
    $tStringToken explode("{",$tString);
            
    $tStringCount count($tStringToken) - 1;
            
    $tString $tStringToken[$tStringCount];
            
    $tStringToken explode("|",$tString);
            
    $tStringCount count($tStringToken) - 1;
            
    $i rand(0,$tStringCount);
            
    $replace $tStringToken[$i];
            
    $tString "{".$tString."}";
            
    $mytext str_replaceFirst($tString,$replace,$mytext);
        }
        return 
    $mytext;
        }

    $Db_text ="
    {A_one|A_two|A_three|A_four|A_five}
    {B-one|B-two|B-three|B-four|B-five|B-six|B-seven|B-eight|B-nine|B-ten|B-eleven|B-twelve}
    {C-one|C-two|C-three}
    {D-one|D-two|D-three|D-four|D-five|D-six|D-seven}
    {E-one|E-two|E-three|E-four|E-five|E-six|E-seven|E-eight|E-nine|E-ten}
    {F-one|F-two|F-three|F-four}
    {G-one|G-two}
    {H-one|H-two|H-three|H-four|H-five|H-six|H-seven|H-eight|H-nine}
    "
    ;

    $groups 100;
    $Db_rand_cd 'R';

    if(
    $Db_rand_cd == 'R') {
            for (
    $i 0$i $groups; ++$i) {

                
    $filename "{$i}.txt";
                
    $filename $path.$filename

                
    $Db_text str_replace("'","`",$Db_text);
            
                
    $selected_text rand_select($Db_text);
                
    $selected_text str_replace(chr(13).chr(10),"<br />".chr(13).chr(10),$selected_text );
                
                
    $selected_text wordwrap($selected_text 70"\n");     
                
    file_put_contents($filename,$selected_text );
                 
                
    $wordCount explode(" ",$selected_text );
                
    $wordCount count($wordCount);
                        
                echo 
    "<br>Created File: $filename, Word Count: $wordCount<br>$selected_text <br>";    
                }
            } 
    This was working OK
    ( it also puts the results into separate files )

    But I noticed that often the rand() would chose the same
    element several times and omit selecting some of the elements altogether.

    Just like rolling a die - you may get the same result several times in a row.
    ( and you could wait a long time to throw a six !!! )

    ( In theory if you throw the die 100 times you should get a six
    about 17 times ... but in practice you could get it 40 times or zero )

    So I was trying to come up with a systematic way of using all
    the elements and produce a more even spread of the data.

    In other words: a better result that the random generator does.

    Maybe your use of the shuffle() function produces a better result ???

    The other advantage of building a systematic "dis-similar" generator
    is that it should be possible to calculate the number of combinations and
    output that number of groups - rather arbitrarily picking a number like 100.

    Does what I am saying make any sense ?

    I think we ( well you !!) were nearly there with the first script.
    Is it possible to modify that one to effect the looping of same-quantity
    data sets ?

    Thanks.



    .
    Last edited by jeddik; 11-09-2013 at 03:57 PM.
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

  13. #13
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    If you look at the resulting data, each data point in every set is always used before the next re-shuffle happens.
    So its not just a simple random point per cycle.

    Looking at the "A" set, each point is clearly used without any omissions:

    Code:
    01) A_05 B-03
    02) A_03 B-02
    03) A_01 B-12
    04) A_02 B-08
    05) A_04 B-09
    --- Reshuffled
    06) A_01 B-10
    07) A_05 B-11
    08) A_03 B-01
    09) A_02 B-07
    10) A_04 B-05
    --- Reshuffled
    11) A_05 B-04
    12) A_03 B-06
    13) A_01 B-09
    14) A_04 B-01
    15) A_03 B-04
    Last edited by bionoid; 11-09-2013 at 03:57 PM.
    JavaScript: Learn | Validate | Compact

  14. #14
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    OK, this is the last idea that's currently come to mind.
    This time I record the set lengths, and if there was a proceeding set with the same length I shift the data points X amount of times.
    So the resulting data is still predictable.

    index.php
    PHP Code:
    <?php

        $groups  
    20;
        
    $Db_data =
            
    '{A_01|A_02|A_03|A_04|A_05}' .
            
    '{B-01|B-02|B-03|B-04|B-05|B-06|B-07|B-08|B-09|B-10|B-11|B-12}' .
            
    '{C-01|C-02|C-03}' .
            
    '{D-01|D-02|D-03|D-04|D-05|D-06|D-07}' .
            
    '{E-01|E-02|E-03|E-04|E-05|E-06|E-07|E-08|E-09|E-10}' .
            
    '{F-01|F-02|F-03|F-04}' .
            
    '{G-01|G-02}' .
            
    '{H-01|H-02|H-03|H-04|H-05|H-06|H-07|H-08|H-09}' .
            
    '{I-01|I-02|I-03|I-04}' .
            
    '{J-01|J-02|J-03|J-04}'
        
    ;

        
    //CREATE MANAGABLE SETS
        
    $sets = array();
        foreach (
    explode('}'trim($Db_data)) as $i) {
            if (
    $j trim(array_pop(explode('{'$i)))) {
                
    $sets[] = explode('|'$j);
            }
        }

        
    //STORE CURRENT SET MEMBER IN A NEW GROUP, AND ROTATE EACH SET PER CYCLE
        
    for ($i 0$i $groups; ++$i) {
            
    $group = array();
            
    $sizes = array();
            for (
    $j 0$j count($sets); ++$j) {
                
    $group[] = current($sets[$j]);
                
    $k count($sets[$j]); $sizes[$k] = (isset($sizes[$k]) ? $sizes[$k] : 0) + 1;
                if (
    next($sets[$j]) === false) {reset($sets[$j]);
                    if (
    $sizes[$k] > 1) {
                        for (
    $a 0$a < ($sizes[$k] - 1); ++$a) {array_push($sets[$j], array_shift($sets[$j]));}
                    }
                }
            }
            echo 
    str_pad($i 12'0'STR_PAD_LEFT) . ') ' join(' '$group) . '<br />'//THE GROUP IS OUTPUTTED HERE
        
    }

    ?>
    Output

    Code:
    01) A_01 B-01 C-01 D-01 E-01 F-01 G-01 H-01 I-01 J-01
    02) A_02 B-02 C-02 D-02 E-02 F-02 G-02 H-02 I-02 J-02
    03) A_03 B-03 C-03 D-03 E-03 F-03 G-01 H-03 I-03 J-03
    04) A_04 B-04 C-01 D-04 E-04 F-04 G-02 H-04 I-04 J-04
    05) A_05 B-05 C-02 D-05 E-05 F-01 G-01 H-05 I-02 J-03
    06) A_01 B-06 C-03 D-06 E-06 F-02 G-02 H-06 I-03 J-04
    07) A_02 B-07 C-01 D-07 E-07 F-03 G-01 H-07 I-04 J-01
    08) A_03 B-08 C-02 D-01 E-08 F-04 G-02 H-08 I-01 J-02
    09) A_04 B-09 C-03 D-02 E-09 F-01 G-01 H-09 I-03 J-01
    10) A_05 B-10 C-01 D-03 E-10 F-02 G-02 H-01 I-04 J-02
    11) A_01 B-11 C-02 D-04 E-01 F-03 G-01 H-02 I-01 J-03
    12) A_02 B-12 C-03 D-05 E-02 F-04 G-02 H-03 I-02 J-04
    13) A_03 B-01 C-01 D-06 E-03 F-01 G-01 H-04 I-04 J-03
    14) A_04 B-02 C-02 D-07 E-04 F-02 G-02 H-05 I-01 J-04
    15) A_05 B-03 C-03 D-01 E-05 F-03 G-01 H-06 I-02 J-01
    16) A_01 B-04 C-01 D-02 E-06 F-04 G-02 H-07 I-03 J-02
    17) A_02 B-05 C-02 D-03 E-07 F-01 G-01 H-08 I-01 J-01
    18) A_03 B-06 C-03 D-04 E-08 F-02 G-02 H-09 I-02 J-02
    19) A_04 B-07 C-01 D-05 E-09 F-03 G-01 H-01 I-03 J-03
    20) A_05 B-08 C-02 D-06 E-10 F-04 G-02 H-02 I-04 J-04
    JavaScript: Learn | Validate | Compact

  15. #15
    Join Date
    May 2006
    Posts
    245
    I think you must be a genius !

    It looks like both of those options will produce good
    results - better than the random function I had.

    I will do testing with both.

    Thank you so much.
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles