Find value in data columns

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
geo
Posts: 31
Joined: Sun Aug 04, 2019 7:28 pm

Find value in data columns

Post by geo » Mon Dec 23, 2019 8:22 pm

Hello
Is there a way to find the value closest to 2755 in column 1 and return the value from column 2?
Kind of like the match/index functions in excel

Thanks for any help

2780.5273 7010.989
2765.9229 7494.5054
2748.073 7978.022
2731.846 8498.732
2717.2415 8963.651
2699.3916 9484.361
2679.919 9967.878
2627.992 10507.186
2576.065 10972.1045
2529.006 11474.218
2475.4563 11976.331

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Find value in data columns

Post by richmond62 » Mon Dec 23, 2019 9:15 pm

Are those numbers in:

1. A space/tab delimited text document -> scrolling list field.

2. A table field,

3. A data grid?

geo
Posts: 31
Joined: Sun Aug 04, 2019 7:28 pm

Re: Find value in data columns

Post by geo » Mon Dec 23, 2019 9:19 pm

the numbers can be in any of the three, whatever works best

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Find value in data columns

Post by richmond62 » Mon Dec 23, 2019 9:39 pm

I put your data into a scrolling list field and did what was, frankly, some baby Maths:
-
Screenshot 2019-12-23 at 22.33.43.png
-
Button "DIFF from 2755":

Code: Select all

on mouseUp
   set the itemdelimiter to tab
   put 1 into KOUNT
   repeat until line KOUNT of fld "rawDATA" is empty
      put item 1 of line KOUNT of fld "rawDATA" into XXX
      put (2755 - XXX) into ZZZ
      if ZZZ < 0 then
         put (ZZZ * (-1)) into ZZZ
      end if
      put (line KOUNT of fld "rawDATA") & tab & ZZZ into line KOUNT of fld "cooked"
      add 1 to KOUNT
   end repeat
end mouseUp
Button "CLOSEST to 2755":

Code: Select all

on mouseUP
   set the itemDelimiter to tab
   sort lines of fld "cooked" by item 3 of each
   put item 2 of line 1 of fld "cooked" into fld "fRESULT"
end mouseUP
Attachments
geo.livecode.zip
Here's the stack.
(1.48 KiB) Downloaded 169 times

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Find value in data columns

Post by dunbarx » Mon Dec 23, 2019 11:37 pm

Hi.

Two ways I would do it. Step through each and see which one resonates with you. Given your data in a field 1:

Code: Select all

on mouseUp
   get fld 1   
   repeat with y = 1 to the number of lines of it
      put abs(word 1 of line y of it - 2755) & "," && word 2 of line y of it & return after temp
   end repeat 
   sort temp numeric  by item 1 of each
   answer line 1 of temp
end mouseUp

on mouseUp
   get fld 1
   repeat for each line tLine in it
      put abs(word 1 of tLine - 2755) into closestvalue[word 2 of tLine]
   end repeat
combine closestvalue with return and space
sort closestvalue numeric by word 2 of each
answer line 1 of closestvalue
end mouseUp
A word of advice. Put a delimiter that is easily identified to delimit your data. It took me a bit to determine that you were using spaces. Commas or tabs are much better. I added commas in the first example.

Craig

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

Re: Find value in data columns

Post by [-hh] » Mon Dec 23, 2019 11:57 pm

You could use a sort function (shortens Craig's first handler a little bit):
Given your data is in field "IN".

Code: Select all

on mouseUp
  answer closestValue(2755, fld "IN")
end mouseUp

function closestValue pVal, pStr
  sort pStr numeric by abs(pVal - word 1 of each)
  return word 2 of line 1 of pStr
end closestValue  
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Find value in data columns

Post by dunbarx » Tue Dec 24, 2019 4:53 am

Hermann.

Very sleek.

Craig

geo
Posts: 31
Joined: Sun Aug 04, 2019 7:28 pm

Re: Find value in data columns

Post by geo » Tue Dec 24, 2019 8:23 pm

Thanks guys, that put me on the right track

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”