Still awaiting that handbook with lots of examples may I ask another elementary question or two:
In an array with 5 values, how can I truncate the first/last 2?
How can I delete values from an array that contain string $string?
Truncating an array
- martin
- Official Nisus Person
- Posts: 5228
- Joined: 2002-07-11 17:14:10
- Location: San Diego, CA
- Contact:
Re: Truncating an array
There are two ways to remove values from an array, either one value at a time, or by removing a whole range all at once. If you want to remove them one at a time, simply remove the first element twice. Just be careful because removing the first value shifts all the other indexes down:js wrote:In an array with 5 values, how can I truncate the first/last 2?
Code: Select all
$names = Array.new('Arya', 'Baratheon', 'Clegane', 'Dany', 'Eddard')
$names.removeValueAtIndex(0) # removes 'Arya' and shifts all entries down, so 'Baratheon' is now at index 0
$names.removeValueAtIndex(0) # removes 'Baratheon'
Code: Select all
$names = Array.new('Arya', 'Baratheon', 'Clegane', 'Dany', 'Eddard')
$deleteRange = Range.new(0,2)
$names.removeValuesInRange($deleteRange)
Code: Select all
$names = Array.new('Arya', 'Baratheon', 'Clegane', 'Dany', 'Eddard')
$count = $names.count
If $count > 1 # the array must have at least two values to remove!
$deleteRange = Range.new($count - 2, 2)
$names.removeValuesInRange($deleteRange)
End
Code: Select all
$names = Array.new('Arya', 'Baratheon', 'Clegane', 'Dany', 'Eddard')
$count = $names.count
If $count > 1
$subRange = Range.new(0, $count - 2)
$names = $names.subarrayInRange($subRange)
End
You'll need to inspect all values in the array, and for each one, if some condition is met, remove that value from the array. The trick is to keep in mind that removing a value from an array changes the indexes of all values that come after it. So the easiest is to just process the array back to front, that way you don't have to worry about the indexes changing.How can I delete values from an array that contain string $string?
This example will remove all names containing the letter 'e':
Code: Select all
$names = Array.new('Arya', 'Baratheon', 'Clegane', 'Dany', 'Eddard')
$index = $names.count - 1
While $index >= 0
$name = $names[$index]
# check if the value should be removed
If $name.rangeOfString('e', 'i')
$names.removeValueAtIndex($index)
End
$index -= 1 # move to the previous index/value
End
Code: Select all
$names = Array.new('Arya', 'Baratheon', 'Clegane', 'Dany', 'Eddard')
$index = $names.count - 1
While $index >= 0
$name = $names[$index]
# check if the value should be removed
If $name.find('[aeiou]$', 'Ei')
$names.removeValueAtIndex($index)
End
$index -= 1 # move to the previous index/value
End