[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

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

[Solved] How to delete an arbitrary element from an array

Post by hylton » Wed Oct 10, 2012 11:46 am

Hello,

I just downloaded the LiveCode trial a few days ago and am loving it!

I have two questions to start off with that I need help with please:

1. How do I change this weird forum username that has been assigned to me: hyltonclarkeBUSH1Ex?

2. I have worked through the first three TickedOff videos. Currently, I am close to having it working, except I am struggling with the delete task action.

I have implemented my code using handlers that can all access a global array gTaskList.

The following delete line does not work, it clears out the entire gTaskList:

Code: Select all

on deleteTask
   delete element gHilitedLine of gTaskList
   go to card "list"
end deleteTask
gHilitedLine is a global variable that stores the value of the line that the user clicked on. Example, the user clicked on line 1, then gHilitedLine = 1.
gTaskList is the array that stores the Task Title and Task Description.

So my question is, how do I delete a single arbitrary element within an array?

Example, if the array has 5 elements, how do I delete the third element of the array only, and leave the rest in the array intact?

Thank you.

Hylton
Last edited by hylton on Mon Apr 06, 2015 9:17 am, edited 4 times in total.

monte
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1564
Joined: Fri Jan 13, 2012 1:47 am
Contact:

Re: How to delete an arbitrary element from an array

Post by monte » Wed Oct 10, 2012 11:56 am

Delete variable gTaskList[gHilitedLine]
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

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

Re: How to delete an arbitrary element from an array

Post by hylton » Wed Oct 10, 2012 12:01 pm

Hi monte

Unfortunately, that doesn't work.

Here is why:

Create an array with 3 elements, and gTaskList looks like:

1
2
3

Now, using your suggestion to delete the first entry, it works, so now gTaskList looks like this:

2
3

Now try and delete the first task again, using gTaskList[1] and it fails, because 1 no longer exists.

Hence the reason for this thread :)

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

Re: How to delete an arbitrary element from an array

Post by Klaus » Wed Oct 10, 2012 1:25 pm

Hi Hylton,

welcome to the forum!
hyltonclarkeBUSH1Ex wrote:1. How do I change this weird forum username that has been assigned to me: hyltonclarkeBUSH1Ex?
I'm afraid you can't!

Since these usernames are created automatically, you have three options:
1. write to "support@runrev.com" and ask if they can change it
2. create a new account with a new username, but I don't know if this is easily possible.
3. try to live with it :)
hyltonclarkeBUSH1Ex wrote:2. I have worked through the first three TickedOff videos. Currently, I am close to having it working, except I am struggling with the delete task action.
I have implemented my code using handlers that can all access a global array gTaskList.
The following delete line does not work, it clears out the entire gTaskList:

Code: Select all

on deleteTask
   delete element gHilitedLine of gTaskList
   go to card "list"
end deleteTask
gHilitedLine is a global variable that stores the value of the line that the user clicked on. Example, the user clicked on line 1, then gHilitedLine = 1.
gTaskList is the array that stores the Task Title and Task Description.

So my question is, how do I delete a single arbitrary element within an array?
Hm, if the number in gHilitedLine does not correspond to the keynumber of your array, you need to do something different 8)

I would use a repeat loop and search the for the CONTENT of that key, of which you don't know the number like this:

Code: Select all

...
put line gHilitedLine of fld "that the user clicked on" of cd x into tContent
repeat for each key tKey in gTaskList

  ## Found the right key?
  if gTaskList[tKey] = tContent then

   ## Then delete it:
    delete variable gTaskList[tKey]
    exit repeat
  end if
end repeat
...
Don't worry, this will be fast enough, even with 1000s of keys in the array, but will of course only work with unique content in the keys.
If you have doublettes in it, this will only delete the FIRST occurrence of that content.


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 » Wed Oct 10, 2012 4:15 pm

Hi Klaus

Thank you very much for the welcome and for the detailed answer!

To quote the movie 300 "This is blasphemy! This is madness!" :D

Surely, what I am asking is fundamentally basic to working with arrays?

I thought of a loop, but I would then have to consider both keys within each element to avoid exactly what you have suggested, duplicate entries.

To code a robust application, we need to consider these pitfalls.

So, my question to you is:

1. Is it better to rewrite the complete array each time a change is made, so that the array keys are rewritten or
2. Is there a built-in function that can reset the array IDs after an array element has been deleted or
3. I create a third key inside the array which I reset and query instead of the builtin array IDs?

Also, in the help files, I find very little info about arrays. Surely this is pretty fundamental stuff we are talking about? I would have expected a whole section in the dictionary just talking about arrays and all the functions that can be applied to them.

Kind regards
Hylton

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

Re: How to delete an arbitrary element from an array

Post by Klaus » Wed Oct 10, 2012 4:54 pm

Hi Hylton,
hyltonclarkeBUSH1Ex wrote: 1. Is it better to rewrite the complete array each time a change is made, so that the array keys are rewritten or
It depends, but In your special case where corresponding (to userselected line numbers) keys are obviously a must, I'd say yes.

To do so, use also the "repeat for each" loop:

Code: Select all

...
## repeat for each is MUCH MUCH MUCH faster than "repeat with i = ..." so, we simply use a counter to count ourselves :-)
put 0 into tCounter
repeat for each key tKey in gTaskList
  add 1 to tCounter
  put gTaskList[tKey] into gTaskList2[tCounter]
