best way to search an integer array
so I have written code to search char array for something, find it, remove it, and adjust the char array
(using find_string remove _string, etc). What is the best way to do this on an integer array.
Example I am storing numbers in an integer array, I want to search the array for a number, remove it and shift everything up to replace it. Any ideas?
(using find_string remove _string, etc). What is the best way to do this on an integer array.
Example I am storing numbers in an integer array, I want to search the array for a number, remove it and shift everything up to replace it. Any ideas?
0
Comments
Well, I'd do a For Loop, search for the desired number. If found, then set a flag in the loop to start shifting the numbers down.
You didn't say if you wanted to just remove the first instance of the offending number or all of them. That would also have to be determined.
However, if I had to keep an integer array just as a "storage" facility, having no particular connection between an array index and a particular role of the stored value, then I would probably choose a solution similar to a csv file. Store the values as delimited strings in a char array separated with a "," (or whatever).
When stored/reused, ITOA/ATOI will do the job, and you can take advantage of the string manipulation and parsing.
If you are doing a lot of removing from the array, I think that the overhead involved in this method will be a lot less than just recreating the array or shifting values continuously. Of course this all depends on how large the array is. If you are dealing with a maximum of 10 values, you might as well just shift the values.
Jeff
// Name : ==== IntegerCollection_Find ==== // Purpose: // Params : // Returns: // Notes : // define_function integer IntegerCollection_Find(integer _nCollection[], integer _nRequestedItem, integer _nInstance) { stack_var integer _nCurrentItem stack_var integer _nCurrentInstance _nCurrentInstance = 0 for(_nCurrentItem = 1; _nCurrentItem <= length_array(_nCollection); _nCurrentItem++) { if(_nCollection[_nCurrentItem] = _nRequestedItem) { _nCurrentInstance++ if(_nCurrentInstance = _nInstance) return _nCurrentItem } } return 0 } // Name : ==== IntegerCollection_RemoveByIndex ==== // Purpose: Removes an item from the collection at the specified index and repacks the collection. // Params : _oCollection - the collection to remove from // _nIndex - index of the item to remove // Returns: TRUE if found and removed, FALSE otherwise // Notes : // define_function char IntegerCollection_RemoveByIndex(integer _oCollection[], integer _nIndex) { stack_var integer _nLoop _oCollection[_nIndex] = 0 // Shuffle all collection items up by one position if(_nIndex <= length_array(_oCollection)) { for(_nLoop = _nIndex; _nLoop < length_array(_oCollection); _nLoop++) { _oCollection[_nLoop] = _oCollection[_nLoop+1]; } set_length_array(_oCollection, length_array(_oCollection)-1); return TRUE } else { return FALSE } } // Name : ==== IntegerCollectionRemoveByKey ==== // Purpose: Removes a specific integer from the collection // Params : Collection, plus an integer to match // Returns: Returns TRUE if an item was removed, FALSE otherwise // Notes : Item not removed if not found. // define_function char IntegerCollection_RemoveByKey(integer _oCollection[], integer _oKey, integer _nInstance) { stack_var integer _nIndex _nIndex = IntegerCollection_Find(_oCollection, _oKey, _nInstance) if(_nIndex) { IntegerCollection_RemoveByIndex(_oCollection, _nIndex) return TRUE; } return FALSE; }Note that your integer array has to have its length set. If it is populated at run time, you'll need to use set_length_array to resize it as you add items.