Liberty BASIC Community Forum
« Example programs - update »

Welcome Guest. Please Login or Register.
Sep 21st, 2017, 12:17am


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1 2  Notify Send Topic Print
 veryhotthread  Author  Topic: Example programs - update  (Read 679 times)
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5526
xx Example programs - update
« Thread started on: Aug 2nd, 2017, 10:56am »

What say you all to working through the example programs that currently ship with Liberty and updating them? There are a variety of styles like PRINT #Handle, "Command" that should now be #Handle "command". Wait instead of goto [Loop] and other little wrinkles.

Why not grab and example and update it, no embellishments, just stick to the original lesson/concept. Make sure it runs well and follows current convention, then post it here.

Carl can pick up the revised code if he likes the change.


User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5526
xx Re: Example programs - update
« Reply #1 on: Aug 2nd, 2017, 11:31am »

Now to show you what I mean and to spark no doubt endless discussion take the first example.

Code:
print amPmTime$(time$())

function amPmTime$(time$)

    colonIndex = instr(time$, ":")
    hours = val(left$(time$, colonIndex - 1))
    amOrPm$ = " AM"
    if hours > 12 then
            hours = hours - 12
            amOrPm$ = " PM"
        else
            if hours = 0 then hours = 12
    end if
    amPmTime$ = str$(hours) + mid$(time$, colonIndex) + amOrPm$

end function
 



Code:
'the function time$() returns "hh:mm:ss" in 24hr format
'pass this to a function to convert it to 12hr AM/PM format

print amPmTime$(time$())
wait

function amPmTime$(t$)

    'find the position of the first colon in the string
    colon=instr(t$,":")

    'extract the hour24 value from t$
    hour24 = val(left$(t$, colon - 1))

    'truncate t$ to the remaining ":mm:ss"
    t$=mid$(t$,colon)

    'are we AM or PM?
    if hour24 > 12 then

            'set to 12hr PM format
            hour = hour24 - 12
            amOrPm$ = " PM"
        else

            'handle midnight and set to 12hr AM format
            if hour24 = 0 then hour = 12
            amOrPm$ = " AM"
    end if

    'now pass the result back to the main program in the function name
    amPmTime$ = str$(hour) + t$ + amOrPm$
end function

 


You may have better ideas, you may think it does not need changed?
User IP Logged

BeeTrap
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 21
xx Re: Example programs - update
« Reply #2 on: Aug 3rd, 2017, 07:42am »

The "flow" of the code is better, but most importantly, the addition of the documentation helps newbies, LIKE ME, to understand what is going on! Any help is welcomed, and appreciated, by me. Thanks. If the "updates" aren't widely accepted, could the new versions be made available as "an option"?
« Last Edit: Aug 3rd, 2017, 07:45am by BeeTrap » User IP Logged

tsh73
Board Moderator

member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1681
xx Re: Example programs - update
« Reply #3 on: Aug 3rd, 2017, 08:26am »

I would say it asks for word$/after$
Code:
function amPmTime$(t$)

    'split the time string into hh and tail 
    hh$=word$(t$,1,":")
    tail$=after$(t$,":")

    'get hour24 value
    hour24 = val(hh$)

    'are we AM or PM?
    if hour24 > 12 then

            'set to 12hr PM format
            hour = hour24 - 12
            amOrPm$ = " PM"
        else

            'handle midnight and set to 12hr AM format
            if hour24 = 0 then hour = 12
            amOrPm$ = " AM"
    end if

    'now pass the result back to the main program in the function name
    amPmTime$ = str$(hour);":";tail$ ;amOrPm$
end function 
User IP Logged

damned Dog in the Manger
CarlGundel
Administrator
ImageImageImageImageImage


member is offline

Avatar

author of Liberty BASIC


Homepage PM


Posts: 2833
xx Re: Example programs - update
« Reply #4 on: Aug 3rd, 2017, 08:41am »

on Aug 2nd, 2017, 10:56am, Rod wrote:
What say you all to working through the example programs that currently ship with Liberty and updating them? There are a variety of styles like PRINT #Handle, "Command" that should now be #Handle "command". Wait instead of goto [Loop] and other little wrinkles.

Why not grab and example and update it, no embellishments, just stick to the original lesson/concept. Make sure it runs well and follows current convention, then post it here.

Carl can pick up the revised code if he likes the change.

Go for it. smiley

-Carl
User IP Logged

author of Liberty BASIC
http://www.libertybasic.com
CarlGundel
Administrator
ImageImageImageImageImage


member is offline

Avatar

author of Liberty BASIC


Homepage PM


Posts: 2833
xx Re: Example programs - update
« Reply #5 on: Aug 3rd, 2017, 08:47am »

Code:
amPmTime$ = str$(hour);":";tail$ ;amOrPm$ 


The use of semicolons is ok, but in the examples using + is preferred on the right side of = .

-Carl
on Aug 3rd, 2017, 08:26am, tsh73 wrote:
I would say it asks for word$/after$
Code:
function amPmTime$(t$)

    'split the time string into hh and tail 
    hh$=word$(t$,1,":")
    tail$=after$(t$,":")

    'get hour24 value
    hour24 = val(hh$)

    'are we AM or PM?
    if hour24 > 12 then

            'set to 12hr PM format
            hour = hour24 - 12
            amOrPm$ = " PM"
        else

            'handle midnight and set to 12hr AM format
            if hour24 = 0 then hour = 12
            amOrPm$ = " AM"
    end if

    'now pass the result back to the main program in the function name
    amPmTime$ = str$(hour);":";tail$ ;amOrPm$
end function 
User IP Logged

author of Liberty BASIC
http://www.libertybasic.com
mmiscool
Team Liberty BASIC


member is offline

Avatar



AIM
Homepage PM

Gender: Male
Posts: 226
xx Re: Example programs - update
« Reply #6 on: Aug 3rd, 2017, 10:14am »

I always use the ; symbol.

The reason for this is because it works with both numeric and string values. Using the plus sign with a numeric type and string type results in a failure. Using the semicolon works flawlessly in either case.

Code:
bla$ = bla$ ; "Some More Test"
x = 5
bla$ = bla$ ; x 



Results in failure
Code:
bla$ = bla$ + x 


Way to do it with plus signs. Seems less clean.
Code:
bla$ = bla$ + str$(x) 
User IP Logged

http://mmiscool.com
http://esp8266basic.com
http://smbisoft.com
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5526
xx Re: Example programs - update
« Reply #7 on: Aug 3rd, 2017, 11:46am »

Wow, now there you go, three opinions. Anatoly wants to introduce the word$() function. Fair enough but there are multiple ways to split the string, which is the simplest?

You might argue word$() is actually the Liberty BASIC best way to split a string$.

Then we have concatenation. Carl pushes for + after =, mmiscool prefers ; because it concatenates strings and numeric values without caring about type.

Word$() probably needs billboard space, ; is powerful if a little contra to the norm. Normally you would only be allowed to concatenate strings.

So I am favouring word$() and waiting to hear if we go with ; or +

Can't wait for the next installment of the example programs.
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5526
xx Re: Example programs - update
« Reply #8 on: Aug 3rd, 2017, 12:10pm »

Ok is this a consensus before we move on?

Code:
'the function time$() returns "hh:mm:ss" in 24hr format
'pass this to a function to convert it to 12hr AM/PM format

print amPmTime$(time$())
wait

function amPmTime$(t$)

    'extract the hour24 value from t$ using the word$() function
    hour24 = val(word$(t$,1,":"))


    'truncate t$ to the remaining "mm:ss" and
    'append to ":" using the after$() function
    t$=":";after$(t$,":")

    'are we AM or PM?
    if hour24 > 12 then
            'set to 12hr PM format
            hour = hour24 - 12
            amOrPm$ = " PM"
        else
            'handle midnight and set to 12hr AM format
            if hour24 = 0 then hour = 12
            amOrPm$ = " AM"
    end if
    'now pass the result back to the main program in the function name
    'note the ; concatenation will happily merge numeric and string types
    'you may also use + to concatenate but the types must all be strings,
    'so str$(hour)+t$+amOrPm$
    amPmTime$ = hour ; t$ ; amOrPm$
end function
 
User IP Logged

CarlGundel
Administrator
ImageImageImageImageImage


member is offline

Avatar

author of Liberty BASIC


Homepage PM


