Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 17:26 10 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Two syntax questions.

Author Message
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 07:55pm 24 Nov 2017
Copy link to clipboard 
Print this post

I have integer arrays wherein I store event times obtained from TIMER calls. I need to calculate and print a duty cycle, which means I have to divide one integer by another integer to produce a float then convert that float to a string and print it, maybe to a text file. Will this work, can I divide an integer by an integer to produce a float like this?

OPTION EXPLICIT
OPTION BASE 1
DIM t%(3) = (97080, 200000, 0) 'start, end, elapsed
DIM dc ' duty cycle float
t(3)=t(2)-t(1) ' 102920
dc = t(3) / t(2) ' 0.5146
print str$(dc,1,4) ' 0.5146


#2. Granpa's basic included a FIELD statement which identified sections of a string buffer filled from a random access file buffer as named variables like this:

FIELD [#l<file number>,<field width> AS <string variable>
FIELD 1,20 AS N$,lO AS ID$,40 AS ADD$

The FIELD statement is missing from MMbasic.

Is it practical to use a string array to substitute for FIELD like this?

OPTION EXPLICIT
OPTION BASE 1
DIM f$(70) length 1 '1|20 N$, 21|10 ID$, 31|40 ADD$
N$="01234567890123456789" ' length 20
ID$="ABCDEFGHIJ" ' length 10
ADD$="Any old address with 40 characters is OK" ' length 40
PACK(N$,1,20)
PACK(ID$,21,10)
PACK(ADD$,31,40)
DISPLAY
END

SUB PACK(S$,start%,length%)
local i%
for i%=0 to length%-1
f$(start%+i%)=mid$(S$,(i%+1),1)
next i%
END SUB 'PACK

SUB DISPLAY
local i%
for i%=1 to 70
print f%(i%);
next i%
END SUB 'DISPLAY

Has someone had a better idea than this? I don't feel like re-inventing the wheel.

Paul in NY
 
erbp
Senior Member

Joined: 03/05/2016
Location: Australia
Posts: 195
Posted: 12:31am 25 Nov 2017
Copy link to clipboard 
Print this post

Re Question #1:
  Paul_L said   Will this work, can I divide an integer by an integer to produce a float like this?

Yes you can. Your example code will work exactly as your comments show the results you expect. Just make sure you use the forward slash (/) as the divide operator - this specifies floating point division. If you use the back slash (\) it will do integer division and the result in your example will be 0.

I will leave someone more familiar with string manipulation to comment on question #2.

Cheers,
Phil.
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1671
Posted: 12:45am 25 Nov 2017
Copy link to clipboard 
Print this post

  Paul_L said  The FIELD statement is missing from MMbasic.

Hi Paul,
maybe this is helpful. (Works for MM2)
Regards
Michael (from Germany )

PS:
I didn't try your code example.

To complete this I copy Geoffs code example to this place:
  Geoffg said   As an experiment I wrote the FIELD$() function as an MMBasic subroutine. It is so simple that I am not sure that it is worth putting it into the language.

Function Field$(s As String, n As Integer)
Const delim = "," ' set this to the field delimiter
Local Integer i, StartIdx = 1, EndIdx

' get the start of the field in StartIdx
For i = 2 To n
StartIdx = Instr(StartIdx, s, delim) + 1
If StartIdx = 1 Or StartIdx > Len(s) Then Exit Function
Next i

' get the end of the field in EndIdx and extract the field
EndIdx = Instr(StartIdx, s, delim)
If EndIdx = 0 Then EndIdx = 255
Field$ = Mid$(s, StartIdx, EndIdx - StartIdx)

' trim leading and trailing spaces
Do While Mid$(Field$, 1, 1) = " " : Field$ = Mid$(Field$, 2) : Loop
Do While Right$(Field$, 1) = " " : Field$ = Mid$(Field$, 1, Len(Field$) - 1) : Loop
End Function
Edited by twofingers 2017-11-26
causality ≠ correlation ≠ coincidence
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4126
Posted: 06:57am 25 Nov 2017
Copy link to clipboard 
Print this post

In the first example in post #1 there is inconsistency with t and t% which I suspect would be a bug.

But the general idea looks fine.

The PACK thing looks cumbersome but may well work.

My understanding of FIELD is that the Field$ function is vaguely similar but not the same. However, I'm unconvinced that FIELD is really a good thing. (When forced to use it, you do. That's not to say you want to be forced.)

JohnEdited by JohnS 2017-11-26
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 12:36am 26 Nov 2017
Copy link to clipboard 
Print this post

Database files are usually composed of fixed length records divided into fixed length sub-fields but there are no delimiters. The FIELD statement provides a way to identify individual sub-fields using variable names.

A Database Management System additionally differentiates between persistent field variables which exist in the data file outside of the program, and transient field variables which exist in core (RAM) memory only while the program is running.

A DBMS typically points to a specific record number, the location of which is calculated by multiplying the record length by the record number. It then reads the entire record into a RAM buffer and makes the sub-fields available by their defined names.

A special command, usually REPLACE, is needed in order to actually write a changed sub-field from the RAM buffer into the disk file.

I've been trying to figure out how much of this functionality can be written conveniently in MMbasic. I'm wondering if it would be possible to re-create interpreted dBase III in MMbasic. Between SEEK #fnbr, pos; INPUT$(nbr, #fnbr); EOF(#fnbr); LOC(#fnbr); and LOF(#fnbr) it seems that Geoff has provided all the needed functionality.

Paul in NY
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 01:09am 26 Nov 2017
Copy link to clipboard 
Print this post

  JohnS said   In the first example in post #1 there is inconsistency with t and t% which I suspect would be a bug.


