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

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

Post Reply
Mariasole
Posts: 235
Joined: Tue May 07, 2013 9:38 pm

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

Post by Mariasole » Wed Dec 19, 2018 2:09 pm

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
"I'm back" - The Cyberdyne Systems Model 101 Series 800 Terminator

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9655
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Wed Dec 19, 2018 4:03 pm

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

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

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

Post by MaxV » Wed Dec 19, 2018 5:52 pm

Mariasole,
you need to start using Sqlite, it is inside livecode
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9655
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Wed Dec 19, 2018 6:16 pm

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

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

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

Post by FourthWorld » Wed Dec 19, 2018 7:25 pm

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?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7227
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

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

Post by jacque » Wed Dec 19, 2018 10:13 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

jameshale
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 474
Joined: Thu Sep 04, 2008 6:23 am
Location: Melbourne Australia

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

Post by jameshale » Fri Dec 21, 2018 6:40 am

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.

mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

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

Post by mrcoollion » Fri Dec 21, 2018 11:19 am

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)

Mariasole
Posts: 235
Joined: Tue May 07, 2013 9:38 pm

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

Post by Mariasole » Sat Dec 22, 2018 4:47 pm

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
(=^..^=)
"I'm back" - The Cyberdyne Systems Model 101 Series 800 Terminator

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7227
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

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

Post by jacque » 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. :)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Mariasole
Posts: 235
Joined: Tue May 07, 2013 9:38 pm

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

Post by Mariasole » Mon Dec 24, 2018 5:04 pm

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
"I'm back" - The Cyberdyne Systems Model 101 Series 800 Terminator

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”