Help with lists

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
neutrox
Posts: 194
Joined: 05 Mar 2012 15:23

Help with lists

Post by neutrox »

please:

seemed to me it was a trivial task but after few hours i need help...
how do I compare two lists and report found on list 2 without the use of formatlist sort/dedupe?
IOW it is important to keep the original sequence of occurrences in the final report.

list 1:
some stuff.txt
unsorted stuff.txt
neutral stuff.txt

list 2:
tasks to do.txt
some stuff.txt
readme.txt
reqs.txt
neutral stuff.txt
seen.txt

correct answer:
some stuff.txt
neutral stuff.txt

highend
Posts: 14955
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Help with lists

Post by highend »

Compare each entry in the left list with each entry in the right list and when a match is found, remove it from the right list.

Like this:
some stuff.txt

Compared with:
tasks to do.txt
some stuff.txt
readme.txt
reqs.txt
neutral stuff.txt
seen.txt

some stuff.txt is found, remove if from the right list and continue with unsorted stuff.txt from the left list.

it's only a foreach() with gettoken in the left listand a bit of regexereplace / replace in the right list.
One of my scripts helped you out? Please donate via Paypal

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Help with lists

Post by Stefan »

I don't know if i understand what you want to do, or where this lists came from.
But something like this should give you an idea:

Code: Select all

  $list1 = "some stuff.txt<crlf>
            unsorted stuff.txt<crlf>
            neutral stuff.txt";

  $list2 = "tasks to do.txt<crlf>
            some stuff.txt<crlf>
            readme.txt<crlf>
            reqs.txt<crlf>
            neutral stuff.txt<crlf>
            seen.txt"; 


  $out="";
  foreach($line, $list2, "<crlf>"){

         //IF $line from $list2 is found in $list1, i get an position start index equal or greater zero:
         if (  strpos($list1, $line, ,1) >= 0  ){  $out = "$out$line<crlf>";  }

  }

  // If wanted, some cosmetic: remove last added CRLF:
      $out = replace($out, "<crlf>", "", ,strlen($out)-5);

  msg $out;


/*
Output:
---------------------------
           XYplorer
---------------------------
        some stuff.txt
       neutral stuff.txt
---------------------------
             OK   
---------------------------

*/

HTH? :D


.

neutrox
Posts: 194
Joined: 05 Mar 2012 15:23

Re: Help with lists

Post by neutrox »

i have to understand quick that strpos thingie lol

many thanks to Stefan and highend that helped me a lot with this and other post

neutrox
Posts: 194
Joined: 05 Mar 2012 15:23

Re: Help with lists

Post by neutrox »

hope i'm not being abusive but is there a way for the script to to not output duplicated entries? I mean... if a string is found once, it being not reported a second time?
again, without the use of formatlist sort/dedupe as duplicates may not be immediate neighbors?

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Help with lists

Post by Stefan »

neutrox wrote:hope i'm not being abusive but is there a way for the script to to not output duplicated entries?
I mean... if a string is found once, it being not reported a second time?
again, without the use of formatlist sort/dedupe as duplicates may not be immediate neighbors?
Nema problemo.

It's a pity that formatlist dedupe works only on sorted lists.
// $out = formatlist($out, "d", "<crlf>");
// $out = formatlist($out, "sd", "<crlf>");

but we can do it ourself the same way as we did it above.... with the aid of strpos()

Code: Select all

  $list1 = "some stuff.txt<crlf>
            unsorted stuff.txt<crlf>
            neutral stuff.txt";

  $list2 = "tasks to do.txt<crlf>
            some stuff.txt<crlf>
            readme.txt<crlf>
            reqs.txt<crlf>
            some stuff.txt<crlf>
            neutral stuff.txt<crlf>
            some stuff.txt<crlf>
            seen.txt"; 


  $out=""; 
  foreach($line, $list2, "<crlf>"){

   if ( strpos($list1, $line, ,1) >= 0 ){ 

       //Add only if not already in $out:
       if ( strpos($out, $line, ,1) == -1 ){
           $out = "$out$line<crlf>"; }
       }

  }


  $out = replace($out, "<crlf>", "", ,strlen($out)-5);

  msg $out;

neutrox
Posts: 194
Joined: 05 Mar 2012 15:23

Re: Help with lists

Post by neutrox »

ive put a foreach (list 1) inside a foreach (list 2) inside a foreach (l1 == l2) { l3 against l1 or l2 },

then completely reversed the logics behind lists comparison just to end in the boondocks again :oops:

couldn't do without your help, guys!

Post Reply