|Truncating an array
|Page 1 of 1|
|Author:||js [ 2010-06-08 08:36:40 ]|
|Post subject:||Truncating an array|
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?
|Author:||martin [ 2010-06-08 09:23:33 ]|
|Post subject:||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:
If you wanted to do it all at once:
Removing the last two values is the same, just with different indexes. Of course, mostly one doesn't know how many values are in an array, so we have to calculate the index using the array's count:
In all this, there is another strategy. Rather than deleting parts of the array, we could just extract a subset of the original array. Whether or not this is useful depends on your macro's code/situation, but I'll show it here just in case. Again, removing the last two entries:
You could also assign the subarray to a new variable, keeping the original/full array intact.
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.
This example will remove all names containing the letter 'e':
You can see the 'i' option is given to "rangeOfString", so that the match is case insensitive. You might want to get fancier and match using PowerFind/regex expressions. If so, one can use the "find" command. Here's an example that removes all names ending in a vowel:
|Page 1 of 1||All times are UTC - 8 hours|
|Powered by phpBB® Forum Software © phpBB Group