|
Post by Binary1248 on May 29, 2020 17:38:10 GMT
How do I fill an array from a line of data.
Here is what I tried.
'Name Future program ';this program is to convert using array 'First created May 29 2020
Dim table1$(5) 'Declare table size/type alpha table table1$(3)="f" 'preset a value in the table to see the change later
table1$="5";"B";"J";"C";"K" ' This is NOT WORKING ? How to fill this table with line of data
' Now Test, should print J, not an f
print table1$(3) 'should be J, not f ; EDIT: This was fixed by help in later postings.
|
|
|
Post by cundo on May 29, 2020 18:49:17 GMT
How do I fill an array from a line of data. Here is what I tried. 'Name Future program ';this program is to convert using array 'First created May 29 2020 Dim table1$(5) 'Declare table size/type alpha table table1$(3)="f" 'preset a value in the table to see the change later table1$="5";"B";"J";"C";"K" ' This is NOT WORKING ? How to fill this table with line of data ' Now Test, should print J, not an f print table1$(3) 'should be J, not f Hi welcome to the forums! I think you should go with double arrays. example: dim arr$(10,10) arr$(1,1)= "5" arr$(1,2)= "B" arr$(1,3)= "J" and so on.
|
|
|
Post by B+ on May 29, 2020 20:00:31 GMT
This is like one of the first lessons in Basic reading DATA items into an array:
' BASIC 101 load array from DATA with READ ' 5 data items to load data "5","B","J","C","K" dim table$(5) 'set up array to hold data items for i = 1 to 5 'loop for reading 5 items 'read table$(i) 'dang cant read directly into array? read item$ table$(i) = item$ next
'check the load for i = 1 to 5 print table$(i) next
|
|
|
Post by Binary1248 on May 29, 2020 22:03:04 GMT
Thanks all, that 2nd suggestion will do the trick. While I showed only 5 items, the actual prgm will have much longer data to deal with. It's been many years since I used basic, so I am re-learning most of it. Paul
|
|
|
Post by tsh73 on May 29, 2020 22:16:29 GMT
Also JB has very handy WORD$ function. (reads from strings, delimited with spaces (by default - see Help for details). Strings could be read from files)
'JB (LB) specific WORD$ function 'gets VERY handy sometimes ' 5 data items to load dataStr$= "5 B J C K" dim table$(5) 'set up array to hold data items for i = 1 to 5 'loop for reading 5 items table$(i) = word$(dataStr$, i) next
'check the load for i = 1 to 5 print table$(i) next
|
|
|
Post by Binary1248 on May 30, 2020 0:58:11 GMT
Thanks tsh73, I need to refresh my mind on handling/reading string data. I have been thru many types of Basic in my lifetime but every one has some variations in the code structure, but I will get there with help.
|
|
|
Post by honkytonk on May 30, 2020 9:39:58 GMT
a$="5 B J C K " for x=1 to len(a$) ' to count items if word$(a$,x)="" then xx=x-1:exit for next x for x=1 to xx array$(x)=word$(a$,x) next x
|
|
|
Post by Rod on May 30, 2020 14:02:24 GMT
'lets say you just have a list of variable length data a$="A BB J CC K "
'first off dim the table array, nice to know how many items 'but if you dont just make it big enough dim table$(100)
'set up to check by getting first item in the list pos=1 w$=word$(a$,pos)
'now run through the list till you are done 'we will be done when word$() returns an "" while w$<>"" table$(pos)=w$ pos=pos+1 w$=word$(a$,pos) wend numitems=pos-1
|
|
|
Post by Binary1248 on May 30, 2020 17:52:03 GMT
Thanks all, these samples are great and I am running each and saving for future reference. I have my simple table code working but I find myself spending more time playing around with things like building an EXE file of my code, trying different string manipulators and just having fun.
So my original idea for the end use of my code is now way behind. Of course I have no schedule (I am retired) but being a hardware and software engineer I just can't get away from trying different code snippets.
|
|
|
Post by B+ on May 30, 2020 19:32:58 GMT
Yes, great hobby for retirement, keeps mind active and creative. WORD$() is one of at least 3 of JB's greatest assets.
|
|
|
Post by Binary1248 on May 31, 2020 3:31:23 GMT
OK, I have searched for a description of this WORD$() cmnd, or is it ? Can't seem to find any reference to it in the Liberty Basic listing of codes and help files..
I have a good understanding of cutting up strings with left/right/and other things that disassemble strings, but I can't find that command.
|
|
|
Post by tenochtitlanuk on May 31, 2020 8:12:10 GMT
Below is an exerpt from the JB help files.. run jb20help.html in the installation directory jb2help sub directory. Word$ is the last entry in the list of keywords and I use it a LOT!!
WORD$( s$, n )
WORD$( stringExpression, n [,string delimiter] ) Description:
This function returns the nth word in the string, string variable or string expression, stringExpression. The leading and trailing spaces are stripped from stringExpression and then by default it is broken down into 'words' at the remaining spaces inside. If n is less than 1 or greater than the number of words in stringExpression, then "" is returned. The string delimiter is optional. When it is not used, the space character is the delimiter.
Usage:
print word$("The quick brown fox jumped over the lazy dog", 5)
Produces:
jumped
And:
'display each word of sentence$ on its own line sentence$ = "and many miles to go before I sleep." token$ = "?" while token$ <> "" index = index + 1 token$ = word$(sentence$, index) print token$ wend
Produces:
and many miles to go before I sleep.
|
|
|
Post by tsh73 on May 31, 2020 10:17:16 GMT
|
|
|
Post by Rod on May 31, 2020 18:14:29 GMT
If you click on the ? icon the help file should open in your default browser and there is a list of commands and functions in alphabetical order at the foot of the first page. Word$() is the last listed function.
Is the help file displaying properly for you? or do we need to fix something?
|
|
|
Post by B+ on May 31, 2020 18:43:52 GMT
If you click on the ? icon the help file should open in your default browser and there is a list of commands and functions in alphabetical order at the foot of the first page. Word$() is the last listed function. Is the help file displaying properly for you? or do we need to fix something? This is assuming you have JB v 2.0.
|
|