Log in

View Full Version : How to conveniently fill a 2-dimensional array?



marcelma
12-14-2011, 10:22 AM
Hello,

How can I fill an array with entries like:

Line1 = (67, 104)
Line2 = (67, 72)
Line3 = (99, 104)
Line4 = (67, 818, 104, 818)

These numbers are character codes. Each line in the array is meant to contain codes for a character string.

I could put them into quotation marks and write:

A = Array("67, 104", "67, 72" ...) but then I would have a one-dimensional array containing strings, not a two-dimensional array containing character codes.

Thanks a lot in advance,
Marcel

gmaxey
12-14-2011, 11:41 AM
I don't know if it will be convienent or not but:

Sub ScratchMacro()
Dim i As Long
Dim j As Long
Dim pStr As String
Dim arrX(3, 3)
arrX(0, 0) = 67
arrX(0, 1) = 104
arrX(1, 0) = 67
arrX(1, 1) = 72
arrX(2, 0) = 99
arrX(2, 1) = 104
arrX(3, 0) = 67
arrX(3, 1) = 818
arrX(3, 2) = 104
arrX(3, 3) = 818
For i = 0 To UBound(arrX, 1)
pStr = ""
For j = 0 To UBound(arrX, 2)
If Not IsEmpty(arrX(i, j)) Then
pStr = pStr & ChrW(arrX(i, j))
End If
Next j
Debug.Print pStr
Next i
End Sub

Paul_Hossler
12-14-2011, 01:57 PM
Not great, but maybe something like this


Sub test()
Dim v As Variant
v = Array(Array(1, 2), Array(3, 4), Array(5, 6, 7, 8, 9))

MsgBox v(0)(0)
MsgBox v(0)(1)
MsgBox v(2)(4)

End Sub




But I'm curious about what are you trying to accomplish by building what seems to be a byte array like this

Paul

marcelma
12-14-2011, 05:19 PM
Dear Greg, dear Paul,

thank you very much for your response. I think Paul's answer is sufficiently convenient for my practical purposes. :-)

What I am trying to accomplish is a core-routine for a chararacter conversion macro. I am using one since many years, which checks the pair of characters preceeding the insertion point and changes (depending on the character combination) either both or only the rightmost one into characters with diacritical marks. If the preceeding character has already diacritical marks it is being changed back into its ASCII form. This way I need only one key-combination (or button) to put the proper diacritical marks on about 50 characters.

The basic concept is to keep the characters before and after conversion in two arrays of equal length, look for the character(string) in one array and replace it with the character(string) of the same position in the other array.

The reason why I am using character codes instead of characters is, that this way I can identify them easier.

Recently it has become necessary to re-write the routine in such a way, that it can exchange character combinations of variable length against character combinations of variable length.

Marcel

yolanda88
12-27-2011, 01:01 AM
Useful info. Hope to see more good posts in the future.