Page 2 of 2

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

Posted: Fri Oct 12, 2012 12:10 am
by Klaus
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

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

Posted: Fri Oct 12, 2012 12:48 am
by hylton
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"?

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

Posted: Fri Oct 12, 2012 1:09 am
by mwieder
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.

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

Posted: Fri Oct 12, 2012 1:15 am
by sritcp
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.

Re: How to delete an arbitrary element from an array

Posted: Fri Oct 12, 2012 1:26 am
by hylton
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.

Re: How to delete an arbitrary element from an array

Posted: Fri Oct 12, 2012 1:37 am
by hylton
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?

Re: How to delete an arbitrary element from an array

Posted: Fri Oct 12, 2012 2:47 am
by mwieder
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.

Re: How to delete an arbitrary element from an array

Posted: Fri Oct 12, 2012 4:19 am
by sritcp
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.

Re: How to delete an arbitrary element from an array

Posted: Fri Oct 12, 2012 7:31 am
by Mark
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

Re: How to delete an arbitrary element from an array

Posted: Sat Oct 13, 2012 10:30 am
by hylton
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.

Re: How to delete an arbitrary element from an array

Posted: Sat Oct 13, 2012 12:41 pm
by Klaus
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

Re: How to delete an arbitrary element from an array

Posted: Sat Oct 13, 2012 2:02 pm
by hylton
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!!

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

Posted: Sat Oct 13, 2012 2:07 pm
by Klaus
My pleasure :D