Posts: 2833
xx Re: Example programs - update
« Reply #9 on: Aug 3rd, 2017, 6:49pm »

I can't really concede the use of ; in place of + in example programs only because I'm pretty sure that users coming from other BASIC's will find it confusing.

But, an example called semicolonsNotPluses.bas that demonstrates the variation would be ok. smiley

-Carl
User IP Logged

author of Liberty BASIC
http://www.libertybasic.com
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5526
xx Re: Example programs - amPMTime.bas
« Reply #10 on: Aug 4th, 2017, 06:31am »

Ok, + it is, and after all that and fifteen years of use it was found to be bugged! try the midnight and noon strings on the old code:)

While the code might appear pedantic the objective is that a complete beginner understands what is going on.

Code:
'the function take "hh:mm:ss" in 24hr format
'and returns "hh:mm:ss" in 12hr format
print amPmTime$("00:30:30")
print amPmTime$("12:30:30")

'you can also send it the current time using the time$() function
print amPmTime$(time$())
wait

function amPmTime$(t$)

    'extract the hour24 value from t$ using the val() and word$() functions
    hour24 = val(word$(t$,1,":"))


    'truncate t$ to the remaining "mm:ss" and
    'append to ":" using the after$() function
    t$=":"+after$(t$,":")

    'are we AM or PM?
    select case
        case hour24 > 12
            'set to 12hr PM format deduct 12 hours
            hour12 = hour24 - 12
            amOrPm$ = " PM"
        case hour24 = 12
            'set to 12hr PM format
            hour12 = hour24
            amOrPm$ = " PM"
        case hour24 < 12
            'handle midnight and set to 12hr AM format
            hour12 = hour24
            if hour12 = 0 then hour12 = 12
            amOrPm$ = " AM"
    end select

    'set the hour value back to a leading zero string
    'so if the hour12 is 1 it becomes "01"
    h$=right$("00"+str$(hour12),2)

    'now pass the result back to the main program in the function name
    'build the result by adding the component strings together
    amPmTime$ = h$ + t$ + amOrPm$
end function

 
« Last Edit: Aug 4th, 2017, 3:34pm by Rod » User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5526
xx Re: Example programs - ASCII.bas
« Reply #11 on: Aug 4th, 2017, 06:37am »

Well does anyone else think the string building and printing is confusing, would a simple print in the loop suffice?

Code:
    ' This program ask for a string and then
    ' displays a list of ASCII codes for each
    ' character in the entered string

    input "Please enter a string >"; entry$

    for index = 1 to len(entry$)

        result$ = result$ + str$(asc(mid$(entry$,index,1))) + chr$(13)

    next index

    print result$
 
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5526
xx Re: Example programs - ascii.bas
« Reply #12 on: Aug 4th, 2017, 10:27am »

Straw man

Code:

    'This lists the ascii value of characters in a string


    'ask the user for a string to parse
    input "Please enter a string >"; entry$

    'get the length of the input string using len() function
    'loop from the first character to the last using a for next loop
    for index = 1 to len(entry$)
        'use mid$() function to slice up the string
        'then asc() to get the ascii value
        print asc(mid$(entry$,index,1))
    next index
 
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5526
xx Re: Example programs - binaryFileExample.bas
« Reply #13 on: Aug 4th, 2017, 10:46am »

Straw man


Code:
    'This program shows basic binary file operation
    'first create a file and put some text in it.
    open "myfile.dat" for output as #my
    #my "hey diddle diddle"
    close #my

    'Now open the file in binary mode, seek to
    'a specific place and read or write some text.
    open "myfile.dat" for binary as #my
    'move the record pointer to position 4 and see what is there now
    seek #my, 4
    print input$(#my, 6)
    'move the record pointer back and rewrite the value
    seek #my, 4
    #my "doddle"
    'move the record pointer back and check what is there now
    seek #my, 4
    print input$(#my, 6)

    close #my
 
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5526
xx Re: Example programs - update
« Reply #14 on: Aug 4th, 2017, 10:49am »

We probably need a simple sequential file example and a raf file example and for them all to be named so they appear in order.

fileOpsBinary.bas
fileOpsRAF.bas
fileOpsSequential.bas
User IP Logged

Pages: 1 2  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls