Date: 02-16-2022
Return to Index
created by gbSnippets
'The built-in PowerBASIC Array Insert is capable of inserting array elements
'Syntax: ARRAY INSERT array([index]) [FOR count] [, expression]
'if "FOR count" is left off, all values are shifted up
'default value of index is zero
'if no expression provided, insertion element is set to 0 or ""
'Array Insert DOES NOT redimension an array
'Primary Code (5 examples):
Array Insert MyArray() 'insert 0 at lowest bound, all others shifted up, last element lost
Array Insert MyArray(5) 'insert 0 at index 5, all others shifted up, last element lost
Array Insert MyArray(4) For 4, 'insert 0 at index 5, total of 4 shifted up, last element lost
Array Insert MyArray(5), 12 'insert 12 at index 5, all others shifted up, last element lost
Array Insert MyArray(2) For 3, 12 'insert 12 at index 2, total of 3 shifted up, element 11 lost
'Compilable Example: (Jose Includes)
'this example walks through an array, putting its values in the caption of the dialog
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As DWord, MyArray() as Long
Function PBMain() As Long
Dim MyArray(8), i as Long
Dialog New Pixels, 0, "Array Insert Element Test Code",300,300,240,200, %WS_OverlappedWindow To hDlg
For i = 0 to 8 : MyArray(i) = i : Next i
Control Add Label, hDlg, 100, "Array reset each time to: 0 1 2 3 4 5 6 7 8", 30,10,200,25
Control Add Button, hDlg, 200, "0 at index 0, shift all", 30,40,130,25
Control Add Button, hDlg, 300, "0 at index 5, shift all", 30,70,130,25
Control Add Button, hDlg, 400, "0 at index 4, shift 4", 30,100,130,25
Control Add Button, hDlg, 500, "12 at index 5, shift all", 30,130,130,25
Control Add Button, hDlg, 600, "12 at index 2, shift 3", 30,160,130,25
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local temp$, i As Long
If CB.Msg = %WM_Command AND CB.Ctlmsg = %BN_Clicked Then
For i = 0 to 8 : MyArray(i) = i : Next i
Select Case CB.Ctl
Case 200
Array Insert MyArray() 'insert 0 at lowest bound, all others shifted up, last element lost
Case 300
Array Insert MyArray(5) 'insert 0 at index 5, all others shifted up, last element lost
Case 400
Array Insert MyArray(4) For 4 'insert 0 at index 4, total of 4 shifted up, last element lost
Case 500
Array Insert MyArray(5), 12 'insert 12 at index 5, all others shifted up, last element lost
Case 600
Array Insert MyArray(2) For 3, 12 'insert 12 at index 2, total of 3 shifted up, element 11 lost
End Select
temp$ = "Array now contains: " + $crlf + $crlf + Str$(myarray(0))
For i = 1 To 8 : temp$ = temp$ + " - " + Str$(myarray(i)) : Next i
MsgBox temp$
End If
End Function
'gbs_00066
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm