best way to search an integer array
samos
Posts: 106
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
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.