Page 1 of 2

Using a value as a key in array

Posted: Sat Jan 04, 2020 11:50 pm
by kaveh1000
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.

Code: Select all

put Countries[2]["Country"]
and I would get France. Then I say

Code: Select all

put Countries[2]["Capital"]
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

Re: Using a value as a key in array

Posted: Sun Jan 05, 2020 12:28 am
by mrcoollion
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. :lol:

Regards,

Paul (mrCoolLion)

Re: Using a value as a key in array

Posted: Sun Jan 05, 2020 4:29 pm
by Klaus
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

Re: Using a value as a key in array

Posted: Sun Jan 05, 2020 4:36 pm
by kaveh1000
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. :-)

Re: Using a value as a key in array

Posted: Tue Jan 07, 2020 8:26 pm
by dunbarx
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

Re: Using a value as a key in array

Posted: Tue Jan 07, 2020 8:41 pm
by kaveh1000
So am I doing the right thing Craig, by your reasoning?

K

Re: Using a value as a key in array

Posted: Tue Jan 07, 2020 8:44 pm
by FourthWorld
Does the data need to be an array?

Re: Using a value as a key in array

Posted: Tue Jan 07, 2020 8:54 pm
by kaveh1000
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.

Re: Using a value as a key in array

Posted: Tue Jan 07, 2020 9:17 pm
by FourthWorld
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.

Re: Using a value as a key in array

Posted: Tue Jan 07, 2020 9:37 pm
by Mark
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.

Re: Using a value as a key in array

Posted: Tue Jan 07, 2020 9:47 pm
by kaveh1000
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.

Re: Using a value as a key in array

Posted: Tue Jan 07, 2020 10:20 pm
by Mark
Okay, but then you can't do what you want to do according to your original question. All fine by me.

Re: Using a value as a key in array

Posted: Tue Jan 07, 2020 10:36 pm
by kaveh1000
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.

Re: Using a value as a key in array

Posted: Wed Jan 08, 2020 7:25 pm
by jacque
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.

Re: Using a value as a key in array

Posted: Wed Jan 08, 2020 10:58 pm
by dunbarx
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