# Insert A Row Into Existing 2Darray

## Recommended Posts

Hi,

I'm looking to insert a row into an array based upon a mathematical calculation of where it should be in the array.

for example:

array = {{0.1,40}{0.2,60}{0.6,180}}

0.1    40

0.2    80

0.6  180

so if i had new values to be added to the array: newarray =  {{0.4, 110}}

I want them to be added to the array before the last row due to 0.4 being >0.2 but <0.6 (the value in the second column isn't important)

so the array becomes:

0.1    40

0.2    80

0.4  110

0.6  180

But the array assignments can't change, so the value 0.2 is still held in array[1][0] but now 0.6 is in array[4][0]

I was thinking maybe using search() function to return the row of the 1st value in column 0 which is >0.4 and then somehow inserting the new row in to the returned row -1

kind of pseudo code...

array.insertAt(search(array[][0]>newarray[][0],0)-1,newarray)

Is this anywhere near close?

Cheers

##### Share on other sites

You are on the right track, but probably don't want to do it in one line since you need to handle when the new item goes at the end.  Also, you don't want to subtract one because insertAt inserts BEFORE the index provided.

```private index = search(array[][0] > newarray[][0])
if (index == -1) // didn't find, add at end
array.append(newArray)
else
array.insertAt(index, newArray)
endif
```

Also, I'm assuming you wanted commas in your original declaration between each row:

array = {{0.1,40},{0.2,60},{0.6,180}}

BTW, just for those that are curious, to do the above in one line:

execute(search(array[][0] > newarray[][0]) == -1, "array.append(newArray)",  "array.insertAt(search(array[][0] > newarray[][0]), newArray)")

You would never want to do this for this task for several reasons:

1) its much harder to read and understand

2) it requires 2 calls to the search() function.  Search() is quite fast, but if the arrays are long, doing it twice might really add up

3) normally less lines means faster execution in DAQFactory since its an interpretted language, but in this case you still have two lines (instead of 3 in the good code), and more important, execute() and evaluate() are slower than a regular script line because DAQFactory can't do any sort of precompilation.  It has to actually process the string at runtime.

##### Share on other sites

Wow, super fast response thanks!

So, i'm nearly there. I had a problem if the array was empty so added an ifempty clause.

Also, i'm trying to append/insertAt a single value at a specific column (column 18) but it's not working- is there a way to code this? How do you specify dimensions for insertAt or append?

Sorry, i'm pretty new to arrays and struggling to think in multi-dimension!

if (!IsEmpty(array))

private index = search(array[][18] > newvalue)

if (index == -1)

array[][18].append(newvalue)

else

array[][18].insertAt(index, newvalue)

endif

else

method[][18] = newvalue

endif

##### Share on other sites

I'm being stupid, i can define the array position rather than insertAt so:

array[numrows(array)][18] = newvalue

and

array[index][18] = newvalue

##### Share on other sites

although that doesn't work as doesn't insert just overwrites- oops!

##### Share on other sites

InsertAt() only works in the 1st dimension.  You can't just insert into the 19th column (index 18) because you have to insert a full row.  Otherwise, different columns would have different lengths and that wouldn't be a proper 2 dimensional array.  A 2 dimensional array has to be a rectangular grid where every row has the same number of columns, and every column has the same number of rows.  Plus, presumably everything at a particular index means something together, so if you insert just in the 19th column, everything after would be out of align.  What you probably want to do is create the whole row, filling in default values, then adjust the 19th column, then insert.  So something like (I don't know how many columns you have, so just guessed 20):

```private newRow = transpose(fill(0,20),1)  // create an array with 20 columns, all 0
newRow[0][18] = newValue

if (!IsEmpty(array))
private index = search(array[][18] > newvalue)
if (index == -1)
array.append(newrow)
else
array.insertAt(index, newrow)
endif
else
array = newRow
endif
```
##### Share on other sites

PS: Of course you don't have to initialize the array with 0's.

##### Share on other sites

thanks, that makes sense. Because I can't have blank values in the array I'm going to have to have a little think about how I store the data. I've been using an array to store a big table of values as it is super easy to save and load this to a csv file. I think I'll save it with the zeros (or a -999 or something as i need values going negative) and then split the file when I open it into two arrays and remove the zeros.

Anyway, I need a change of strategy. Thanks for your help.

On a related note, when i use file.savedialog function i can't get the other files of the same file type to show up in the dialog box?

i'm calling them .meth files and using the line:

file.savedialog("methodname.meth","C:\method_folder\","*.meth","save method")

it works fine when i use the load dialog and my code is the same. When I load files, all the files that are in the folder show up nicely in the list of files but not when I am saving

cheers

##### Share on other sites

DAQFactory doesn't support null / empty values inside an array.  An array / variable can be completely empty, or it can have values, but not both.  For example, if you did:

global m

m[3] = 5

you'd end up with {0,0,0,5}.  DAQFactory defaulted the array elements to 0 to make an array big enough.  So, you'll have to put in some sort of invalid value for empty values and do a find/replace later.

##### Share on other sites

I've changed the way i'm doing things so I don't have gaps but I still have an issue with the code you specified in your 1st reply. it works fine with just one column but not two

```newarray[][0] = value1
newarray[][1] = value2

if (!IsEmpty(array[][0]))
private index = search(array[][0] > newarray[][0])
if (index == -1) //didn't find, add at end
array.append(newarray)
else
array.insertAt(index, newarray)
endif
else
array = newarray
endif
```
##### Share on other sites

What doesn't work?  Define everything as a private at the top with preset values that demonstrates the problem and post the script so I can see what you mean.

##### Share on other sites

Hi, sorry, yes I realised I hadn't actually said what didn't work!

Yesterday it was putting the two columns of newarray into just one column of array.

But... after a good nights rest and no change to the code at all it seems to be working perfectly and I cannot recreate what was happening yesterday!

should i post my other question as a separate topic?

"On a related note, when i use file.savedialog function i can't get the other files of the same file type to show up in the dialog box?

i'm calling them .meth files and using the line:

file.savedialog("methodname.meth","C:\method_folder\","*.meth","save method")

it works fine when i use the load dialog and my code is the same. When I load files, all the files that are in the folder show up nicely in the list of files but not when I am saving"

Many thanks