If you are using PBCC change both MSGBOX statements to PRINT and immediately following the PRINT statements enter the line
Code:
somestring$=WAITKEY$
somestring$=WAITKEY$
IF e = 1 THEN GOTO 3 END IF
Open "Test.txt" For Output As #1 Print #1, a() ' NB numeric values saved as ASCII text equivalent values Close #1 ' delimited w/ $CRLF (new line per data value)
#Debug Display On ' Useful when developing apps with arrays (traps out of bounds errr etc.) #Dim All #Register None #Include "WIN32API.INC" '------------------/ Function PBMain() Dim sTest$, h&, e&, d&, x&, Rec&, n& Dim a&() Dim FileData$() 1: x = 6 : d = 8 ReDim a(1 To 3, 1 To x) ' 3 'columns' by 6 'rows' 2: h = 1 Do While h <= x ' h = 'row' number e = e + d e = e - x a(1,h) = h ' assign a value to each column in this row a(2,h) = e ' a(3,h) = x ' sTest = sTest + Str$(a(1,h)) + $Tab + Str$(a(2,h)) + $Tab + Str$(a(3 ,h)) + $Cr h = h + 1 ' INCR h Loop 3: MsgBox " h" + $Tab + " e" + $Tab + " x" + $Cr + $Cr + _ sTest,,"Results" : sTest = "" Open "Test.txt" For Output As #1 Print #1, a() ' NB numeric values saved as ASCII text equivalent values Close #1 ' delimited w/ $CRLF (new line per data value) ' some time later :) ... Open "Test.txt" For Input As #1 FileScan #1, Records To Rec ReDim FileData(1 To 3, 1 To Rec/3) Line Input #1, FileData() Close #1 ReDim a(1 To 3, 1 To Rec/3) For n = 1 To Rec/3 ' Convert (ASCII) file data back to numeric array a(1,n) = Val(FileData(1,n)) : a(2,n) = Val(FileData(2,n)) : a(3,n) = Val(FileData(3,n)) sTest = sTest + Using$("###",a(1,n)) + $Tab + Using$("###",a(2,n)) + $Tab + Using$("###",a(3,n)) + $Cr Next MsgBox " h" + $Tab + " e" + $Tab + " x" + $Cr + $Cr + _ sTest,,"From file" End Function '------------------/PBMain ' File Test.txt data values stored thus: ' column 0, row 0 ' column 1, row 0 ' column 2, row 0 ' column 0, row 1 ' column 1, row 1 ' column 2, row 1 ' column 0, row 2 ' etc...__________________
#Debug Display On ' Useful when developing apps with arrays (traps out of bounds errr etc.) #Dim All #Register None #Include "WIN32API.INC" '------------------/ Function PBMain() Dim sTest$, h&, e&, d&, x&, Rec&, n& Dim a&() Dim FileData$() x = 6 : d = 8 Redim a(0 To 2, 0 To x-1) ' 3 'columns' by 6 'rows' Do While h < x ' h = 'row' number e = e + d e = e - x a(0,h) = h ' assign a value to each column in this row a(1,h) = e ' a(2,h) = x ' sTest = sTest + Str$(a(0,h)) + $Tab + Str$(a(1,h)) + $Tab + Str$(a(2 ,h)) + $Cr h = h + 1 ' INCR h Loop MsgBox " h" + $Tab + " e" + $Tab + " x" + $CR + $CR + _ sTest,,"Results" : sTest = "" Open "Test.txt" For Output As #1 Print #1, a() ' NB numeric values saved as ASCII text equivalent values Close #1 ' delimited w/ $CRLF (new line per data value) ' some time later :) ... Open "Test.txt" For Input As #1 FileScan #1, Records To Rec ReDim FileData(0 To 2, 1 To Rec/3) Line Input #1, FileData() Close #1 ReDim a(0 To 2, 1 To Rec/3) For n = 1 To Rec/3 ' Convert (ASCII) file data back to numeric array a(0,n) = Val(FileData(0,n)) : a(1,n) = Val(FileData(1,n)) : a(2,n) = Val(FileData(2,n)) sTest = sTest + Str$(a(0,n)) + $Tab + Str$(a(1,n)) + $Tab + Str$(a(2,n)) + $Cr Next MsgBox " h" + $Tab + " e" + $Tab + " x" + $CR + $CR + _ sTest,,"From file" End Function '------------------/PBMain ' File Test.txt data values stored thus: ' column 0, row 0 ' column 1, row 0 ' column 2, row 0 ' column 0, row 1 ' column 1, row 1 ' column 2, row 1 ' column 0, row 2 ' etc...
#COMPILE EXE #DIM ALL 'CONSTANTS %d=10 '%d and %x are now the "constants previously declared" and the values never change %x=6 FUNCTION PBMAIN () AS LONG LOCAL a() AS LONG LOCAL e,h,fnum AS LONG DIM a(0 TO %x-1) DO WHILE h < %x a(h)=e 'assign the value of e to the first element of the array a(0) because at this point h=0 'left out the PRINT # statement, will use it once the array is full 'left out the IF block because at this point e=0 and where line 3 is I have no idea 'if it was meant to go to the PRINT # statement and succeeded you'd have created an endless loop. e=e+%d e=e-%x INCR h 'the same as h=h+1 LOOP fnum=FREEFILE 'the use of FREEFILE means you don't need the first CLOSE statement in Dave's example 'OPEN statement ' 'PRINT # fnum, a() 'CLOSE # fnum MSGBOX "1st a(0)="+FORMAT$(a(0))+$CRLF+"2nd a(1)="+FORMAT$(a(1))+$CRLF+"3rd a(2)="+FORMAT$(a(2))+$CRLF+ _ "4th a(3)="+FORMAT$(a(3))+$CRLF+"5th a(4)="+FORMAT$(a(4))+$CRLF+"6th a(5)="+FORMAT$(a(5)) 'the array: this would be your 'h' column ' a(0)=0 ' a(1)=4 ' a(2)=8 ' a(3)=12 ' a(4)=16 ' a(5)=20 'to get the %x and %d columns you need an array with 2 dimensions and you would make the following 'changes to the above code. 'Change DIM a(0 TO %x-1) to DIM a(0 TO 2,0 TO %x-1) 'Change a(h)=e to a(0,h)=e 'Insert between e=e+%d and e=e-%x the following a(1,h)=e 'Insert after the line e=e-%x the following a(2,h)=e 'Comment out the two lines of the MSGBOX or change to reflect the 2 dimensions 'You can copy and paste them into position. 'just those changes should give you an array like: ' the 'h' column the %d column the %x column ' a(0,0)=0 a(1,0)=10 a(2,0)=4 ' a(0,1)=4 a(1,1)=14 a(2,1)=8 ' a(0,2)=8 a(1,2)=18 a(2,2)=12 ' a(0,3)=12 a(1,3)=22 a(2,3)=16 ' a(0,4)=16 a(1,4)=26 a(2,4)=20 ' a(0,5)=20 a(1,5)=30 a(2,5)=24 'This is the closest I can figure from your description 'You might try inserting your own values for %d and %x ' *********************************************COPIED so I didn't have to keep switching to and fro 'DO WHILE h < x 'ARRAY ASSIGN a()=e 'PRINT #1, A() ' 'IF e = 1 THEN 'GOTO 3 'END IF ' 'e = e + d 'e = e - x ' 'h = h + 1 ' 'LOOP 'x and d are constants previously declared. What I'd like to have is the array storing the values 'in three columns, x, h and e (ideally,the x values would only show up in the first row and then 'be blank thereafter; the h is the count). I can do this with the regular print function into a 'file but then I'd need an arary to get the values back to work. I'm not actually sure if what I'm 'asking makes sense or is doable, but I think it is. END FUNCTION
#Debug Display On ' Useful when developing apps with arrays (traps out of bounds errr etc.) #Dim All #Register None #Include "WIN32API.INC" '------------------/ Function PBMain() Dim sTest$, h&, e&, d&, x&, Rec&, n& Dim a&() Dim FileData$() x = 6 : d = 8 Redim a(0 To x-1) Do While h < x e = e + d e = e - x a(h) = e sTest = sTest + Str$(a(h)) + $Cr h = h + 1 Loop Close #1 MsgBox sTest,,"Results" : sTest = "" Open "Test.txt" For Output As #1 Print #1, a() ' NB numeric values saved as ASCII text equivalent values Close #1 ' delimited w/ $CRLF Open "Test.txt" For Input As #1 FileScan #1, Records To Rec ReDim FileData(1 To Rec) Line Input #1, FileData() Close #1 ReDim a(1 To Rec) For n = 1 To Rec ' Convert file data back to numeric array a(n) = Val(FileData(n)) sTest = sTest + Str$(a(n)) + $Cr Next MsgBox sTest,,"From file" End Function '------------------/PBMain
REDIM MyARray (2, nRow-1) 'columns 0, 1,2; as many rows as needed
TYPE MyRow Col1 AS whatever Col2 AS whatever Col3 AS whatever END TYPE .... REDIM Row(nRow-1) AS MyRow
REDIM COL1 (nRow-1) AS whatever REDIM COL2 (nRow-1) AS whatever REDIM COL3 (nRow-1) AS whatever
ARRAY ASSIGN a()=e
#COMPILE EXE #DIM ALL FUNCTION PBMAIN () AS LONG LOCAL myarray() AS LONG LOCAL x, y, counter,calculation AS LONG DIM myarray(0 TO 2, 0 TO 3) FOR x = 0 TO 2 FOR y = 0 TO 3 INCR counter calculation=2*counter myarray(x,y)=calculation MSGBOX "Element #"+FORMAT$(counter)+" myarray(x,y) where x="+FORMAT$(x) _ +" y="+FORMAT$(y)+" equals "+FORMAT$(calculation) NEXT y NEXT x MSGBOX "Element # 1 myarray(0,0)="+FORMAT$(myarray(0,0))+$CRLF+ _ "Element # 2 myarray(0,1)="+FORMAT$(myarray(0,1))+$CRLF+ _ "Element # 3 myarray(0,2)="+FORMAT$(myarray(0,2))+$CRLF+ _ "Element # 4 myarray(0,3)="+FORMAT$(myarray(0,3))+$CRLF+ _ "Element # 5 myarray(1,0)="+FORMAT$(myarray(1,0))+$CRLF+ _ "Element # 6 myarray(1,1)="+FORMAT$(myarray(1,1))+$CRLF+ _ "Element # 7 myarray(1,2)="+FORMAT$(myarray(1,2))+$CRLF+ _ "Element # 8 myarray(1,3)="+FORMAT$(myarray(1,3))+$CRLF+ _ "Element # 9 myarray(2,0)="+FORMAT$(myarray(2,0))+$CRLF+ _ "Element # 10 myarray(2,1)="+FORMAT$(myarray(2,1))+$CRLF+ _ "Element # 11 myarray(2,2)="+FORMAT$(myarray(2,2))+$CRLF+ _ "Element # 12 myarray(2,3)="+FORMAT$(myarray(2,3)) MSGBOX FORMAT$(myarray(0,0))+" "+FORMAT$(myarray(1,0))+" "+FORMAT$(myarray(2,0))+$CRLF+ _ FORMAT$(myarray(0,1))+" "+FORMAT$(myarray(1,1))+" "+FORMAT$(myarray(2,1))+$CRLF+ _ FORMAT$(myarray(0,2))+" "+FORMAT$(myarray(1,2))+" "+FORMAT$(myarray(2,2))+$CRLF+ _ FORMAT$(myarray(0,3))+" "+FORMAT$(myarray(1,3))+" "+FORMAT$(myarray(2,3)) END FUNCTION
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Leave a comment: