Array and Delimiters

  • Thread starter Thread starter Peace
  • Start date Start date
P

Peace

I am having trouble writing code to turn an array into a delimited string.

Dim splitout As String
splitout = Join(DataArray(rows, columns), " ")

rows and columns are integers representing rows and columns quanitity of
datasets.

Could someone help show what I am doing wrong?
When ever I write splitout to to a text file nothing is there.
 
Peace said:
I am having trouble writing code to turn an array into a delimited
string.

Dim splitout As String
splitout = Join(DataArray(rows, columns), " ")

rows and columns are integers representing rows and columns quanitity
of datasets.

Could someone help show what I am doing wrong?
When ever I write splitout to to a text file nothing is there.

How is DataArray declared? It would have to be a 2-dimensional array of
1-dimensional string arrays.

If DataArray is a string array, you can write
splitout = Join(DataArray, " ")

You can also use the shared method System.String.Join.
 
I am sorry for not being complete originally.

rows = dsreportscopy.Tables(0).Rows.Count
columns = dsreportscopy.Tables(0).Columns.Count

Dim DataArray(rows, columns) As Object
For c = 0 To columns - 1
DataArray(r, c) =
dsreportscopy.Tables(0).Columns.Item(c).ColumnName)
For r = 0 To rows - 1
DataArray(r, c) = dsreportscopy.Tables(0).Rows(r).Item(c))
Next
Next

Dim splitout As String
splitout = Join(DataArray(rows, columns), " ")
 
Peace said:
I am sorry for not being complete originally.

rows = dsreportscopy.Tables(0).Rows.Count
columns = dsreportscopy.Tables(0).Columns.Count

Dim DataArray(rows, columns) As Object
For c = 0 To columns - 1
DataArray(r, c) =
dsreportscopy.Tables(0).Columns.Item(c).ColumnName)
For r = 0 To rows - 1
DataArray(r, c) =
dsreportscopy.Tables(0).Rows(r).Item(c))
Next
Next

Dim splitout As String
splitout = Join(DataArray(rows, columns), " ")

Why the first
DataArray(r, c) =
statement? It is outside the loop that uses 'r'.


How do you want to concatenate the dataTable content? One space between each
column and a new line for each record? How do you want to convert the values
to a string?
 
Hi Armin,

One space (actually would prefer a tab but it was nto accepting Chr(9))
between each column and a new line for each record.

How would you have written this?

The end goal is:
(provided by cindy concerning writing an array to a MS Word table)
- Turn the array into a delimited string (using the VB Join function, for
example). The delimiter can be anything you like, just as long as it's a
character in the text. Or, you'd have to put the individual entries into
"quotes" so that it's clear where the character is NOT a delimiter.

- Assign this string to a RANGE in the Word document (rng.Text = s)

- convert the range (containing the delimited string) to a table (the
ConvertToTable method), assigning it to an object variable (Dim tbl as
Word.Table tbl = rng.ConvertToTable('set the params)
 
Hi Cor / Peace,

It looks the basic approach here would work. You're building a comma-delimited
string, yes? However, you should loop columns for each row. Think of the data
as each record's fields are a column in a row:

Field1,Field2,Field3
Data1,Data2,Data3
Data1,Data2,Data3

If you're going this route, no need to put it into an array - you'll already
have the delimited string! Just keep on concatenating the string: After
processing each row, add a vbCR character to the string. (vbCR because this is
what Word understands as "new paragraph", and works with best when converting
delimited strings to tables).

Basically, if you'd copy paste my sample, above, into Word it would convert
with no problems into a table. This is the kind of end-result you need to
generate.
Can you see if this fits your problem?

It makes from a datatable (the part after ds. with a dataset) a arraylist
with comma seperated strings.

Test it very good, because in this way I did not make it before and I become
always confused to check xy, yx
\\\
Dim a As New ArrayList
For i As Integer = 0 To dt.Columns.Count - 1
Dim b As New System.Text.StringBuilder
For y As Integer = 0 To dt.Rows.Count - 1
b.Append(dt.Rows(y)(i))
If y <> dt.Rows.Count - 1 Then
b.Append(",")
End If
Next
a.Add(b.ToString)
Next
///

-- Cindy
 
Hi Cindy,

I was not sure from the message of Peace if it had to be a csv file was what
you had said.

The arraylist and stringbuilder approach I use because that is very fast.
(Of course I could do it in one time)

(to make it even more efficient now I know it has to be a CSV File)if y<>dt.Rows.count - 1 then
b.append(",")
else
b.append(vbcrlf)
end if

And after that it is just making from the strings in the arraylist a csv
file by

For ......... (but I did (and do not want to do everything), that is not
good for Peace) :-))

Although it is almost nothing to do it.

But if I understand it wrong about that csv textfile, please message me.

Cor
 
Cindy,

Are you saying if I copy and paste the array into word it will make a table
out of it?

I am sorry if I am not understanding.
 
For ......... (but I did (and do not want to do everything), that is not
good for Peace) :-))

I appreciate your concern about my wellbeing but it is not like I have not
tried and I have spent a good deal amount of time writing the excel sample
that was connected to this. I am putting in the legwork so please help if
you can. I also am in a production environment which I know you know what
that means and I have spent too much time on this already.
 
Hi Peace,

It is very easy to do and I will when I and you have both an answer from
Cindy,
I think it is extra about 4 lines of code.

Cor
 
Thanks man.....

beginning to sweat this one.

Cor said:
Hi Peace,

It is very easy to do and I will when I and you have both an answer from
Cindy,
I think it is extra about 4 lines of code.

Cor
 
Hi Cor,
But if I understand it wrong about that csv textfile
csv FORMAT, but not an actual file.

Peace needs to put his data in a delimited string format,
carried in a string variable (strMyData, let's call it).
The field delimiter doesn't have to be a comma, it could
also be a TAB character (Chr(9)) (which he has indicated
he'd prefer, as I recall), or anything else. Record
delimiter *must* be a Chr(13).

Once he has that, it has to go into a Word range, then it
can be converted to a Word table, roughly like this:

Dim rng as Word.Range = ActiveDocument.Range
rng.Collapse wdCollapseEnd
rng.Text = strMyData
Dim tbl as Word Table = rng.convertToTable('params here)

Thanks for providing the datatable to string manipulation
stuff in "NET-speak" :-)

-- Cindy
 
Hi Cindy,
this is peace I thought it was safe to go back to my old name.....
SO then, to create the delimited string with my array called datasetarray
how would I do this?
 
Hi Scorpion53061,
SO then, to create the delimited string with my array called datasetarray
how would I do this?
Cor was actually taking care of this part? I'm not that conversant in
NET-speak, yet, to be able to give you "good" code for the non-Word part.

Note that in the original discussion on your question about how to make a
table in Word I gave you an EXAMPLE what you could do. I didn't say you HAD
to have an array. It all depends on where you're starting from...

From the additional information you've provided since, in this thread, it
looks like you should go directly from your data to the delimited string;
an array would be an extra (and unnecessary) step.

What you NEED is the delimited string. How you get there depends entirely
on the specific circumstances. Use what's most efficient or convenient.

-- Cindy
 
sure. The whole exercise was because writing a dataset cell by cell in a
Word table was taking too long.

So hopefully if I can get this array to a delimited string I will be okay
from there. I will wait for Cor.

Thank you much for your help!!
 
Hi Peace,

Complete with test .
The first part is only to build up a table.

I hope it is also as Cindy told it, so lets wait her comments?

Cor

\\\
Option Strict On
Public Module Main
Public Sub Main()
'First the testtable
'Make a datatable with 8 columns
Dim dt As New DataTable
For i As Integer = 0 To 7
Dim dc As New DataColumn(Chr(i + 48))
dt.Columns.Add(dc)
Next
'And 12 rows every column filled with a letter
For i As Integer = 0 To 11
dt.Rows.Add(dt.NewRow)
For y As Integer = 0 To dt.Columns.Count - 1
dt.Rows(i)(y) = Chr(y + 65)
Next
Next
'Conform the wish of Cindy Conventional build string
'brrr but only to show the start of the testtable
Dim Scorpion As String
For i As Integer = 0 To dt.Rows.Count - 1
For y As Integer = 0 To dt.Columns.Count - 1
Scorpion = Scorpion & dt.Rows(i)(y).tostring
If y <> dt.Columns.Count - 1 Then
Scorpion = Scorpion & chr(9)
Else
Scorpion = Scorpion & chr(13)
End If
Next
Next

MessageBox.Show(Scorpion)


'Here start conversion XY to YX
Dim a As New ArrayList
For i As Integer = 0 To dt.Columns.Count - 1
Dim b As New System.Text.StringBuilder
For y As Integer = 0 To dt.Rows.Count - 1
b.Append(dt.Rows(y)(i))
If y <> dt.Rows.Count - 1 Then
b.Append(Chr(9))
Else
b.Append(Chr(13))
End If
Next
a.Add(b.ToString)
Next
Dim Peace As New System.Text.StringBuilder
For i As Integer = 0 To a.Count - 1
Peace.Append(a(i).ToString)
Next
Dim Cindy As String = Peace.ToString

MessageBox.Show(Cindy)
'Text from Cindy
' Dim rng As Word.Range = ActiveDocument.Range
' rng.Collapse(wdCollapseEnd)
' rng.Text = Cindy
' Dim tbl as Word Table = rng.convertToTable('params here)


End Sub
End Module
///
I hope this helps a little bit?

Cor
 
I am frustrated due to lack of the ability to communicate what I am trying
to do. But that is my bad for not explaining it well.

A dataarray with 2 elements (dataarray(r,c)) where r = the rows of a dataset
and c is the columns of a dataset.

We want to write that dataarray to a delimited string.

It is back to the drawing board on this
 
Hi Scorpion,

That is the first part of the sample, just run and try it.
(Although if you wish is I will rewrite the first part also using the
stringbuilder (two rows of code), that is a little bit nicer if it is real a
hugh dataset)

I used a datatable,
but if you read
ds.tables(0) you have a datatable.

The second part of the sample changes xy to yx

Cor
 
Back
Top