Page 1 of 1

Array manipulation

Posted: Thu Jun 01, 2006 3:28 pm
by lbtony
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.....

Posted: Thu Jun 01, 2006 3:56 pm
by lbtony
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...

Posted: Thu Jun 01, 2006 4:24 pm
by Mark
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

Posted: Thu Jun 01, 2006 6:36 pm
by Janschenkel
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.

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 :-)
For the 'Delete' button, use the following script.

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
For the 'Save changes' button, use the following script.

Code: Select all

on mouseUp
  SavePropertiesData
end mouseUp
Finally, the 'Revert changes' button would have the following script.

Code: Select all

on mouseUp
  LoadPropertiesData
end mouseUp
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.

Posted: Fri Jun 02, 2006 12:03 pm
by lbtony
Thank you guys, they are so helpful.
Many thanks.....