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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

comparing algorithm

  • 15-01-2009 1: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