end repeat
...
hyltonclarkeBUSH1Ex wrote:2. Is there a built-in function that can reset the array IDs after an array element has been deleted or
Not that I knew.
Check the "User Guide" (Menu: Help) for "Array variables", but not too much info there...
hyltonclarkeBUSH1Ex wrote:3. I create a third key inside the array which I reset and query instead of the builtin array IDs?
Sorry, don't get this? But maybe yes :D


Best

Klaus

Adrian
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 79
Joined: Wed Jul 11, 2012 5:03 pm

Re: How to delete an arbitrary element from an array

Post by Adrian » Thu Oct 11, 2012 10:20 am

If the task list were a simple variable with each task on a line, rather than an array, you could perhaps remove an item using Filter (filter gTaskList without gHighlightedLine)?

Note that I'm new and not an expert, so this (a) may not suit your needs now (but is worth knowing for other applications); and (b) could be rubbish!

Cheers,

Adrian

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 » Thu Oct 11, 2012 10:38 am

Hi Hylton,

Why do you want to delete an element but not its key? Very often, my keys aren't numeric. The keys tell something about the element, e.g. because the key is a unique ID number. If I delete an element, usually I also want to delete its key.

Having said that, it doesn't mean that you can't do what you want:

Code: Select all

on foo
     put "aa" into x[1]
     put "bb" into x[2]
     put "cc" into x[3]
     delete variable x[1]
     combine x by cr
     split x by cr
     put the keys of x
end foo
This script only works with sequential numerical keys.

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

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

Re: How to delete an arbitrary element from an array

Post by Klaus » Thu Oct 11, 2012 11:06 am

Ah, yes, I always forget about "split" and "combine"! :shock:

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

Re: How to delete an arbitrary element from an array

Post by hylton » Thu Oct 11, 2012 8:27 pm

Adrian wrote:If the task list were a simple variable with each task on a line, rather than an array, you could perhaps remove an item using Filter (filter gTaskList without gHighlightedLine)?

Note that I'm new and not an expert, so this (a) may not suit your needs now (but is worth knowing for other applications); and (b) could be rubbish!

Cheers,

Adrian
Hi Adrian

Thanks for the tip. I am trying to do this with an array because of the nature of the TickedOff application. It needs to keep track of the Task Title and the Task Description. My thinking was to use 1 array with child elements instead of 2 variables.

Mark wrote:Hi Hylton,

Why do you want to delete an element but not its key? Very often, my keys aren't numeric. The keys tell something about the element, e.g. because the key is a unique ID number. If I delete an element, usually I also want to delete its key.
Hi Mark

That is not where I was having a problem. My problem was, that if I delete an element in an array, the built-in array ID dies with it. If I delete element 1, element 1 no longer exists. According to LiveCode, the first element in the array now has a built-in array ID of 2. This is where this thread stemmed from. How do I delete the first element if it is no longer array id 1.

I now see, that in your example, you have created your own numeric array ID as one of the keys.

That is something that I did not consider when I started this thread.

Code: Select all

combine x by cr
split x by cr
put the keys of x
Can you please explain what those three lines do?

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 » Thu Oct 11, 2012 8:36 pm

Hi,

This three lines re-create all keys, starting at 1 again. The combine command makes a list of the elements of the array and the split commands creates a new array from that list, with new keys starting at 1.

Although my solution works, I still think it is not the correct way to deal with arrays. Keep in mind that the sort order of the elements may change, when you re-create the keys.

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 » Thu Oct 11, 2012 9:04 pm

Hi Mark

Thanks for the help, but I am not getting this. I can't get your code to work in the context of my stack. A Message Box (Single Line) pops up and the task list field on card "list" is empty.

Code: Select all

on deleteTask
   delete variable gTaskList[gHilitedLine]
   combine gTaskList by cr
   split gTaskList by cr
   put the keys of gTaskList
   go to card "list"
end deleteTask
Please can you explain to me, what is the correct way of working with arrays?

My question seems simple enough, if I delete the first element in the array, how do I delete the new first element in the array?

monte
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1564
Joined: Fri Jan 13, 2012 1:47 am
Contact:

Re: How to delete an arbitrary element from an array

Post by monte » Thu Oct 11, 2012 9:23 pm

Why not delete variable tArray[min(replaceText(keys(tArray,cr,comma))]

Another option would be to increment your task number when you delete the previous task.
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

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

Re: How to delete an arbitrary element from an array

Post by hylton » Thu Oct 11, 2012 9:44 pm

Hi Monte

We are getting closer!!

My issue is not with line 1 per say. That was where my logic was failing before, but this problem is true for any array ID.

If I delete array ID 5, or the fifth element :D it no longer exists and I can't reference the new element which is actually called array ID 6 (element 6), but is now in the 5th elements position.

Can you make your solution more general, to deal with any element, being deleted and then followed by any other element being deleted?

That is why I asked if the array IDs can be reset somehow. If I can rewrite the array back into itself after each delete, then the problem with these disappearing IDs will be solved. The array will be all fresh and new and none of the IDs will be missing.

Many solutions have been posted in this thread, but I can't get any of them to work. This is due to my lack of experience, not the solutions that have been provided.

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

Re: How to delete an arbitrary element from an array

Post by hylton » Thu Oct 11, 2012 10:25 pm

Klaus!!

Your solution worked!! :D

Code: Select all

on deleteTask
   delete variable gTaskList[gHilitedLine]
   put 0 into tCounter
   repeat for each key tKey in gTaskList
      add 1 to tCounter
      put gTaskList[tkey] into tTaskListTemp[tcounter]
   end repeat
   put tTaskListTemp into gTaskList
   go to card "list"
end deleteTask
To actually see this handler work was epic!!

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”