Welcome! Log In Create A New Profile

Advanced

Add columns to a String Array

Posted by Geoff Spillane 
Add columns to a String Array
March 16, 2022 02:20PM
WinDev27 - I wish to use a 2 dimensional array of strings but I don't know the size of the dimensions before I start. I can use ArrayAddLine to increase the row dimension and I thought I could use AddColumn to increase the column dimension but that gives me an error "This function is not available for an element of 'array (ANSI string)' type". Is there a way to achieve this?
Re: Add columns to a String Array
March 17, 2022 08:06AM
Hi,

The functions ArrayInfo(yourarray, tiNumberRows) and ArrayInfo(yourarray, tiNumberColumns) returns the number of rows and columns respectively.

Regards

Vassilis Boutsikas
Re: Add columns to a String Array
March 17, 2022 08:27AM
Thank you for your reply Vassilis, but this is only possible to use after I already have the array.

To clarify I have a CSV file that is too complex to use the CSVtoArray function so I need to interrogate the complete file before the number of columns and number of rows are known to be able to define the Array to assign the values of each field.

What I do at the moment is define the Array as dynamic, then run a procedure to count the number of columns (nCol) in the first row, then establish the Array as = new array of 1 by nCol string. I can then use ArrayAddLine to handle the dynamic increase of the number of rows as it processes the whole file.

It would have been easier if there was another function such as AddColumn that could have been used in this instance.

Best Regards,
Geoff
Re: Add columns to a String Array
March 18, 2022 01:55PM
…. Out



Edited 1 time(s). Last edit at 03/19/2022 12:57PM by cabinetman.
Re: Add columns to a String Array
March 18, 2022 11:58PM
Hi Dennis,

Before you make a sarcastic reply you need to check the advice you give is relevant.

I have already been through all those Array functions and nothing there allows me to dynamically add a column to an Array.

Of course you are putting yourself forward as being much wiser so perhaps you can point to the function that will achieve this.

Geoff
Re: Add columns to a String Array
March 19, 2022 05:46PM
Hi Geoff

Not tested but this looks like the way to go with standard arrays that are dynamic in WX.
x is array dynamic of [0,0] string
AddLine(x,"A","B","C")
AddLine(x,"D","E","F","G")
AddLine(x,"H")

I think the management of the columns (4 in this case but not filled in each 1st dimension) is handled automatically in this case.
To test I'ld say...

Cheers

Peter Holemans
Re: Add columns to a String Array
March 20, 2022 12:54AM
Hi Peter,

Thank you for your efforts, however this doesn't work either. The row dimension increases due to the AddLine function but the column dimension remains at 0 elements and therefore in your example, trying to subsequently reference say x[2,4] which you might expect to produce "G", it produces an error because the array remains with 0 elements in the column dimension.

Best Regards,
Geoff
Re: Add columns to a String Array
March 20, 2022 07:06PM
Hi again

OK, thinking out of my head (not tested) this should work then...
x is array dynamic of [0,0] string
RedefArray(3)
AddLine(x,"A","B","C")
RedefArray(4)
AddLine(x,"D","E","F","G")
RedefArray(1)
AddLine(x,"H")

INTERNAL PROCEDURE RedefArray(NumColsNeeded) 
   NumCols is int = ArrayInfo(x,tiNumberColumns)
   If NumCols < NumcolsNeeded then 
   for i = NumColsNeeded to NumCols Step -1
      AddColumn(x)
   end
END

Cheers

Peter Holemans
Re: Add columns to a String Array
March 21, 2022 12:13AM
Hi Peter,

Thanks once again for your efforts. If you have a quick look at my original post, you'll see that I have already tried using AddColumn but it is not allowed in this instance.

Given that what I'm doing is creating an advanced version of the CSVtoArray function, and therefore reading a complex CSV, what I finished up doing was to parse just the first row of the CSV to determine the number of columns used, then used the following:
MyDynamicArray = new array of 1 by ColNumbers string

I could then use AddLine to dynamically increase the number of rows.

Best Regards,
Geoff
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: