Page 1 of 1

a ray for array... (=^..^=)

Posted: Wed Dec 19, 2018 2:09 pm
by Mariasole
Dear friends of the forum!
I would need a ray of knowledge regarding the infamous arrays! :oops:

I've always tried to avoid them! But continuing in the studies of LC, I think I can not do without it!
Now, even reading all the lessons available on the site, I have not understood them well! :roll:

Excuse me, but it's not totally my fault! Until I deal with fields and variables (which for me are an "abstraction" of a field) I understood, but the arrays are a bit complex because, at least I think, I do not have any computer literacy. Sorry, I try to ask for help as little as possible!

That said, I'll give you this example that I can not solve:

I created a csv field of this type:

Type of cake, number of cakes in the fridge, temperature, chef

Cheese cake, 3, 5 °, Sandra
Banana cake, 2, 4 °, Mariasole
Banana cake, 1, 4 °, Zoe
Banana cake, 5, 4 °, Filippa
Cheese cake, 1, 5 °, Fabiana
Cheese cake, 5, 5 °, Marta
Raspberry cake, 1, 0 °, Marta
Strawberry cake, 3, 0 °, Mariasole

Now, I have to load this list into an array.
This I understood how to do it (at least in part!) :(

Code: Select all

	put field 1 into theData
	split theData by return and comma
But here is the first problem. If I see what I have loaded into the array I find:

Banana cake = 5, 4 °, Filippa

But I wanted (in short, I expected):

Banana cake = 5 = 4 ° = Filippa


I also tried to write this (I am ashamed!):

Code: Select all

	split theData by return and comma and comma and comma
but of course it does not work! :lol:


I would like to use this list for example to get this result.

How many cakes do we have in the fridge for the next party?

The result that I expect would be this:

Banana cake, 8
Cheese cake, 9
Strawberry cake, 3
Raspberry cake, 1

But I do not understand how to isolate only certain parts of the array and add them. To put them in order I found instead how to do!

Is there anyone who can help me to understand?
Thank you so much to everyone !!!!


Peace and love!


(=^..^=)
Mariasole

Re: a ray for array... (=^..^=)

Posted: Wed Dec 19, 2018 4:03 pm
by dunbarx
Hello, Mariasole.

Try this experiment. Place the list you posted into a field 1. Make a button. In the button script;

Code: Select all

on mouseUp
   get fld 1
   
   repeat for each line tLine in it
      put item 1 of it into myArray[item 2 of it][item 3 of it][item 4 of it]
   end repeat
  breakpoint
   
   repeat for each line tLine in it
      add item 2 of tLine to cakeCounter[item 1 of tline]
   end repeat
   breakpoint
end mouseUp
I placed breakpoints in two places to stop execution, because one of the best places to see what array variables are doing is in the debugger. So in the first breakpoint you can see the entire deconstructed array. This may not be terribly useful for you. In the second breakpoint, you can see the counting gadget you asked for.

Arrays take a little practice, but they are very fast and can hold a lot of data. And yes, it is tricky to find "interior" parts of data (items in your example) unless you are experienced. The "interior" parts are in fact arrays themselves.

I had made an enhancement request a couple of years ago to allow direct access to the interior elements of an array, sort of what you were trying to do. There is a good thread about this on the forum. I will try to find it and add it here.

Craig Newman

Re: a ray for array... (=^..^=)

Posted: Wed Dec 19, 2018 5:52 pm
by MaxV
Mariasole,
you need to start using Sqlite, it is inside livecode

Re: a ray for array... (=^..^=)

Posted: Wed Dec 19, 2018 6:16 pm
by dunbarx
Mariasole.

If you run my experiment, put a breakpoint right at the "add item 2..." line, so you can watch the accumulation of the count in each of the array variables.

Craig

Re: a ray for array... (=^..^=)

Posted: Wed Dec 19, 2018 7:25 pm
by FourthWorld
There are many great uses for arrays, particularly for hierarchical data.

But when a delimited list does what you need, does it have to become an array?

Re: a ray for array... (=^..^=)

Posted: Wed Dec 19, 2018 10:13 pm
by jacque
Here's another basic explanation of arrays: http://livecode.byu.edu/arrays/introToArrays.php

It only talks about one-dimensional arrays, which is the most basic level. Craig's example deals with multi-dimensional arrays, which are arrays that contain other arrays.

Re: a ray for array... (=^..^=)

Posted: Fri Dec 21, 2018 6:40 am
by jameshale
It would seem you want a simple list of cakes with totals.

Arrays in LiveCode are associated lists, that is a collection of paired values.

This collection will have a name (the array name) and the members of the collection will have two values.
The first being the "key" and the second being the value associated with that key.
The "key" and the "value" can be any string.
(This is sometimes confusing as we may have as our "keys" strings of numerals making the array seem to be an indexed array which we may be familiar with from other languages.)

So in your case we could state what you want as...
A list of cakes (the array) with the cake name (key) and its total count (value)

Your original list was:
Cheese cake, 3, 5 °, Sandra
Banana cake, 2, 4 °, Mariasole
Banana cake, 1, 4 °, Zoe
Banana cake, 5, 4 °, Filippa
Cheese cake, 1, 5 °, Fabiana
Cheese cake, 5, 5 °, Marta
Raspberry cake, 1, 0 °, Marta
Strawberry cake, 3, 0 °, Mariasole

And the list (array) you want to end up with is

Banana cake, 8
Cheese cake, 9
Strawberry cake, 3
Raspberry cake, 1

The split and combine commands work on arrays but assume they are single dimension so can't really be used on your original list.
Even though you want to effectively end up with a simple array, you will need to build it from scratch.
Having said that, this is very simple.

We will call it CakesA, and it will have as its "key" the cake's name (item 1 of each line) and its value will be the number of cakes of that name (item 2 of each line totalled per cake).

On a stack place a field and two buttons.

Then have your original list in this field (say field "f1")

Btn 1 can be called "Count Cakes" and have the following script:

Code: Select all

on mouseUp pMouseButton
   put fld "f1" into temp
   countCakes temp
end mouseUp
Btn 2 can be called "Show count" and have the following script:

Code: Select all

on mouseUp pMouseButton
   showcount
end mouseUp
Now make two handlers, "countCakes" [working on the list of cakes/counts/temp/chef] and "showCount" [to display the array] and put them on a card or stack script.

Code: Select all

local CakesA

on countCakes plist
  repeat for each line tline in plist
    add item 2 of tline to CakesA[item 1 of tline]
  end repeat
end countCakes

to see what we have...

on showCount 
   put empty into flist
   put the keys of CakesA into thekeys
   sort thekeys ascending
   put the number of lines in thekeys into tlimit
   repeat with x = 1 to tlimit
      put line x of thekeys & comma && CakesA[line x of thekeys] & return after flist
   end repeat
   put flist
end showCount
As you can see by this example the summarising you wanted to do is ideally suited to an array, given it is basically a one line script within a repeat loop. In fact there are a lot more lines to display the array than make it.

Try it out.

This method is a variation of an example given on the LC site for doing a word count.

Code: Select all

on countWords ptext
  repeat for each word tword in ptext
    add 1 to WordA[tword]
  end repeat
end countWords
Anyway, hope this is of use.

Re: a ray for array... (=^..^=)

Posted: Fri Dec 21, 2018 11:19 am
by mrcoollion
This is the way I look at array’s and I use them a lot.

First of all I try to see an array from a database standpoint. When you put data into a database file you need to look at the data from a hierarchy point of view.
In your case the top level of you hierarchy is the Chef. Also because each Chef is unique There is only one of each).
The next level is the type op Pie’s they make. Per chef the type of pie is also unique (one pie type per chef).
So this would make the hierarchy as follows:

[ChefName]>[TypeOfCake]>

Because you have two data items at the same level NumberOfCakes and Temperature we need to split those under the level of TypeOfCake.
So what I typically do is give those hirachies an appropiate name e.g.:

ArrayName [ChefName][TypeOfCake][“NumberOfCakes”][NumberOfCakes]
ArrayName [ChefName][TypeOfCake][“Temperature”][Temperature]

Now I can put the data in the array and retrieve it per chef.

Lets say you have multiple fridges than you need another hierachy level the fridgename.
This level can be placed between ChefName and TypeOfCake. In this case the array would be:

ArrayName [ChefName][FridgeName][TypeOfCake][“NumberOfCakes”][numberOfCakes]
ArrayName [ChefName][FridgeName][TypeOfCake][“Temperature”][Temperature]

Or depending on the purpose of the array you can choose the FridgeName as the top level (Because the fridgename is also unique). In that case the array structure would be:

ArrayName [FridgeName][ChefName][TypeOfCake][“NumberOfCakes”][numberOfCakes]
ArrayName [FridgeName][ChefName][TypeOfCake][“Temperature”][Temperature]


Hope this helps.

Kind regards,

Paul (mrcoollion)

Re: a ray for array... (=^..^=)

Posted: Sat Dec 22, 2018 4:47 pm
by Mariasole
Thank you so much to everyone!
Now I'm studying all your suggestions to understand if ... I understand! :shock:
Once I have assimilated the lessons, I will try to summarize ...
Thank you! Grazie davvero, siete molto carini!

Mariasole
(=^..^=)

Re: a ray for array... (=^..^=)

Posted: Sun Dec 23, 2018 7:11 pm
by jacque
Mariasole, there is something very sweet and unassuming about you. I'm glad you are here. I smile at all your posts. :)

Re: a ray for array... (=^..^=)

Posted: Mon Dec 24, 2018 5:04 pm
by Mariasole
jacque wrote:
Sun Dec 23, 2018 7:11 pm
Mariasole, there is something very sweet and unassuming about you. I'm glad you are here. I smile at all your posts. :)
Thank you so much Jacque!
On this cold winter evening you warmed my heart!
Merry Christmas to you and to all those you love! :D

(='.'=)
Mariasole