The code in question is as follows:
OPTION EXPLICIT
OPTION BASE 1
DIM t%(3) = (97080, 200000, 0) 'start, end, elapsed
DIM dc ' duty cycle float
t(3)=t(2)-t(1) ' 102920
dc = t(3) / t(2) ' 0.5146
print str$(dc,1,4) ' 0.5146

I thought that once a variable is defined by say DIM a$, b% then subsequent references to it without the type flag like a="abc" : b=2 would point to the correct variable.

In other words, you only HAVE to specify the type in the DIM statement, LOCAL statement, or, in the case of a called function in the function definition like FUNCTION C$(a$, b%) where a$ and b% would effectively become local variables within the C$() function but really point back to the variables passed in the call to the function in the parent procedure. (What did that Polack from NY just say?)

This also means that you can't have the simultaneous existence of an A$, A%, A! and A# the way you could in MSBasic.

All of which leads to an additional question. If you define a function as a given type like C$(a$,b%) will you be able to call it without using the type character like x$=C("a",1).

Comments anyone.

Paul in NY
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1671
Posted: 11:06am 26 Nov 2017
Copy link to clipboard 
Print this post

  Paul_L said   Database files are usually composed of fixed length records divided into fixed length sub-fields but there are no delimiters. The FIELD statement provides a way to identify individual sub-fields using variable names.

hmm, something like Peters "JSON$()" function for the Pi (s. MMBasic Ver 5.4.14)?
causality ≠ correlation ≠ coincidence
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 03:25pm 26 Nov 2017
Copy link to clipboard 
Print this post

@twofingers -- OK, I give up, what's Peter's "JSON$()" function and where can I find it?

Paul in NYEdited by Paul_L 2017-11-28
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1671
Posted: 07:25pm 26 Nov 2017
Copy link to clipboard 
Print this post

@Paul
  Quote  OK, I give up, what's Peter's "JSON$()" function and where can I find it?
MMbasic Ver 5.4.14 for Pi (aka Picromite) manual p. 16.
Michael

http://www.thebackshed.com/forum/forum_posts.asp?TID=9973&KW=Pi-cromite_Manual
Edited by twofingers 2017-11-28
causality ≠ correlation ≠ coincidence
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 06:27am 27 Nov 2017
Copy link to clipboard 
Print this post

@twofingers -- ah ha, yes FIELD is something like JSON$() except that it works on the non-volatile file contents. The database file is sort of like a long string because it doesn't have any delimiters. I haven't been paying much attention to the work being done on the Pi.

The absence of delimiters means that the definition of the repetitive database record has to be encoded into the program that uses it, so that each database file is only accessible by the program that wrote it.

This deficiency was solved by Fred Thompson and Jack Hatfield at JPL in the 1960s. They incorporated a definition of the individual database record into a header in the database file, then they wrote an interpreter called JPLDIS which could read the header of any database file and establish the individual variable names representing the fields of the record upon reading the header of any database file. This was released to public domain by JPL in 1973 written in Fortran running on a Univac mainframe. Wayne Ratliff, also at JPL, ported JPLDIS to PTDOS running on an IMSAI 8080 in order to keep track of the office football pool. Wayne Ratliff called this Vulcan, ported it to CP/M and formed Ashton Tate with George Tate and Hal Lashlee. The name was changed to dBase II in short order. In 1984 they released dBase III which was a re-write of dBase II in C. FoxBase and Clipper later evolved from the public domain JPLDIS. Microsoft bought FoxBase and killed it. Computer Associates bought Clipper and killed it. I still can't figure out why!

So much for history from the resident geezer. This is way beyond the complexity of what I am trying to do. I will be satisfied if I can make Geoffs excellent random access functions work on a file with the record definition known to the program.

Paul in NY
Edited by Paul_L 2017-11-28
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1671
Posted: 12:30pm 27 Nov 2017
Copy link to clipboard 
Print this post

Hi Paul,

now I remember ... the old DBase III & Clipper-days!
I should have read your text more carefully (I didn't see your 2. post).
Thanks a lot for the explanation.

MichaelEdited by twofingers 2017-11-28
causality ≠ correlation ≠ coincidence
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 09:38pm 27 Nov 2017
Copy link to clipboard 
Print this post

Hi Michael,

I'm glad to know there's somebody else around here old enough to remember dBase III.

Actually I kind of liked the original IBM ISAM routines that I first saw on a 360 about 1961. That was a revelation!

The Indexed Sequential Access Method required the database record definition to be in the program. This provided additional security since you could not easily figure out the structure of a database file or the indexes to it by looking at the data files so if you didn't actually have the program running you couldn't read the data.

Paul in NY
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1671
Posted: 09:54pm 27 Nov 2017
Copy link to clipboard 
Print this post

  Paul_L said   Hi Michael,

I'm glad to know there's somebody else around here old enough to remember dBase III.

Yes, but I guess here at the TBS are many many more of them. It's now 30 years since I programmed in Clipper. Often I thought we should have a database system for MMs ... just to play with.

Michael
causality ≠ correlation ≠ coincidence
 
BrianP
Senior Member

Joined: 30/03/2017
Location: Australia
Posts: 292
Posted: 07:57am 28 Nov 2017
Copy link to clipboard 
Print this post

I'm one!

B
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 01:05pm 28 Nov 2017
Copy link to clipboard 
Print this post

Good grief!! More geezers!!

Should we start a contest to see who can honestly remember the Pearl Harbor radio news broadcasts and Roosevelt's "Day of Infamy" speech?

Paul in NY
Older than dirt.
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025