FR - Arrays... Standard Array Functionality In LiveCode

Something you want to see in a LiveCode product? Want a new forum set up for a specific topic? Talk about it here.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
sumpm1
Posts: 19
Joined: Tue May 21, 2013 2:35 am

FR - Arrays... Standard Array Functionality In LiveCode

Post by sumpm1 » Mon May 27, 2013 11:23 am

Can we discuss getting "standard array functionality" into LiveCode...?



In LiveCode, it seems that arrays behave differently than in many other programming languages. And so this may confuse new programmers and learners, as well as people that have used arrays before.

If you DELETE an myArray[2] in LiveCode, the LENGTH of myArray does not shrink. It seems that LiveCode uses "associative arrays." And each element is "associated" to its key. So deleting myArray[2] just leaves a "blank" spot in the second element of myArray. Whereas in most programming languages, deleting myArray[2] would shrink the number of elements in myArray, and myArray[3] would shift into the 2nd element.

This makes some very weird things happen, and forces us to INVENT the standard procedures that make arrays useful in the first place.

This is a shame. And I can't imagine how hairy things would get if you are using multidimensional arrays!

And if we are using LiveCode to teach kids how to code, they should know the proper use of an array!



If I am totally misunderstanding this, and the users in the following threads are misunderstanding proper array usage as well, please let me know. I just want LiveCode to be as simple to use as possible.

This is documented here:
http://forums.runrev.com/viewtopic.php?f=7&t=13116
http://www.mail-archive.com/use-livecod ... 12824.html

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1682
Joined: Sat Feb 28, 2009 11:49 pm

Re: FR - Arrays... Standard Array Functionality In LiveCode

Post by sturgis » Mon May 27, 2013 1:26 pm

In this case, there isn't actually a "blank spot" in the array. The element is gone. But since it is an associative array, and keys are assigned by you (or the datagrid or whatever) then since its an association, whatever key is associated with data doesn't change when other keys are deleted.

If you had an array with keys "fredslastname", "maryslastname","joeslastname" and delete myArray["maryslastname"] joe and fred don't have a name change just because mary is gone. Numeric keys are treated the same way. They aren't an index, they're just a key that happen to be a number. In fact you can't count on array keys (even numeric) coming out the same way they went in.

If you were to

repeat for each key tKey in myArray
put tKey & comma after msg
end repeat

you might get lucky and have the list of keys come out 1,2,3,4,5,6,7,8,9,10,11,12 but you might not. The sequence could end up being any conceivable order.

When you delete a key, try this.

put the number of keys in myArray into tStartKeys

then delete an element

then look at how many keys are left in myArray. Should be 1 less than the value of tStartKeys


I either a) keep a list of keys that I manage, or b) when working with arrays I get the keys of the array and sort them, when I display the data I used the sorted keys so that the order is the same, and then.. well if I want to work with an element, or whatever, I used the line number.

So, to delete the 5th task in the task list, would mean to delete myVariable[line 5 of my sorted list that has the keys in it.......] and then delete line 5 of the sorted list that has the keys in it <-- bad pseudocode but you get the idea

It would be nice of there were numerically indexed arrays though. But till then I just remember that the array acts more like a database, and the "primary key (unique integer autoincrement) is immutable after creation. (unless as other threads have mentioned, you just want to rebuild the array every time a change is made)

sumpm1
Posts: 19
Joined: Tue May 21, 2013 2:35 am

Re: FR - Arrays... Standard Array Functionality In LiveCode

Post by sumpm1 » Mon May 27, 2013 6:34 pm

When you delete a key, try this.

put the number of keys in myArray into tStartKeys

then delete an element

then look at how many keys are left in myArray. Should be 1 less than the value of tStartKeys
Yes, LiveCode reports one less element in myArray after delete. Say my Array has 52 items (a deck of cards), and I:

1.

Code: Select all

Delete delete myArray[2]    -- remove the second card of the deck
2.

Code: Select all

repeat with i=1 to number of keys in myArray
  put myArray[i] into field "output"
end repeat
The result will be 52 items! And one of them will be blank.
I don't want that blank card to stay in my deck, so I should only have 51 cards left in the deck.
That is how "standard arrays" or "numerical arrays" as you have called them work. And how most people use arrays in other languages.
I either a) keep a list of keys that I manage, or b) when working with arrays I get the keys of the array and sort them, when I display the data I used the sorted keys so that the order is the same, and then.. well if I want to work with an element, or whatever, I used the line number.

