Comparing two lists based on a simple function

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Comparing two lists based on a simple function

Post by Da_Elf » Sat Oct 14, 2017 3:10 pm

List 1
1-6,11-13,15-22,23-30

List 2
4,7,8,12,20

I want to compare these two lists to pull out 4,12,20 from the 2nd list because they are inside the ranges in the first list. Do i have to use a long method of multiple repeats which id have to build a function for or is there a nice neat way to do it?

my guess is to loop through list 2 and ask if the item is <= item1 of line 1 of list 1 or >= item 2 of line 2 of list 1 and check every item in list 1 and if it proves true add it to a 3rd list. then go to the next item in list 2 and do the same again

my long way around

Code: Select all

put empty into list3
repeat for each item nitem in list2
	repeat for each item ritem in list1
		set itemDelimiter to "-"
		if nitem >= item 1 of ritem AND nitem <= item 2 of ritem then
			put nitem and comma after list3
		set itemDelimiter to comma
	end repeat
end repeat
delete the last char of list3

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Comparing two lists based on a simple function

Post by [-hh] » Sat Oct 14, 2017 6:32 pm

You could try the following.
Runs here with 10000 repeats in 95 millisecs average (LC 6.7.1, because you are using LC 6).

Code: Select all

function checkRanges listOfNumbers, listOfRanges
  set linedel to "-"
  repeat for each item N in listOfNumbers
    repeat for each item R in listOfRanges
      if N < line 1 of R then exit repeat
      if N > line 2 of R then next repeat
      put comma & N after lst
      exit repeat
    end repeat
  end repeat
  return char 2 to -1 of lst
end checkRanges
Works as long as the ranges are sorted numerically and are not overlapping.

If the list of numbers is very long and numerically sorted then one could try to use this by indexing the numbers but there will be a tradeoff because one must give up the "for each". Also, depending on the relation of the number of items of both lists, an outer loop over the list of ranges (and inner loop list of numbers) may be faster.
shiftLock happens

jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: Comparing two lists based on a simple function

Post by jiml » Sun Oct 15, 2017 6:54 pm

This also works:

Code: Select all

   put "1-6,11-13,15-22,23-30" into list1
   put "4,7,8,12,20" into list2
   put empty into list3
   repeat for each item nitem in list2
      repeat for each item ritem in list1
         if nitem = min(max(nitem,trueword 1 of ritem),trueword -1 of ritem) then
            put nitem & comma after list3
            exit repeat
         end if
      end repeat
   end repeat
   delete the last char of list3
   
Jim Lambert

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: Comparing two lists based on a simple function

Post by Da_Elf » Sun Oct 15, 2017 7:39 pm

More or less we have have just slightly different ways of doing it but its more or less the same.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”