Update string array

I'm sure it's simple but I can't find the answer so I'm hoping someone can help.
All I want to do is add (or remove) values to a string array

How do I change

string strValues[] = {"CAT", "DOG"}


to

 

string strValues[] = {"CAT", "DOG", "MOUSE", "DUCK"}


or visa versa

----
Martin

 


Martin_Hunter - Wed Nov 23 11:33:59 EST 2011

Re: Update string array
OurGuest - Wed Nov 23 12:21:06 EST 2011

You don't change static arrays but if you look in the help file you will find a section under dynamic arrays which are diffent thant the static arrays you used in your example.

Re: Update string array
llandale - Wed Nov 23 13:21:37 EST 2011

"array" with a lower-case "a" is something you can do to other data types, they are defined at a static size; you cannot do what you suggest. "Array" with a capital "A" is a separate type and you can "get" and "put" data into it and it dynamically expands as needed. You MUST keep track of what kind of data you put in the cells and you MUST keep track yourself of how many Rows and Columns you have populated.

Typically you will have a fixed number of columns and add rows dynamically. Typically also you have a sibling Skip list you use to index things in your Array; which is far faster than searching of it.

There is some way to use a string "Array" as a character "array" but I don't know about that.

  • Louie

Re: Update string array
LindyEd - Wed Nov 23 13:50:25 EST 2011

If you know the max size of the string array you could just prefill it with empty strings. The following would first print:

CAT
DOG
<cr>(These are blank carriage returns)
<cr>
It would then print
CAT
DOG
MOUSE
DUCK
 

int i
string strValues[] = {"CAT", "DOG", "", ""}
for i in 0 : 3 by 1 do
{
    print strValues[i] "\n"
}
 
strValues[2] = "MOUSE"
strValues[3] = "DUCK"
 
for i in 0 : 3 by 1 do
{
        print strValues[i] "\n"
}

Re: Update string array
Thursday1036PM - Wed Nov 23 15:24:23 EST 2011

LindyEd - Wed Nov 23 13:50:25 EST 2011

If you know the max size of the string array you could just prefill it with empty strings. The following would first print:

CAT
DOG
<cr>(These are blank carriage returns)
<cr>
It would then print
CAT
DOG
MOUSE
DUCK
 

int i
string strValues[] = {"CAT", "DOG", "", ""}
for i in 0 : 3 by 1 do
{
    print strValues[i] "\n"
}
 
strValues[2] = "MOUSE"
strValues[3] = "DUCK"
 
for i in 0 : 3 by 1 do
{
        print strValues[i] "\n"
}

Just to expand on Lindy's post, if you need a very large number just throw in an array size. Then maybe also keep an integer representing the number of valid entries in the array.

string strValues[100]
int strValuesSize = 0


Then you could implement something like this...

 

void add(string strValue)
{
    strValues[strValuesSize++] = strValue
}
 
void remove(int index)
{
    int i
    for (i = index + 1; i < strValuesSize; i++)
    {
        strValues[i - 1] = strValues[i]
    }
    strValuesSize--
}
 
void printAll(void)
{
    int i
    for (i = 0; i < strValuesSize; i++)
    {
        print strValues[i] "\n"
    }
}


Then your original example would be something like...

 

 

add("CAT")
add("DOG")
printAll()
 
add("MOUSE")
add("DUCK")
printAll()

 

Re: Update string array
Martin_Hunter - Thu Nov 24 02:30:53 EST 2011

Thanks everyone for your feedback.
I also found out that once a string array is defined it cannot be overwritten or deleted!