Array help

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
edwardeddyus
Posts: 10
Joined: Mon Mar 25, 2013 7:17 pm

Array help

Post by edwardeddyus » Wed Jul 12, 2017 12:46 am

I created an array

Code: Select all

put "Purpose Data"  into ar[1]["purpose" ]
   put "Dose Data" into ar[2]["dose" ]
   put "Side Effects Data"  into ar[3]["side effects" ]
   put "Emergency Conditions Data" into ar[4]["Emergency cond" ]
   put "Abuse Potential Data" into ar[5]["Abuse Potential" ]
   put "Cautions Data" into ar[6]["Cautions" ]
but when I run

Code: Select all

   local counter
   put 0 into counter
   repeat for each key tKey in ar
      repeat for each key sKey in ar[tKey]
         //put the number of fields of group ar[tKey][sKey] into tCount
     end repeat
     put counter+1 into counter
    put sKey into btnnms[counter]
     answer counter & "=" & sKey
   end repeat
I get

6=Cautions
1=purpose
2=dose
3=side effects
4=Emergency cond
5=Abuse Potential

Why am I getting the last item in the array first?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9847
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Array help

Post by FourthWorld » Wed Jul 12, 2017 1:40 am

Associative arrays have no inherent sense of order. They are name-value pairs, regardless whether the name is numeric or alphanumeric.

If you need to sort keys or other values obtain from an array use the sort command.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Array help

Post by [-hh] » Wed Jul 12, 2017 3:19 am

Your script is correct and works here (both parts in one "MouseUp").
Probably you didn't reset the counter to 0 as above when you got the wrong result?
shiftLock happens

Post Reply

Return to “Talking LiveCode”