So, to delete the 5th task in the task list, would mean to delete myVariable[line 5 of my sorted list that has the keys in it.......] and then delete line 5 of the sorted list that has the keys in it <-- bad pseudocode but you get the idea
Yes, we are left WRITING THE CODE for standard array functionality. And what would we do if we had a multidemensional array to manage? We would be WORKING OUR BUTTS OFF to get standard functionality.

In order to get standard array functionality, we have TO WRITE THE CODE FOR MANAGING THE KEYS EVERY TIME. Isn't that horrible? Isn't that wasteful? Isn't that inefficient?

In fact, this drawback might deter a student from completing a project, or even a whole host of projects. If the student realizes he needs an array, uses one, realizes things aren't working properly when using arrays, finds this thread and the ones I have mentioned, writes THEIR OWN CODE to manage array keys, and then GETS BOGGED DOWN with that whole process...

The student might just say "it's too hard, I couldn't finish that project, I got stuck."

And what is the point of using arrays if they don't serve the correct function?
And how does this benefit new programmers? They won't even know how an array works!
They will be extra confused. And they will have tons of extra lines of code in their programs that don't make sense to them; WORKING AROUND a missing feature in LiveCode.

These new programmers will lack the knowledge of how arrays actually work. And these new programmers will be learning bad coding practices when using arrays in LiveCode. If they go to a new language after LiveCode, they will think that they need to manage array elements manually; bad practice.

It would be nice of there were numerically indexed arrays though.
Yes that is what I am requesting, "numerically indexed arrays."
If it has to become some special type of array, fine. But we need them, so we can teach new programmers properly.

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

Re: FR - Arrays... Standard Array Functionality In LiveCode

Post by FourthWorld » Mon May 27, 2013 7:27 pm

sumpm1 wrote:Yes that is what I am requesting, "numerically indexed arrays."
If it has to become some special type of array, fine. But we need them, so we can teach new programmers properly.
To teach them properly, we would teach them that there are at least two types of arrays.

One is not categorically "better" than another, nor is either in any way deficient. They're just different things, and helping students understand the strengths and weaknesses of each will help them use both more effectively.

Numerically-indexed arrays are on RunRev's to-do list, but in all fairness to RunRev the choice to implement associative arrays first was arguably a good one, since they're far more flexible and in many (not all, but many) cases one can also use them for most common tasks requiring numeric indexing.

But unlike strictly-numerically-indexed arrays, associative arrays impose few limits on key values. Integers, floats, strings - nearly anything that doesn't contain a NULL byte can be used for the key, provided it's no longer than 255 bytes.

I recognize that your expectation that associative arrays would behave identically to numerically-indexed has been a source of frustration, but given the breadth of tasks associative arrays have been successful solutions for, while we wait for RunRev to implement numerically-indexed arrays it may be helpful if you could tell us a bit more about the specifics of the problem you're trying to solve. Hopefully we'll be able to come up with a solution that satisfies your needs well enough for now.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

Klaus
Posts: 11061
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: FR - Arrays... Standard Array Functionality In LiveCode

Post by Klaus » Mon May 27, 2013 8:13 pm

Hi sumpm1,

1. welcome to the forum! :-)

2.
sumpm1 wrote:...Yes, LiveCode reports one less element in myArray after delete. Say my Array has 52 items (a deck of cards), and I:
1.

Code: Select all

Delete delete myArray[2]    -- remove the second card of the deck
2.

Code: Select all

repeat with i=1 to number of keys in myArray
  put myArray[i] into field "output"
end repeat
The result will be 52 items! And one of them will be blank.
This is just not true and a contradiction to yourself!? :-)

-> Yes, LiveCode reports one less element in myArray after delete.
-> Say my Array has 52 items (a deck of cards), and I: Delete delete myArray[2]
-> The result will be 52 items!
Nope -> 51


Best

Klaus

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3279
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: FR - Arrays... Standard Array Functionality In LiveCode

Post by bn » Mon May 27, 2013 9:19 pm

Sumpm1,
repeat with i=1 to number of keys in myArray
put myArray into field "output"
end repeat

if you do that on your array you will get one empty result because you are requesting a key that does not exist anymore.
In your example of 52 elements in the array and if you delete the element with the key 2 and ask in your repeat loop for the value of key 2 (i=2) it will of course return empty since you deleted the element with key 2
But your loop will only go from 1 to 51.

put this into a button (I only use 12 elements to make the point)

