Search through nested arrays

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: heatherlaine, Klaus, FourthWorld, kevinmiller

Post Reply
Posts: 2
Joined: Thu Jul 28, 2016 6:28 am

Search through nested arrays

Post by oellph » Sun Dec 03, 2017 5:42 pm

Hi all,

I've struggled to work this one out and finally need to turn to the community for help. I have a repeating array with many nested arrays (all fake data btw), where I want to extract the firstnames, lastnames and nhs number. The tricky part is that the nhs number is sometimes under the first nested array of [partyAddtionalInfo] and sometimes under the second.

My attempt, which works feels a bit clunky.

Code: Select all

  repeat for each Key temp in tParties
      repeat for each Key temp2 in tParties[temp][partyAdditionalInfo]
         repeat for each key temp3 in tParties[temp][partyAdditionalInfo][temp2]
            if tParties[temp][partyAdditionalInfo][temp2][temp3] is "uk.nhs.nhs_number" then
               put tParties[temp][partyAdditionalInfo][temp2]["value"] into tNHSNumber
            end if
         end repeat
      end repeat
      put tNHSNumber && tParties[temp][firstNames] && tParties[temp][lastNames] & cr after tPatientList
   end repeat
I was reading through this example ( ... dable-form) and I can almost 'sense' a better way, using a function which calls itself, but I can't quite figure it out.

I could leave my solution as is, but I'm keen to understand any better ways.


Posts: 308
Joined: Sun Apr 27, 2014 2:45 am

Re: Search through nested arrays

Post by Da_Elf » Mon Dec 11, 2017 9:55 pm

i dont usually use "arrays" i would just create my own lists with items in it. so to search for an entry based on a part of my list if would use something like this.

Code: Select all

local mylist
on filllist
   put empty into mylist
   put "bob" & tab & "hope" & tab & "1234" & cr after mylist
   put "jill" & tab & "walker" & tab & "2345" & cr after mylist
   put "mike" & tab & "young" & tab & "3456" & cr after mylist
   put "peter" & tab & "hadley" & tab & "4567" after mylist
end filllist

function searchlist thesearch
   put empty into mysearch
   set itemdelimiter to cr
   repeat for each item linecheck in mylist
      set itemdelimiter to tab
      if item 3 of linecheck is thesearch then
         put linecheck & cr after mysearch
      end if
      set itemdelimiter to cr
   end repeat
   delete the last char of mysearch
   return mysearch
end searchlist

on mouseup
   put the text of fld "search" into search
   answer searchlist(search)
end mouseup

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”