[Solved] How to delete an arbitrary element from an 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

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

Re: [SOLVED] How to delete an arbitrary element from an arra

Post by Klaus » Fri Oct 12, 2012 12:10 am

Hi Hylton,

cool :D

So please allow me to be a bit nitpicky:
My issue is not with line 1 per say
I am sure you mean "per se", right?
Sorry, I'm into languages and that really hurts 8)


Best

Klaus

hylton
Posts: 53
Joined: Thu Oct 04, 2012 12:49 pm

Re: [SOLVED] How to delete an arbitrary element from an arra

Post by hylton » Fri Oct 12, 2012 12:48 am

hyltonclarkeBUSH1Ex wrote:I am sure you mean "per se", right?
Thanks for that 8)

Something strange is happening. The handler is working and gTaskList is rewritten perfectly, but when I want to display the array in the field, the elements are jumbled up. Example: I create 10 tasks, then after deleting task 5, the list looks like:

Task 8
Task 9
Task 10
Task 1
etc.

Code: Select all

on openCardList
   put empty into field "task list"
   repeat for each key tKey in gTaskList
      put gTaskList[tKey]["task title"] & return after field "task list"
   end repeat
end openCardList
For some reason, tKey usually begins with value 8, if I have approximately 10 elements in the array.

Is there a better way of displaying the contents of gTaskList["task title"] into field "task list"?

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: [SOLVED] How to delete an arbitrary element from an arra

Post by mwieder » Fri Oct 12, 2012 1:09 am

The order of elements in an array isn't deterministic. If you want to display the elements in order you'd have to sort the keys in gTaskList before the repeat loop.

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: [SOLVED] How to delete an arbitrary element from an arra

Post by sritcp » Fri Oct 12, 2012 1:15 am

Would this work?

Code: Select all

put the keys of gTaskArray into gKeysList
# I have renamed your gTaskList as gTaskArray; gKeysList would be a list
delete variable gTaskArray[item gHilitedLine of gKeysList]
delete item gHilitedLine of gKeysList
# Now, if you display the remainder of the array, the order of the elements in the array should be undisturbed.
Regards,
Sri.

hylton
Posts: 53
Joined: Thu Oct 04, 2012 12:49 pm

Re: How to delete an arbitrary element from an array

Post by hylton » Fri Oct 12, 2012 1:26 am

Hi Sri

Thank you for your suggestion, but I don't understand what I am supposed to do with gKeysList?

If I put your code into my handler and rename it back to gTaskList, nothing happens. If I create 9 tasks and delete a task, I still have 9 tasks.

hylton
Posts: 53
Joined: Thu Oct 04, 2012 12:49 pm

Re: How to delete an arbitrary element from an array

Post by hylton » Fri Oct 12, 2012 1:37 am

mwieder wrote:The order of elements in an array isn't deterministic. If you want to display the elements in order you'd have to sort the keys in gTaskList before the repeat loop.
I thought that I had achieved that with Klaus' code, but unfortunately, after looking carefully at the array in the variable inspector, I see that the keys are being rewritten correctly, but the contents of the elements no longer match the keys, which then makes the display of the elements in the field jumbled up.

The jumbled up elements comes from the delete task handler, before I display the elements of the array in the field.

Any ideas on how to adjust the delete task handler, so that this problem is avoided?

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: How to delete an arbitrary element from an array

Post by mwieder » Fri Oct 12, 2012 2:47 am

Sorry, after now having gone back over page 1 I'm not sure I understand what the problem is that Klaus is trying to solve. I'll leave it to him to explain, or maybe someone who knows what the TickedOff video is.

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: How to delete an arbitrary element from an array

Post by sritcp » Fri Oct 12, 2012 4:19 am

Hi Hylton:

I solved your problem in principle; since I did not refer specifically to the TickedOff project, you will have to make the necessary changes. Here's a more detailed explanation:
1. I created an array gTaskArray; it has, say, 4 keys; call them a, b, c, d; the corresponding element values are "element1", "element2", "element3", element4", let's say.
2.

Code: Select all

put the keys of gTaskArray into gKeyList
The above statement puts the keys of the array into a return-delimited list, which looks like:
a
b
c
d
3.

Code: Select all

# assuming gHilitedLine holds the number of the line clicked by the user (in the list of array keys, i.e., a, b, c, or d)
   set the itemDel to return  -- you need to do this since comma is the default itemDelimiter
   delete variable gTaskArray[item gHilitedLine of gKeyList]
   delete item gHilitedLine of gKeyList
If the user had clicked on the second line, gHilitedLine = 2, so gTaskArray would be deleted, and the gKeyList would look like this:
a
c
d
If you displayed the array elements, it would be:
element1
element3
element4
So, the order of your array elements holds.
I checked and it works. You will have to modify the actual statements to fit your specific situation.

Regards,
Sri.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: How to delete an arbitrary element from an array

Post by Mark » Fri Oct 12, 2012 7:31 am

Hi,

To get the list of elements in "correct" order and subsequently delete the first key, do this:

Code: Select all

put the keys of myArray into myKeys
sort lines of myKeys numeric
delete myArray[line 1 of myKeys]
As someone else has noted, arrays are non-deterministic, which causes these troubles.

Kind regards,

Mark
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

hylton
Posts: 53
Joined: Thu Oct 04, 2012 12:49 pm

Re: How to delete an arbitrary element from an array

Post by hylton » Sat Oct 13, 2012 10:30 am

Unfortunately, none of these answers are getting me to a
place where I understand how multi-dimensional arrays
work.

So let me try explain again.

My array looks like this in the variable inspector:

Code: Select all

gTaskArray
   1
      task title 1
      task description 1
   2
      task title 2
      task description 2
   3
      task title 3
      task description 3
If I use combine, I lose my multi-dimensional array, so that doesn't work.

What I am trying to achieve is this:

Example, I have three tasks, as shown in the array above, and I delete task 2, using

Code: Select all

 delete variable gTaskArray[gHilitedLine] 
and it works.

So now, gTaskArray looks like this:

Code: Select all

gTaskArray
   1
      task title 1
      task description 1
   3
      task title 3
      task description 3
I now need to update the task list field, so I am looking for the correct code that will take the above
array and give me this result:

Code: Select all

gTaskArray
   1
      task title 1
      task description 1
   2
      task title 3
      task description 3
That is it. The keys needs to be updated, so that the keys are in order.

This is from the very first example from the Business Academy resource, a simple task list application called TickedOff.

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

Re: How to delete an arbitrary element from an array

Post by Klaus » Sat Oct 13, 2012 12:41 pm

Hi Hylton,

try this:
...
global gHilitedLine
put the keys of gTaskArray into tKeys
sort tKeys numeric
## !!!
put line gHilitedLine of tKeys into tKey2Delete
delete variable gTaskArray[tKey2Delete]
## !!!
...

This should assure that you always delete the correct corresponding line key <-> gHilitedline
no matter if there are "holes" in the numbering of the keys.

Now I hope I don't have a "hole" on my thinking :D


Best

Klaus

hylton
Posts: 53
Joined: Thu Oct 04, 2012 12:49 pm

Re: How to delete an arbitrary element from an array

Post by hylton » Sat Oct 13, 2012 2:02 pm

Mind ... Blown!

Accessing arrays with a variable o_O!

Klaus, your code has worked like a champ!

To update the task list field, I used tKeys in the
repeat for each loop, instead of the keys in the array,
and it has all come together.

Thank you!!

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

Re: [SOLVED] How to delete an arbitrary element from an arra

Post by Klaus » Sat Oct 13, 2012 2:07 pm

My pleasure :D

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”