Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

comparing algorithm

Options
  • 15-01-2009 2:47pm
    #1
    Registered Users Posts: 8,070 ✭✭✭


    2 Csv' , i want to print out fields that do not exist in the second list [sent.csv]

    [PHP] <?php

    $orig = file("orig.csv");
    $sent= file("sent.csv");




    for ($i = 0; $i<5; $i++){

    $temp = $orig[$i];

    for ($j = 0; $j<5; $j++){
    if($temp != $sent[$j])
    {
    $a = $temp;
    }
    else{}

    }
    echo "$a<br>";
    $a = null;

    }

    ?>[/PHP]

    That doesn't work as its severely flawed, how would i compare the first field [eg $temp] with all of the fields in the second csv...


Comments

  • Registered Users Posts: 2,781 ✭✭✭amen


    within the loop for the first file you need to loop through the second file for each increment of the first file

    always write down the problem/pseudcode before writing code


  • Registered Users Posts: 8,070 ✭✭✭Placebo


    i thought thats what i was doing with the nested loop, no?

    thanks

    this works, but seems so fragile and put together with a string :(
    [PHP] <?php

    $orig = file("orig.csv");
    $sent= file("sent.csv");


    for ($i = 0; $i<7; $i++){
    $count = 1;
    $temp = $orig[$i];

    for ($j = 0; $j<3; $j++){
    if($temp != $sent[$j])
    {

    $count++;


    }
    else{}

    }
    if($count == 4)
    {
    $a = $temp;
    echo "$a<br>";
    }
    else{}


    }



    ?>[/PHP]


    edit: this doesnt work well on the real data, stops on reading a duplicate etc :/


  • Registered Users Posts: 8,070 ✭✭✭Placebo


    anyone ??


  • Closed Accounts Posts: 30 codecrunchers


    I think you should load both your files into a data structure and then run your algorithm from there. where are the 7 3 5 and loop indices coming from, post a copy of the .csv content


  • Registered Users Posts: 8,070 ✭✭✭Placebo


    i pumped them into and array and used this
    [PHP]$available = array_diff($array, $array3);[/PHP]
    and it worked.
    which is what i should have done but i think, there were some odd characters in the data which ended the code, as it ran fine for test data.

    But would that have mattered if i was just comparing mere strings?


  • Advertisement
Advertisement