Suppose I have a array, and I don't know the number of elements in it and I also don't know the names of the elements in it.
In this situation how can I iterate through this array, find the element with the particular name and delete it?
The reason I'm asking this is that I have a customprop set with many elements in it, and I want to display these elements in a list field and allow users to updating the customprop set by doing deletion/insertion at the list field.
Thank you.....
Array manipulation
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
I have one inefficient solution which is something like:
put customproperties["thePropSetName"] into myArray
set the customproperties["thePropSetName"] to empty
put empty into myNewArray
put the elements of the list field into eleNames
// because user has deleted an item in the list field now
repeat with a=1 to the number of items of eleNames
put item a of eleNames into eleName
put myArray[eleName] into myNewArray[eleName]
end repeat
set customproperties["thePropSetName"] to myNewArray
but what I'm really after is to directly deleting on the array or the elements of a property set.
Thank you...
put customproperties["thePropSetName"] into myArray
set the customproperties["thePropSetName"] to empty
put empty into myNewArray
put the elements of the list field into eleNames
// because user has deleted an item in the list field now
repeat with a=1 to the number of items of eleNames
put item a of eleNames into eleName
put myArray[eleName] into myNewArray[eleName]
end repeat
set customproperties["thePropSetName"] to myNewArray
but what I'm really after is to directly deleting on the array or the elements of a property set.
Thank you...
I assume, by element, you actually mean the key or record in the array that you want to delete. (A record consists of a key which refers to the element). You will need to know which element you want to delete. If you know the key that refers to that element, this will be the easiest way to do it:
combine myArray by return and tab
filter myArray without (myKeyToDelete & tab & "*")
split myArray by return and tab
combine myArray by return and tab
filter myArray without (myKeyToDelete & tab & "*")
split myArray by return and tab
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
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
-
- VIP Livecode Opensource Backer
- Posts: 977
- Joined: Sat Apr 08, 2006 7:47 am
- Location: Aalst, Belgium
- Contact:
Hi Tony,
My first idea would be to copy the customPropertySet into an array, as you have done. Then display the keys of that array in a field, and whenever the user deletes one, delete that item from the array. Finally, update the customPropertySet with the data from the modified array.
Some pieces of code that might come in handy; toss them into the card script.
For the 'Delete' button, use the following script.
For the 'Save changes' button, use the following script.
Finally, the 'Revert changes' button would have the following script.
This method is using a script local variable in your card to act as a 'proxy' to your custom property data. As you can see, this approach allows you to revert changes in a single step.
Hope this gets you started,
Jan Schenkel.
My first idea would be to copy the customPropertySet into an array, as you have done. Then display the keys of that array in a field, and whenever the user deletes one, delete that item from the array. Finally, update the customPropertySet with the data from the modified array.
Some pieces of code that might come in handy; toss them into the card script.
Code: Select all
## Script local variables
local sCustomPropsA
## Handler to extract the data from the property set and put it into a field
on LoadPropertiesData
put the customProperties["MySet"] of me into sCustomPropsA
put the keys of sCustomPropsA into field "PropertyList"
end LoadPropertiesData
## Handler to save the data from the local array into the propertySet
on SavePropertiesData
set the customProperties["MySet"] of me to sCustomPropsA
end SavePropertiesData
## Handler to delete an element from the local array (not the propertySet yet!)
on DeletePropertyNamed pName
delete local variable sCustomPropsA[pName]
end DeletePropertyNamed
## Some more handler could come in handy, to read and update properties by their name
## Those are an exercise to the reader :-)
Code: Select all
on mouseUp
put the hilitedLines of field "PropertyList" into tLines
-- in case more than one line is selected, work from bottom to top
sort items of tLines numeric descending
repeat for each item tLine in tLines
DeletePropertyNamed (line tLine) of field "PropertyList"
delete line tLine of field "PropertyList"
end repeat
end mouseUp
Code: Select all
on mouseUp
SavePropertiesData
end mouseUp
Code: Select all
on mouseUp
LoadPropertiesData
end mouseUp
Hope this gets you started,
Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com
www.quartam.com