Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
kaveh1000
- Livecode Opensource Backer
- Posts: 508
- Joined: Sun Dec 18, 2011 7:23 pm
- Location: London
-
Contact:
Post
by kaveh1000 » Sat Jan 04, 2020 11:50 pm
I have a mental block regarding arrays!
I have a multidimensional array that I created from a spreadsheet. So several rows and columns. The first column (sequential numbers) becomes the key. So I can say e.g.
and I would get
France. Then I say
and get
Paris. similarly for other data, e.g. population. What I actually want to do is to say:
Code: Select all
put Countries["France"]["Capital"]
and get
Paris. I have been scratching my head for a long time. How can I transform my array? I have tried to reset the key, but to no avail. Any education welcome!
Kaveh
Kaveh
-
mrcoollion
- Posts: 722
- Joined: Thu Sep 11, 2014 1:49 pm
- Location: The Netherlands
Post
by mrcoollion » Sun Jan 05, 2020 12:28 am
Would this work for you?
Code: Select all
repeat for each key tCountryNumber in Countries
put Countries [tCountryNumber] into taCountryInformation
put taCountryInformation["Country"] into tCountry
put taCountryInformation["Capital"] into tCapital
put tCapital into taCountries[tCountry]["Capital"]
end repeat
The array taCountries should have the array you want.
It can be done in less code but this way I hope you see what is happening.
Did not test any of this so a mistake could be involved.
Regards,
Paul (mrCoolLion)
-
Klaus
- Posts: 13864
- Joined: Sat Apr 08, 2006 8:41 am
- Location: Germany
-
Contact:
Post
by Klaus » Sun Jan 05, 2020 4:29 pm
Hi Kaveh,
kaveh1000 wrote: ↑Sat Jan 04, 2020 11:50 pm
How can I transform my array?
you can't! You have to "re-create" the array, but the best method is of course, as Paul pointed out,
to create the array directly with the desired keys (key names).
Best
Klaus
-
kaveh1000
- Livecode Opensource Backer
- Posts: 508
- Joined: Sun Dec 18, 2011 7:23 pm
- Location: London
-
Contact:
Post
by kaveh1000 » Sun Jan 05, 2020 4:36 pm
Thank you both. I was waiting to experiment more before commenting, but I thought there might be a more direct way.
Paul's method is very informative and I have used it successfully. Thanks again.
Kaveh
-
dunbarx
- VIP Livecode Opensource Backer
- Posts: 9731
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Post
by dunbarx » Tue Jan 07, 2020 8:26 pm
Hi.
A couple of posts recently involving array issues when keys are numbers. This usually because it is left up to livecode to supply them.
This is similar to numbering your field names. Don’t
Name your keys. It is just a bit of extra work, but SO worth it, if not just for readability.
Craig
-
kaveh1000
- Livecode Opensource Backer
- Posts: 508
- Joined: Sun Dec 18, 2011 7:23 pm
- Location: London
-
Contact:
Post
by kaveh1000 » Tue Jan 07, 2020 8:41 pm
So am I doing the right thing Craig, by your reasoning?
K
Kaveh
-
FourthWorld
- VIP Livecode Opensource Backer
- Posts: 9852
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
-
Contact:
Post
by FourthWorld » Tue Jan 07, 2020 8:44 pm
Does the data need to be an array?
-
kaveh1000
- Livecode Opensource Backer
- Posts: 508
- Joined: Sun Dec 18, 2011 7:23 pm
- Location: London
-
Contact:
Post
by kaveh1000 » Tue Jan 07, 2020 8:54 pm
No, I can use custom properties of buttons. I find that quite intuitive. I thought arrays might be faster, though not much data.
I would often use tables with tabs and just access each column as needed. But my data has lots of returns in each element so hard to use tables.
Kaveh
-
FourthWorld
- VIP Livecode Opensource Backer
- Posts: 9852
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
-
Contact:
Post
by FourthWorld » Tue Jan 07, 2020 9:17 pm
kaveh1000 wrote: ↑Tue Jan 07, 2020 8:54 pm
No, I can use custom properties of buttons. I find that quite intuitive. I thought arrays might be faster, though not much data.
I would often use tables with tabs and just access each column as needed. But my data has lots of returns in each element so hard to use tables.
If the problem lends itself to rows and columns, I'd probably just leave the data in rows and columns.
For large data sets accessed frequently, there can be some good advantages to using arrays. But the best use of arrays is when they fit the problem at hand.
-
Mark
- Livecode Opensource Backer
- Posts: 5150
- Joined: Thu Feb 23, 2006 9:24 pm
-
Contact:
Post
by Mark » Tue Jan 07, 2020 9:37 pm
This is why there are relational databases. For instance, store your data in a SQlite database and you could do
Code: Select all
SELECT country FROM countries WHERE capital = 'Paris'
as well as
Code: Select all
SELECT capital FROM countries WHERE country is "France"
and
Code: Select all
SELECT * FROM countries WHERE id = '2'
If you have comma separated data, for instance
2,France,Paris,80
1,Netherlands,Amsterdam,18
3,Germany,Berlin,90
5,Japan,Tokyo,200
4,USA,Washington DC,150
1,Netherlands,Amsterdam,88
you might as well store it as a custom property as is and get the data using syntax like
Code: Select all
filter d with "[0-9]*,[a-zA-Z]*,Amsterdam,[0-9]*"
but this could be a big hassle if your data contains returns. Therefore I would recommend SQlite.
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
-
kaveh1000
- Livecode Opensource Backer
- Posts: 508
- Joined: Sun Dec 18, 2011 7:23 pm
- Location: London
-
Contact:
Post
by kaveh1000 » Tue Jan 07, 2020 9:47 pm
Thanks Mark. Actually I don't have to do any clever filtering. And I want the data entry to be kept very simple. So I am keeping the data in Google Sheets, and reading them when LiveCode opens a stack. Then I just keep in memory. I actually successfully used MySQL, but I migrated to Google Sheets in order to simplify data manipulation.
Kaveh
-
Mark
- Livecode Opensource Backer
- Posts: 5150
- Joined: Thu Feb 23, 2006 9:24 pm
-
Contact:
Post
by Mark » Tue Jan 07, 2020 10:20 pm
Okay, but then you can't do what you want to do according to your original question. All fine by me.
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
-
kaveh1000
- Livecode Opensource Backer
- Posts: 508
- Joined: Sun Dec 18, 2011 7:23 pm
- Location: London
-
Contact:
Post
by kaveh1000 » Tue Jan 07, 2020 10:36 pm
I import the data into an array, but the keys are automatically the row numbers. Using Paul's suggestion I change set the keys to a name. So that works.
But I am now thinking of creating buttons and setting all the data as custom values. so the button names are the keys. This works for me as I need the buttons anyway, in order to select which "rows" I want to take action with. So I will use the Google Sheet as a "style sheet" to create buttons on the fly and populate each with the values. Then no trouble with data with line breaks.
sorry it is not clear but I find custom values very intuitive and in my case it works as I need buttons.
Kaveh
-
jacque
- VIP Livecode Opensource Backer
- Posts: 7257
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
-
Contact:
Post
by jacque » Wed Jan 08, 2020 7:25 pm
dunbarx wrote: ↑Tue Jan 07, 2020 8:26 pm
A couple of posts recently involving array issues when keys are numbers. This usually because it is left up to livecode to supply them.
This is similar to numbering your field names. Don’t
It depends on the situation. Usually I name array keys but in my current project I need numbers. The array represents pages in a book and I need to access the page content non-sequentially. Splitting the array without a secondary delimiter gives me an array with page numbers as the keys.
So, it depends.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
-
dunbarx
- VIP Livecode Opensource Backer
- Posts: 9731
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Post
by dunbarx » Wed Jan 08, 2020 10:58 pm
Jacque.
A fair point. And if one is careful, one can surely use numbers, even with field names.
But I would still do what we both have advised in the past, maybe something like “p1”?
It likely will require just a little more effort to parse the page number, but we are both used to that.
Craig