Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

comparing algorithm

  • 15-01-2009 02:47PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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