PDA

View Full Version : Ideas for dynamic Array-constructor on the fly



ReneMiner
16-07-2013, 08:53
the idea came up in this discussion (http://www.thinbasic.com/community/showthread.php?t=12157) already and was continued in several others...

As I remember there is some use of Brackets to assign values to multidimensional arrays in a matrix-way somehow like this:



Dim A(2,3) As Long = [ 1, 2, 3] ,_
[ 4, 5, 6]


but might be I'm wrong here and messed something up because I can not find it in the help any more, where I thought I read this... or it was in some example of Petr?

Anyway- the idea is to use brackets as an array-constructor on the fly in a few different ways. Most usual would be this:


Dim A() As Long = [1, 2, 3, 4, 5]
'as a replacement for
Dim A(5) As Long = 1, 2, 3, 4, 5

which does not seem to be of any use at all on the first sight :D
Maybe sense grows in this example:


Dim B() As Double = [ 1.23, 2.34, 3.45, [ 4.56, 5.67, 6.78 ]]
' to replace
Dim B(3,2) As Double = ...

still not very convinient. Imagine passing a few different values to some function as passing some array as function parameters alike this:


Sub demonstrate1( ByVal S() as String )
'...
End Sub

' prepare some variable-content:
String xx = "test"
Dim yy(123) As String ' fill this somehow in your imagination

' and call this way now:

demonstrate1( ["Hi", xx, yy(7), yy(9)] )

' would create/pass local String-array S(4) with
' these values to the sub above

'...

Sub demonstrate2( ByRef S() as String )

' the single variable-references get treated inside this sub
' as if it was a local array S() in one row so one could
' perform thinBasic-Array-methods on them now - and even
' could Sort content of different variables
' of course this way will not accept a simple "Hi" as above

End Sub


to differentiate this from that:



Sub demonstrate3( S1() As String, S2() As String )
'...
End Sub
'passing this:
demonstrate3(["I", "am", "S1", ["Second", "Dimension", "S1"]], ["First", "Dimension", "S2", "Here"] )

will create local array S1(3, 2) and S2(4)

more dimensions perhaps


Dim a() as String = [ "1.1.1","2.1.1","3.1.1",["1.2.1","2.2.1","3.2.1",["1.3.1","2.3.1","3.3.1",["1.4.1","2.4.1","3.4.1"]]], _
"1.1.2","2.1.2","3.1.2",["1.2.2","2.2.2","3.2.2",["1.3.2","2.3.2","3.3.2",["1.4.2","2.4.2","3.4.2"]]], _
"1.1.3","2.1.3","3.1.3",["1.2.3","2.2.3","3.2.3",["1.3.3","2.3.3","3.3.3",["1.4.3","2.4.3","3.4.3"]]], _
"1.1.4","2.1.4","3.1.4",["1.2.4","2.2.4","3.2.4",["1.3.4","2.3.4","3.3.4",["1.4.4","2.4.4","3.4.4"]]], _
"1.1.5","2.1.5","3.1.5",["1.2.5","2.2.5","3.2.5",["1.3.5","2.3.5","3.3.5",["1.4.5","2.4.5","3.4.5"]]] ]

equals
Dim a(3,4,5) as String ' and fill it