Code: Select all

on mouseUp
   repeat with i = 1 to 12
      put 1 into tArray[i]
   end repeat
   delete local tArray[2]
   put the keys of tArray into msg
   sort msg ascending numeric 
   put cr & cr & "the array has " &  the number of lines of the keys of tArray & " keys" after  msg
   put cr & cr &  "the value of element with key 2 :" & tArray[2] & ":-" after msg
end mouseUp
it will put all the stuff into the message box

Kind regards
Bernd

sumpm1
Posts: 19
Joined: Tue May 21, 2013 2:35 am

Re: FR - Arrays... Standard Array Functionality In LiveCode

Post by sumpm1 » Mon May 27, 2013 9:43 pm

-> Yes, LiveCode reports one less element in myArray after delete.
-> Say my Array has 52 items (a deck of cards), and I: Delete delete myArray[2]
-> The result will be 52 items!
Nope -> 51
Hi Klaus! You are correct. My code will print 51 items.

I think my confusion came when item 2 IS BLANK, and others are pointing that out, and i realize this is because of the associative array type, "item 2" is gone, and I asked to print "item 2" thus getting one blank answer.

Thank you for being so helpful. I want to see your product be the best out there. LiveCode has already allowed me to create software THAT I NEVER could have created before; and in a hurry.

You can tell I am passionate about this.

Numerically-indexed arrays are on RunRev's to-do list
Is this true? Can anyone confirm this?

If it is true that RunRev is already aware and has plans of implementing numerically indexed arrays sometime in the future, this would be great news, and I would love to hear it from one of the team.

it may be helpful if you could tell us a bit more about the specifics of the problem you're trying to solve.
Yes. I will start another thread to get help on the specific project I am working on, thank you. I just saw arrays as a simple way to shuffle a list, but then got surprised when my list had many blanks in it. So I went on a search, and found numerically indexed arrays do not exist in LiveCode.
Last edited by sumpm1 on Mon May 27, 2013 10:31 pm, edited 1 time in total.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1682
Joined: Sat Feb 28, 2009 11:49 pm

Re: FR - Arrays... Standard Array Functionality In LiveCode

Post by sturgis » Mon May 27, 2013 9:56 pm

Just got home or i'd have joined in here earlier but..


If you put the following code into a button..

local myArray

repeat with i = 1 to 52
put myArray
end repeat

it will loop 52 times, putting nothing. The keys were never created, but since the variable was declared it exists. But there is nothing associated with key 1, 2, 3, 4, 5, 6 so that is what you see for that key. Nothing. There is nothing in the array with that key.

EDIT: Nevermind, I should read before I post! Its covered.
Last edited by sturgis on Mon May 27, 2013 11:07 pm, edited 1 time in total.

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

Re: FR - Arrays... Standard Array Functionality In LiveCode

Post by FourthWorld » Mon May 27, 2013 10:05 pm

sumpm1 wrote:
Numerically-indexed arrays are on RunRev's to-do list[/qoute]

Is this true? Can anyone confirm this?

If it is true that RunRev is already aware and has plans of implementing numerically indexed arrays sometime in the future, this would be great news, and I would love to hear it from one of the team.
I don't know where's or even if it's been documented, but FWIW I've had discussions with RunRev team members about it. Of course with the current set of priorities (have you seen the Kickstarter stretch goals list? It's quite long) I wouldn't expect them to be able to tackle this right away, but I know it's been considered as an enhancement once the bigger goals are met.
it may be helpful if you could tell us a bit more about the specifics of the problem you're trying to solve.[/qoute]

Yes. I will start another thread to get help on the specific project I am working on, thank you.
Looking forward to it. I suspect that with only relatively minor modifications to your expected algorithm, we can come up with a solution that meets your needs.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

DarScott
Posts: 227
Joined: Fri Jul 28, 2006 12:23 am
Location: Albuquerque
Contact:

Re: FR - Arrays... Standard Array Functionality In LiveCode

Post by DarScott » Mon May 27, 2013 10:18 pm

Perhaps part of this is a pedagogical and documentation problem. We can say LiveCode arrays are "associative arrays". Or maybe a whole new word can be used.

Libraries can be created to work with sequences, sets, even trees, based on contiguous count indexed arrays.

(Someplace I'll propose virtual strings and virtual arrays in implementation, to that underneath those things we want to work efficiently, such as contiguous count indexed arrays, can. I think that goes someplace else.)

Post Reply

Return to “Feature Requests”