|
Post by 2ndtrickop on Sept 18, 2018 17:17:34 GMT
To give my age away, I started learning to program on Penn State's mainframe (an IBM 360/67) waaaay back in the winter of 1967-68. I tinker around mostly with transportation-related simulations (subject of an honors course), but strictly as a hobby.
But to get to the issue at hand, I'm trying to rebuild a model I worked out involvng total mileages for a trucking line I once worked for; most of the trips were terminal-to-terminal with no stops or changes enroute, so a table similar to those found in a road atlas was usually referenced. Deviations enroute could be entered manually later.
I'm starting with six major points in a 6x6 grid, with more to be added later: New York(NY), Philadelphia(PH), Washington(DC), Chicago(CG), Dallas/Fort Worth(TX). and Los Angeles(LA). I'm attempting to enter each of the six as files in a comma-separated vales (csv) format, with the mileage for each of the five possible destinations immediately following the two-letter city code, as shown below:
open "NYOM$.txt (or NYOM$.csv?)" for output as #ROUTEM line input #in, NYOM$
PH,93,DC,229,CG.792,TX,1607,LA,2795
close "NYOM$.txt" for output as #ROUTEM
I'm recovering from a long hiatus in these matters and data in lines, rather than files, were common practice "back in the day", so I'm probably overlooking a minor change or two; any help is appreciated.
|
|
|
Post by B+ on Sept 18, 2018 20:13:06 GMT
There are so many ways to do this that I will likely waste my time reading your mind but here goes:
To save to file: PH,93,DC,229,CG.792,TX,1607,LA,2795
Open "fileName" for output as #1 print #1, "PH,93,DC,229,CG.792,TX,1607,LA,2795" close #1
To read from the file you just saved:
open "filename" for input as #1 line input #1, myFileLine$ close #1
then you can use
dat$ = word$(myFileLine$, dataNumber, ",") to read individual items.
This is a simple, simple straight forward answer that might get you started.
Read about File operations in JB v2.0 from Just Basic IDE.
Of course you can save the whole array to file as well, but that's more involved. You also could do the whole thing with Read/Data and save the data in your JB source file.
BTW Welcome to the forum, I remember using card punchers to write programs for mainframe at school.
PS works for me (with a . changed to a ,) :
Open "fileName.test" for output as #1 print #1, "PH,93,DC,229,CG,792,TX,1607,LA,2795" close #1
open "fileName.test" for input as #1 line input #1, myFileLine$ close #1
for dataNumber = 1 to 10 print word$(myFileLine$, dataNumber, ",") next
|
|
|
Post by 2ndtrickop on Sept 23, 2018 21:00:52 GMT
Thanks for the advice; I've tried a couple of approaches, but with no success as yet. The real problem, as I see it, is that I need a better, and wider exposure to accessing and processing character data within a file;; most of what I've seen in forums is so closely "geared" to individual issues that I can't find the basis -- a set of "ground rules", so to speak. I wonder if anyone can recommend a site better suited to a general interest, or more understandable in "layman's terms"? -- I'm in n hurry, and thanks in advance!
|
|
|
Post by tsh73 on Sept 24, 2018 7:03:49 GMT
I'm afraid there are no friendlier JB site that this one (are where any at all?) You can try asking at LB forum (http://libertybasiccom.proboards.com/) as well. Still, basics is pretty straightforward: (in pseudo code)
you OPEN a file while NOT(EOF(...)) 'until something left LINE INPUT ... 'read whole line ' and if you need split that line in different fields, ' you usually use WORD$ to split by delimiter ' (and VAL to get value from string) wend
As for you problem there is more then one way to skin a cat - and it really depend on what you want. I suggest making 2d array (6x6) making order of cities fixed, like NY-1, PH-2, DC-3, CG-4, TX-5, LA-6 so line PH,93,DC,229,CG.792,TX,1607,LA,2795 would be stored as 0,93,229,792,1607,2795 where 0 corresponds to NY-NY route and so other 5 lines.
|
|
|
Post by Rod on Sept 24, 2018 17:44:46 GMT
Perhaps you are overthinking the complexity. The previous posts have all shown how to handle the data you want to put in a file and how to extract it later.
Perhaps we could better understand your problem if you posted your own code that does not work. That way we can see how you are thinking.
I hate guessing.
|
|
|
Post by Rod on Sept 25, 2018 7:44:33 GMT
But perhaps I have to guess. Having gone back over the posts I am trying to properly understand what you want to do.
First of all its not the file handling that is the first problem it is the structure of the data. You speak of a matrix but give us an example of one line. On a map the table at the rear connects all major towns to all other major towns. It is a proper matrix and by finding the departure town and then looking up the destination town you can find the exact mileage between the two. There is usually a diagonal line down the middle of the matrix that is 0 because you are comparing each town to itself.
Also this matrix needs to be ordered, usually alphabetical order but it might be in distance order for some querys.
So it would look like this (hard to format here)
CG DC LA NY PH CG 0 99 109 99 85
DC 77 0 89 X X
LA X X 0 X X
NY X X X 0 X
PH X X X X 0
So knowing the departure point and destination we can look up the distance.
So in your file you would save the matrix a line at a time DC 77 66 89 X X
In your program you would retrieve it a line at a time and probably store it in a two dimensional array say distance (departure,destination)
Now before I go on are we getting any closer to the problem? Remember the problem is not file handling, firstly it is understanding your data structure and then the processes you wish to action on that data. Then we get to file handling.
PH
|
|
|
Post by 2ndtrickop on Sept 27, 2018 2:05:59 GMT
Well, again, my thanks for all the suggestions. I had tentatively suspended the approach in Post #1, which would seem to be "morphable" into the table/matrix depicted in Posts $5 and #6
I had also contemplated reading the data for each "location" (meaning a city -- suburbs and "satellites" could be served via a "secondary" subroutine when either origin or destination -- or both -- didn't fit the hub-to-hub matrix) as .csr character data, but I'm very rusty on this -- have some MS manuals I'm trying to brush up with.
An admittedly-primitive attempt is shown below:(and I'm aware of the lack of comments -- just didn't have time to separate them this evening)
Terms "data.csv"
Linecount = o
while linecount <> " " ExtractValues()
Linecount=Linecount+1 Line File.Readline(data)
EndWhile
ShowValues()
Sub ExtractValues
while Text.GetIndexOf(linecount, " ") > 0 comma_position = Text.GetIndexzOf(Linecount, " "} values[Array.GetItemCount(values)+1] = Text9.GetSubText{Linecount, 1, comma_position-1) Linecount = Text. GetSubTextToEnd(Linecount, comma_position+1)
EndWhile
values[Array.GetItemount(values)=1] = Linecount csv{Array.GetItemCount(values)+1 = values values = "]
EndSub
Sub ShowValues
For linecount = 1 to Array,GetItemCounrt(csv) For Locn = 1 to Array.getItemCount(csv,[Linecount]) TextWindow.Write(csv[linecount][Locn] + " "}
EndFor
TextWindow.WriteLine{" "}
EndFor
EndSub
(data - partial
6
data for point "CG" (Chicago)
CG NY,792,PH,781,DC,702,TX,988,LA,2018
etc
Thanks again for any advice -- I'm retired, healthy but semi-housebound, and have lots of time
|
|
|
Post by Rod on Sept 27, 2018 8:22:24 GMT
Nice to see descriptive variable names. However Just BASIC is quite a different dialect, simple good old fashioned basic. Here is how I would tackle the task of obtaining the distance between two locations.
'first off lets create our matrix 'by writing to a file called test.csv open "test.csv" for output as #1 #1 "CG,0,700,2014,789,758,1127" #1 "DC,700,0,2668,227,139,1545" #1 "LA,2014,2668,0,2789,2710,1246" #1 "NY,789,227,2789,0,97,1764" #1 "PH,758,139,2710,97,0,1684" #1 "TX,1127,1545,1246,1764,1684,0" close #1
'now set up to retrieve the file for 'use in the program. We will store it 'in a two dimensional array called 'distance(). We will store the alpha 'descriptions in location$() dim distance(6,6) dim location$(6) open "test.csv" for input as #1 for location=1 to 6 line input #1,l$ location$(location)=mid$(l$,1,2) for destination=1 to 6 'we need to skip the alpha and store six distances distance(location,destination)=val(word$(l$,destination+1,",")) next next
'now lets just check we got that right by 'printing out the matrix for location=1 to 6 print location$(location); for destination=1 to 6 print using("####",distance(location,destination));" "; next print next
'ok so far, now to access the array in our program 'can use the alpha to find the array index 'by using a simple function 'you might simplify by using numeric location ids
print print distance(lname("CG"),lname("TX"))
'we are done wait
function lname(n$) location=1 while location$(location)<>n$ location=location+1 wend lname=location end function
|
|
|
Post by B+ on Sept 28, 2018 3:28:11 GMT
There is a way to do this without filing and it might be easier to follow:
' city map milage, using Rod's figures for matrix
'here are the milages Rod setup, now in data statements = DATA SECTION [CG] ' CG DC LA NY PH TX data 0, 700,2014, 789, 758,1127 [DC] data 700, 0,2668, 227, 139,1545 [LA] data 2014,2668, 0,2789,2710,1246 [NY] data 789, 227,2789, 0, 97,1764 [PH] data 758, 139,2710, 97, 0,1684 [TX] data 1127,1545,1246,1764,1684, 0
'here is array setup and data read into it dim map(6, 6) for row = 1 to 6 for col = 1 to 6 read milage map(col, row) = milage next next
'here is a list of cities to lookup in same order as array row or col matrix city$ = "CG DC LA NY PH TX"
'here is code to get city code start and destination and lookup milage [GoAgain] nStart = 0 : nDestiny = 0 'reset back to 0 print print city$ 'show city codes input " Please enter city code start > "; start$ 'get start city input "Please enter city code destination > "; destiny$ 'get destination
for i = 1 to 6 'this starts loop to match city code to a number (i)
'find the number for the city code for array lookup if upper$(start$) = word$(city$, i) then 'word$ finds the i word in city$ nStart = i end if if upper$(destiny$) = word$(city$, i) then 'word$ finds the i word in city$ nDestiny = i end if
'when this condition is reached both start and destination numbers found if nStart > 0 and nDestiny > 0 then 'so lookup the milage in the map print "The milage is ";map(nStart, nDestiny) goto [GoAgain] ' keep going until user quits entering lookups end if
next
Note: in the beginning data section the lines with the [labels] aren't needed, just need the "data.... " lines. The labels were made to help document and read the data statements easier.
|
|
|
Post by B+ on Sept 28, 2018 4:02:09 GMT
Here is a simpler version, just enter city code number directly!
' city map milage, using Rod's figures for matrix
'here is the milages Rod setup, now in data statements = DATA SECTION [CG] ' CG DC LA NY PH TX data 0, 700,2014, 789, 758,1127 [DC] data 700, 0,2668, 227, 139,1545 [LA] data 2014,2668, 0,2789,2710,1246 [NY] data 789, 227,2789, 0, 97,1764 [PH] data 758, 139,2710, 97, 0,1684 [TX] data 1127,1545,1246,1764,1684, 0
'here is array setup and data read into it dim map(6, 6) for row = 1 to 6 for col = 1 to 6 read milage map(col, row) = milage next next
'here is a list of cities to lookup in same order as array row or col matrix city$ = "CG DC LA NY PH TX" code$ = " 1 2 3 4 5 6"
'here is code to get city code start and destination and lookup milage [GoAgain] nStart = 0 : nDestiny = 0 'reset back to 0 print print city$ 'show city codes print code$ 'show codes for city input " Please enter city code number start > "; nStart 'get start city if nStart < 1 or nStart > 6 then end input "Please enter city code number destination > "; nDestiny 'get destination if nDestiny < 1 or nDestiny > 6 then end print "The milage is ";map(nStart, nDestiny) goto [GoAgain] ' keep going until user quits entering lookups
|
|
|
Post by 2ndtrickop on Oct 3, 2018 15:30:57 GMT
Well, my thanks to all; I've got a starting point up and running, and will go on from there.
One more thing I would appreciate (and it shouldn't strain anyone) would be a good "general" set of ground rules on files vs. arrays; the problem is that the subject gets down to specific cases in forums like this one, without addressing the generalities.
To quote Rod:
"Now before I go on are we getting any closer to the problem? Remember the problem is not file handling, firstly it is understanding your data structure and then the processes you wish to action on that data. Then we get to file handling."
Could not agree more!
|
|
|
Post by Rod on Oct 3, 2018 17:25:00 GMT
That's very very easy. You only need a file if the data is to persist over time. If the data never changes then code it as data statements inside the program. If the data builds or changes over time and you need the program to remember it between sessions then you have to store it to a file. It is normal to load default starting positions from a .ini file and to save them at the end. So screen size, colors etc go in a .ini startup file. Mainstream data that evolves over time goes in a .csv or similar data file type.
In most cases you will want to load all the data to an array for processing and normal use. In extreme cases that involve huge data files you may page in and out or use a RAF access system. In the main, data files less than 1 MB should be loaded to arrays for speed and ease of access.
|
|
|
Post by 2ndtrickop on Nov 18, 2018 2:55:34 GMT
And thanks once more to all those who got me straightened out and oriented two months ago; I've made a lot of progress. The "arrays and CSVs" format works fine for the major locations, and I can add smaller "satellites" without expanding the main "XX by XX" grid, which would eat up a lot of space. What I could use now is a little more advice on the fine points of troubleshooting -- specifically, whether there's a parallel to the "step" function I used on the old MS QuickBasic is used years ago.
As it stands now, I can hang a "Stop" command at any point in the execution of a program and read the values. but can't determine the next step without moving "Stop" command and restating the program wondering if I'm overlooking something in the "tool box'?
Hopefully, it's out there, and I just haven't stumbled over it as yet.
|
|
|
Post by B+ on Nov 18, 2018 4:37:10 GMT
Yeah, I remember stepping through a program with QB, you want the ladybug right next to the Run triangle in the IDE.
|
|
|
Post by Rod on Nov 18, 2018 9:43:20 GMT
Yes the ladybug starts the debugger. As a beginner to Just BASIC you should spend a lot of time learning the debugger. You can step, step over and multistep through the program. A good tip is to double click on the left hand margin at the point you want to break the program. A blue dot will appear, now the program will run to that point, you can step on afterwards.
|
|