The Liberty Basic command INPUTTO is very a powerful but often under utilized command. I saw it was highlighted in a Tip Corner of an earlier newsletter issue and here it is exploited in a simple demo.
The Liberty Basic help file explains the INPUTTO command this way:
INPUTTO$(#handle, delim$))
This function reads from the file #handle up to the character specified in delim$ or to the end of the line, whichever comes first. This is handy for reading files which contain comma, tab, pipe, or other delimited items.
'display each comma delimited item in a file on its own line
open "inputto test.txt" for input as #test
while eof(#test) = 0
print inputto$(#test, ",")
wend
close #test
For the purpose of this demo, we will need to create a data file which we will be using INPUTTO to read. The following short Liberty Basic program will create the data file. Copy the code from the article and paste it into a new Liberty Basic document. Save the program and then run it.
'Create Ukraine Data file 'The following code creates a simple sequentual data file 'by Brad Moore - placed in public domain 2003 'written for Liberty Basic 3.x 'visit http://www.libertybasic.com 'Tell the user what we are going to do Print "Creating data file..." 'open a file to write to open "ukraine.dat" for output as #1 'write a heading out to the file and number of data elements print #1, "Agriculture, 26, "; 'read the data elements and write out to the file for x = 1 to 26 read a$ print #1, a$;", "; next x 'write a heading out to the file and number of data elements print #1, "" print #1, "Industries, 26, "; 'read the data elements and write out to the file for x = 1 to 26 read a$ print #1, a$;", "; next x 'write a heading out to the file and number of data elements print #1, "" print #1, "Natural Resources, 23, "; 'read the data elements and write out to the file for x = 1 to 23 read a$ print #1, a$;", "; next x 'close the file close #1 'tell the user we are done print "Data file complete." end 'Resource and products of the Ukraine 'Agriculture data wheat, rye, barley, oats, hops, corn, flax, vegetables, "sugar beets", potatoes, orchards data vineyards, sunflower, tobacco, hemp, cotton data Cattle, horses, swine, sheep, goats, poultry, rabbits, fishing, "bee keeping", silkworms 'Industries data Aircraft, "aerospace technology", shipping, "public transport vehicles", locomotives, turbines data tractors, "food-processing", excavators, mining, "heavy machinery", "machine tools", "electric power" data "electronic equipment", computers, appliances, "ferrous metals", "non ferrous metals", "cast iron", steel data "rolled metal", piping, chemicals, textiles, leather, "consumer goods" 'Natural Resources data coal, oil, "natural gas", "iron ore", manganese, titanium, uranium, bauxite, graphite, salt, potash data potassium, kaolin, sulphur, magnesium, mercury, nickel, gold, garnet, opal, diamonds data "Hydro-electric energy", forestry
Next is a very simple example file that demonstrates INPUTTO in its very simplest form. You can copy and paste this into Liberty Basic as well and run it.
open "ukraine.dat" for input as #1 while eof(#1) = 0 a$ = inputto$(#1,",") print a$ wend 'close the input file close #1
Finally, here is a GUI based demonstration that shows several neat tricks. Amoung them is the implementation of a Status Bar (courtesy of Alyce Watson) and also descrete timing to the millisecond courtesy of the following code:
t = time$("ms") while time$("ms") < t+100 'just wait here for a few milliseconds wend
This is very useful code to forcing a pause for a specific period of time, and not needing to depend on knowing just how fast the users PC is running.
Here is the code for the entire program. I recommend playing around a little with it. This is the best way to learn. Have fun:
'ExploreUkraine - a fun little program demonstrating INPUTTO$ command 'by Brad Moore - copyright 2003 ' 'You are free to use this anyway you would like. It is not good UI design, 'so I recommend using it as a learning tool only. Read the article that 'accompanies this program at http://www.libertybasicuniversity.com/lbnews/nl110.htm ' 'This program was written with LibertyWorkshop. Portions of this code 'are copyright 2002, 2003 by Alyce Watson. 'Find out more about LibertyWorkshop at http://alycesrestaurant.com ' 'written for Liberty Basic v.3.x 'For more about Liberty Basic at http://www.libertybasic.com Res$(1) = "Agriculture" Res$(2) = "Industries" Res$(3) = "Natural Resources" dim List$(100) List$(1) = "Click Reload to see data" [WindowSetup] NOMAINWIN WindowWidth = 437 : WindowHeight = 467 UpperLeftX = INT((DisplayWidth-WindowWidth)/2) UpperLeftY = INT((DisplayHeight-WindowHeight)/2) [ControlSetup] groupbox #main.gpbx, "Resources", 10, 0, 275, 410 button #main.reload, "Reload Data",[reload],UL, 300, 340, 115, 30 button #main.close, "Close",[quit],UL, 300, 380, 115, 30 listbox #main.lstbx, List$(,[lstbx], 25, 55, 245, 340 combobox #main.ResType,Res$(,[ResType], 25, 25, 245, 300 Open "Resources of the Ukraine" for Window_nf as #main #main "trapclose [quit]" #main "font ms_sans_serif 10" #main.ResType "select ";Res$(1) key$ = Res$(1) '** Change statusbar message at any time with sub ChangeStatusText hBar = MakeStatusBar(HWND(#main), "StatusBar") print #main, "resizehandler [ResizeStatus]" [loop] Wait [quit] close #main : END [reload] 'place code here open "ukraine.dat" for input as #1 Call ChangeStatusText hBar, 0, "Data Read Status: Reading" MakeStatusBar = hBar while eof(#1) = 0 gosub [getdatapoint] if a$ = key$ then 'get the next data element - total data points gosub [getdatapoint] a = val(a$) 'now read in the data points for x = 1 to a gosub [getdatapoint] List$(x) = a$ next x 'we are done - we can exit the while loop... exit while end if wend 'close the input file close #1 'now reset the status bar text Call ChangeStatusText hBar, 0, "Data Read Status: Idle" Call ChangeStatusText hBar, 1, "Data: None" MakeStatusBar = hBar 'now reload the listbox #main.lstbx "reload" goto [loop] [getdatapoint] t = time$("ms") a$ = inputto$(#1,",") statusText$ = "Data: "+a$ Call ChangeStatusText hBar, 1, statusText$ while time$("ms") < t+100 'just wait here for a few milliseconds wend return [lstbx] print #main.lstbx, "selection? selected$" notice "You clicked " + selected$ goto [loop] [ResType] print #main.ResType, "selection? selected$" 'make sure the selection is not user entered... for x = 1 to 3 if selected$ = Res$(x) then exit for next x 'x will be 4 if we fall through the loop without exiting... if x = 4 then notice "Choose only items in the list, do not add items. Thanks..." #main.ResType "reload" #main.ResType "select ";Res$(1) goto [loop] end if key$ = selected$ 'change the list displayed redim List$(100) List$(1) = "Click Reload to see data" #main.lstbx "reload" goto [loop] [ResizeStatus] 'resize status bar when window is resized SB.SETPARTS = 1028 struct prt,edge1 as long,edge2 as long 'change measurements below as needed: sizePanel = Int(WindowWidth/2) 'divide WindowWidth by number of panels prt.edge1.struct = 1 * sizePanel prt.edge2.struct = -1 numParts = 2 calldll #user32, "SendMessageA", hStatus as long, SB.SETPARTS as long, numParts As Long, prt As struct, r As Long calldll #user32, "SendMessageA", hStatus as long, _WM_SIZE as long, 0 as long, WindowWidth as long, re as long goto [loop] FUNCTION MakeStatusBar(hWin, caption$) style = _WS_VISIBLE or _WS_CHILD calldll #comctl32,"InitCommonControls", re as void calldll #comctl32,"CreateStatusWindow", style as long, caption$ as ptr, hWin as long, 22 as long, hBar as long SB.SETPARTS = 1028 struct prt,edge1 as long,edge2 as long 'change measurements below as needed: prt.edge1.struct = 150 prt.edge2.struct = -1 numParts = 2 calldll #user32, "SendMessageA", hBar as long, SB.SETPARTS as long, numParts As Long, prt As struct, r As Long '** Replace text in following lines with desired captions for panels: Call ChangeStatusText hBar, 0, "Data Read Status: Idle" Call ChangeStatusText hBar, 1, "Data: None" MakeStatusBar = hBar END FUNCTION SUB ChangeStatusText hBar, panelID, newText$ 'use this sub to update captions on statusbar SB.SETTEXT = 1025 calldll #user32, "SendMessageA", hBar as long, SB.SETTEXT as long, panelID as long, newText$ as ptr, re as long END SUB