Create an Array from sub arrays vb.net - arrays

I have a string array A where I store some values for instance A(1) = "a,b,c" A(2)="1,2" etc. From that array I am creating subarrays using A(1).split(",") and I have
dim subArr1() as string = {"a","b","c"}
dim subArr2() as string = {"1","2"}
etc
Now, I want to create a new two-dimensional array
dim all()() as string = {subArr1, subArr2, ...}
The initial array is created dynamically and could be have 2, 5, or whatever number of items. So I could have any number of subArrays (subArrX)
Any idea how to deal with that? I am writing in vb.net 2013
Thank you

You could use this little LINQ query:
Dim A As String() = {"a,b,c", "1,2"}
Dim parts As IEnumerable(Of String()) = From str In A Select str.Split(","c)
Dim all()() As String = parts.ToArray()
Now the array contains two arrays, the first contains "a","b" and "c" and the second contains "1" and "2".

Related

Initializing a dictionary w/ the values being (Of String, String[]) in VB

I had the idea of creating an array of empty strings:
Dim elements as String = {"%", "&", "#"}
Then creating the dictionary as such:
Dim dictionary = New Dictionary(Of String, elements)
Finally looping through the range of the excel sheet from bottom up (lastindex specified as lenExcel from a function I have to get the last row in a sheet).
for lenExcel To 0 step -1:
Dim faa = cDec(sheet.Range((lenExcel & num).ToString)
If faa = 10.2 Then
elements[0] = sheet.Range(lenExcel & value1).toString
elements[1] = sheet.Range(lenExcel & value2).toString
elements[2] = sheet.Range(lenExcel & value3).toString
With Dictionary
.Add Key:=faa, Item:=elements
End With
Rows(lenExcel).Delete
Next
However, I think this will not work because the dictionary will hold a reference to the elements array. Hence, all of the array values contained within the dictionary for all keys would be the last values given to the elements array.
A possible solution would be to initialize a new array within the dictionary's parameters. That being said, I do not know VB and would appreciate some insight.
Thank you.

How to extract a part of array by referring start and end index just like what we do in Matlab

For example, if we have an array A.
In matlab, we just use A[a:b] to get a sub array easily,where a,b are start point and end point respectively.
Is there similar way to do it in VBA?
Thanks
Working with arrays is incredibly fast so this will probably give no discernable benefit - although I can understand how it may appeal from a coding sense than looping to fill a smaller array
Given you are working with a single element array you could:
Introduce a "marker" string inside the large array.
Join the large array with a delimiter into a single string.
Split the large array by the "marker" string, then separate the reduced string into a smaller array with the delimiter.
The code below dumps the numbers 1 to 100 into an array, and then splits it as above to pull out the first 10 records.
Sub test()
Dim bigArr
Dim subArr
Dim strSep As String
Dim strDelim As String
Dim strNew As String
Dim rowBegin As Long
Dim rowEnd As Long
strDelim = ","
strSep = "||"
'fill array with 1 to 100
bigArr = Application.Transpose(Application.Evaluate("row(1:100)"))
rowBegin = 1
rowEnd = 10
bigArr(rowEnd + 1) = strSep
'make a single string
strNew = Join(bigArr, strDelim)
'split the string at the marker
vArr = Split(strNew, strSep)
ReDim subArr(rowBegin To rowEnd)
'split the smaller string with the desired records
subArr = Split(Left$(vArr(0), Len(vArr(0)) - 1), strDelim)
End Sub

How to get the index of element from contains function of array in vb.net

i have a list of large number of string elements in array
and i am using contains function to check if it contains that element. it is working fine. Now i want to get to know the index/position of element.
suppose the array is
dim s as string() = {"first", "second","third"}
and the string
dim l as string = "third"
method
dim b as boolean = s.Contains(l, StringComparer.CurrentCultureIgnoreCase)
flag
if (b) Then
messagebox.show("It exists")
end if
above array is just an example. original array consists of 7690 entries and each entry is written in utf-8 and indexOf function is not giving any result
First of all, you should consider using a List(Of T) when writing in VB.Net.
The List class provides an List(Of T).IndexOf Method (T).
You can do something like this :
Dim index As Integer = YourList.IndexOf(l)
I believe you're looking for the IndexOf function.
UPDATE: I came up with the following quick example that encoded a string similar to your example string into UTF-8 and it still works:
Dim s As String() = {"first", "second", "third", "four", "five", "six"}
For Each tempString As String In s
Dim bytes As Byte() = Encoding.Default.GetBytes(tempString)
tempString = Encoding.UTF8.GetString(bytes)
Next
Dim l As String = "six"
Debug.Print(Array.IndexOf(s, l))

vb.net how do i find the # of indexes in an array

In vb.net I want to split a string into an array, I also want to be able to know how many indices are in the array.
In vb6
I would write it like this
dim v1, arrIN(), idcCount
v1 = "1,2,3,4,5"
arrin() = split(v1,",")
idcCount = ubound(arrin))
I can get this to actually put those values into an array using:
Dim arrIN() = Split(v1, ",")
But I cannot figure out how to get the count of indices
When I try to test this in the immediate window I get the message below
?UBound(arrIN())
Number of indices is less than the number of dimensions of the indexed array.
You may use arrVar.GetUpperBound(0) and arrVar.GetLowerBound(0) methods.
Have look at MSDN reference : Array.GetUpperBound(dimension)
Dim v1 As String = "1,2,3,4,5"
Dim arrIN() As String = Split(v1, ",")
'Gets the total number of elements in the array
Dim a As Integer = arrIN.Length
'Gets the index of the last element
Dim b As Integer = arrIN.GetUpperBound(0)
In this instance:
arrIN.Length = 5
arrIN.GetUpperBound(0)=4
The zero in GetUpperBound(0) is the dimension that you want the upper index for.
AVD gave you the right answer. I just added arrIN.Length just in case you needed it.

How to assign a set of text values to a string array?

How can I assign a set of text values to an array? Nothing I tried is working!
Months = Array("Jan", "Feb", ..., "Dec")
and others I tried do not work!
Here's something about VB: http://www.devx.com/vb2themax/Tip/18322
Visual Basic doesn't provide any way
to declare an array and initialize its
elements at the same time. In most
cases you end up with setting
individual elements one by one, as in:
Dim strArray(0 To 3) As String
strArray(0) = "Spring"
strArray(1) = "Summer"
strArray(2) = "Fall"
strArray(3) = "Winter"
Under VB4, VB5, and VB6 you can create
an array of Variants on the fly, using
the Array() function:
Dim varArray() As Variant
varArray() = Array("Spring", "Summer", "Fall", "Winter")
but there is no similar function to
create arrays of data types other than
Variant. If you're using VB6, however,
you can create String arrays using the
Split() function:
Dim varArray() As String
' arrays returned by Split are always zero-based
varArray() = Split("Spring;Summer;Fall;Winter", ";")
I'm pretty sure you can only do it like this:
dim months(2) as string
months(0) = "Jan"
months(1) = "Feb"
months(2) = "Mar"
If you're talking about vbscript then this works:
months = Array("may","june","july")
If it's vb.net then:
dim months() as string = {"may","june","july"}

Resources