Insert element in string array

Started by jj2007, April 26, 2010, 07:24:22 PM

Previous topic - Next topic

jj2007

Here is a little proggie that creates and displays a matrix:
include \masm32\MasmBasic\MasmBasic.inc
Init
call Matrix
Inkey CrLf$, "ok, press a key"
Exit
Matrix proc
  Dim My$(9, 25)
  For_ Rows=0 To 25 ; A-Z
For_ cols=0 To 9 ; 0-9
mov ecx, Rows
Let My$(cols, Rows)=Str$(cols)+Chr$(ecx+65)
Next
  Next
  For_ n=0 To 9999 ; it stops inserting @ after a while...
Insert My$(3, 3)
Let My$(3, 3)="@@"
Delete My$(5, 5)
  Next
  For_ Rows=0 To 25
For_ cols=0 To 9
Print My$(cols, Rows), " "
Next
Print
  Next
  ret ; don't forget there are little differences to VB ;-)
Matrix endp
end start

Output:
0A 1A 2A 3A 4A 5A 6A 7A 8A 9A
0B 1B 2B 3B 4B 5B 6B 7B 8B 9B
0C 1C 2C 3C 4C 5C 6C 7C 8C 9C
0D 1D 2D @@ @@ @@ @@ @@ @@ @@
@@ @@ @@ @@ @@ @@ @@ @@ @@ @@
@@ @@ @@ @@ @@
5F 6F 7F 8F 9F
0G 1G 2G 3G 4G 5G 6G 7G 8G 9G
0H 1H 2H 3H 4H 5H 6H 7H 8H 9H
....
0Z 1Z 2Z 3Z 4Z 5Z 6Z 7Z 8Z 9Z

Since I was a bit puzzled by the fact that the middle loop produces only a limited amount of @@ inserts, I converted the proggie to VB to find out what happens there:
Sub Matrix()
Dim My$(9, 25)
  For Rows = 0 To 25
For cols = 0 To 9
Let My$(cols, Rows) = Str$(cols) + Chr$(Rows + 65)
Debug.Print '"clears" the immediate window; there is a hilarious workaround here: http://www.dailydoseofexcel.com/archives/2004/06/09/clear-the-immediate-window/
Next
  Next
  'Insert how ????
  For Rows = 0 To 25
For cols = 0 To 9
Debug.Print My$(cols, Rows);
Next
Debug.Print
  Next
End Sub


Same output except for the Insert. To my surprise, the help function did not produce anything useful under "Insert". A Google search reveals that the VB community works with hilarious workarounds involving RtlZeroMem etc - wow...

My old GfaBasic knows Insert and Delete but only for one-dimensional arrays.
I must have a legit copy of PB somewhere but can't find it - probably on an old Win98 puter. Can somebody be so kind to either test the proggie in PB or at least copy the documentation of Insert/Delete?

Thanks, Jochen

clive

The output makes sense. The @@ never go beyond element (5, 5) because you delete whatever gets there, and the array pulls back

After 1 iteration you'd get this, shifting every thing from (3, 3) 3D, and loosing 4F which had arrived at (5, 5)


0A 1A 2A 3A 4A 5A 6A 7A 8A 9A
0B 1B 2B 3B 4B 5B 6B 7B 8B 9B
0C 1C 2C 3C 4C 5C 6C 7C 8C 9C
0D 1D 2D @@ 3D 4D 5D 6D 7D 8D
9D 0E 1E 2E 3E 4E 5E 6E 7E 8E
9E 0F 1F 2F 3F 5F 6F 7F 8F 9F
0G 1G 2G 3G 4G 5G 6G 7G 8G 9G
0H 1H 2H 3H 4H 5H 6H 7H 8H 9H
....
0Z 1Z 2Z 3Z 4Z 5Z 6Z 7Z 8Z 9Z
It could be a random act of randomness. Those happen a lot as well.

jj2007

Thanks, Clive. It seems indeed plausible. In the meantime, I found the PB equivalent here:
ARRAY INSERT statement
Purpose: Insert a single item into a given array.
Syntax: ARRAY INSERT array([index]) [FOR count] [, expression]
ARRAY INSERT inserts a single data item into array, an n-dimensional array
...
ARRAY INSERT throws away the data in last element of array


I wonder why they discard the last element - it costs absolutely nothing, in terms of speed & size, to resize the pointer table...
But at least PB does have an Insert, in contrast to VB, and it even handles n-dimensional arrays.