Array manipulation

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
lbtony
Posts: 35
Joined: Tue Apr 11, 2006 9:01 am

Array manipulation

Post by lbtony » Thu Jun 01, 2006 3:28 pm

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.....

lbtony
Posts: 35
Joined: Tue Apr 11, 2006 9:01 am

Post by lbtony » Thu Jun 01, 2006 3:56 pm

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...

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

Post by Mark » Thu Jun 01, 2006 4:24 pm

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
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

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Location: Aalst, Belgium
Contact:

Post by Janschenkel » Thu Jun 01, 2006 6:36 pm

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.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

lbtony
Posts: 35
Joined: Tue Apr 11, 2006 9:01 am

Post by lbtony » Fri Jun 02, 2006 12:03 pm

Thank you guys, they are so helpful.
Many thanks.....

Post Reply

Return to “Talking LiveCode”