Recent posts

#11
OxygenBasic Examples / Re: Parse example o2
Last post by Charles Pegge - June 18, 2024, 10:32:34 AM
Hi Frank,

O2 does not support empty parameters (,,) yet,  but I am working on it.

However, to take advantage of defaults in any order, you can do this:
s=myParse(expression="one,two,three", index=2)
#12
OxygenBasic / Re: ternary operator equivalen...
Last post by Frank Brübach - June 17, 2024, 12:13:03 PM
Hi Charles your Code is working Well..  Made an Update of oxygen Basic dont know what was the Problem Here
#13
OxygenBasic Examples / Parse example o2
Last post by Frank Brübach - June 17, 2024, 12:10:46 PM
Hello need a little Help,

Translated this Code of freebasic only one Line doesn't Work properly

' parse example test oxygen
' only one line doesn't work properly
' a=myparse(("one,two,three", ,2)
'
uses console

'indexbase 0

function myParse(string expression, delimiter = ",", int index ) as string
    int count = 1
    string temp = ""
    int d = 1
    int f = 0
    int i = 0
    int z = 1
    do
        i = instr(d, expression, delimiter)
        if i > 0 then
            f = i
            if count = index then
                exit do
            end if
            count += 1
            f += Len(delimiter)
            d = f
        elseif count = index then
            f = Len(expression) + 1
        end if
    loop until i = 0

    return MID(expression,d, f-d)
    'function = mid(expression,d, f-d)
         
end function

string a,b,c

a = myparse("one,two,three", ,2)   ' not ok -> should returns "two"
b = myparse("xyz", , 1)            ' ok -> returns "xyz"
c = myparse("xx1x","x", 3)         ' ok -> returns "1"

Printl "'" + a + "'"
Printl "'" + b + "'"
Printl "'" + c + "'"

wait
#14
OxygenBasic / Re: ternary operator equivalen...
Last post by Charles Pegge - June 17, 2024, 08:43:06 AM
It works here. I also tested it with a console:

uses console

macro iif int(r,e,a,b)
========================
if e
  r=a
else
  r=b
endif
end macro
'
int a = 10, b = 20

print iif(a < b , a , b) cr
wait
#15
OxygenBasic / Re: ternary operator equivalen...
Last post by Frank Brübach - June 17, 2024, 07:25:43 AM
Good morning,

Charles you example doesn't Work Here Got an Error message in
Print iif(a<b,a,b) ' Not found [a

Here's another example

' o2-tenary-frank c to oxygen
uses console

''
'' int a=10, b=20,c;
'' if(a<b){c=a}
'' else{c=b}
'' printf("%d",c)


Dim a As Integer = 10
Dim b As Integer = 20
Dim c As Integer

If a < b Then
    c = a
Else
    c = b
End If

Print c 'result 10

wait
#16
OxygenBasic / Re: ternary operator equivalen...
Last post by Charles Pegge - June 17, 2024, 02:59:20 AM
Hi James,

It can be done with macro functions (the C ternary syntax proved to be too messy)

macro iif int(r,  e,a,b)
========================
if e
  r=a
else
  r=b
endif
end macro
'
int a = 10, b = 20

print iif(a < b , a , b)
#17
OxygenBasic / ternary operator equivalent
Last post by James C. Fuller - June 16, 2024, 10:45:51 PM
Charles,
  Does O2 have a ternary operator equivalent?
int a = 10, b = 20, c;

c = (a < b) ? a : b;

printf("%d", c);

James


#18
OxygenBasic Examples / Re: Tally or Parse?
Last post by Frank Brübach - June 16, 2024, 08:56:42 PM
Thanks Theo for feedback Its more clear now to understand the difference between Tally ( more Things to Count) and Parse( more Things to SEARCH for) I wasnt Sure about this topic so I am asking ..

Powerbasic example both command in one example

#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN () AS LONG

DIM mainString AS STRING
DIM delimiter AS STRING
DIM tokenCount AS LONG

mainString = "PowerBasic,is,very,powerful"
delimiter = ","

tokenCount = PARSECOUNT(mainString, delimiter )
MSGBOX "Number of tokens: " + STR$(tokenCount) ' result 4 words

tokenCount = TALLY(mainString, delimiter)
MSGBOX "Number of tokens2: " + STR$(tokenCount) ' 3 counts to ',' comma here

END FUNCTION
#19
OxygenBasic Examples / Re: Tally or Parse?
Last post by Theo Gottwald - June 16, 2024, 11:45:10 AM
The main difference between Tally and ParseCount in PowerBasic is their purpose and how they handle strings.

Tally
The Tally function is used to count the occurrences of a specific substring within a larger string. It simply counts how many times the search substring appears in the main string.

Syntax:

result = TALLY(mainString, searchString)
mainString: The string in which you want to count occurrences.
searchString: The substring you are looking for.
Example:


DIM mainString AS STRING
DIM searchString AS STRING
DIM count AS LONG

mainString = "PowerBasic is powerful. Basic is easy to learn."
searchString = "is"

count = TALLY(mainString, searchString)
PRINT "Occurrences: "; count

ParseCount
The ParseCount function, on the other hand, is used to count the number of tokens (substrings) in a string that are separated by a specified delimiter. It doesn't count occurrences of a specific substring but instead counts how many pieces the string can be divided into based on the delimiter.

Syntax:

result = PARSECOUNT(mainString, delimiter)
mainString: The string you want to split into tokens.
delimiter: The character or string that separates the tokens.

Example:

DIM mainString AS STRING
DIM delimiter AS STRING
DIM tokenCount AS LONG

mainString = "PowerBasic,is,powerful"
delimiter = ","

tokenCount = PARSECOUNT(mainString, delimiter)
PRINT "Number of tokens: "; tokenCount

Summary of Differences
Purpose:

Tally: Counts the number of times a specific substring appears in a string.
ParseCount: Counts the number of tokens in a string based on a delimiter.
Usage:

Use Tally when you need to know how many times a particular substring appears in a string.
Use ParseCount when you need to know how many parts a string can be split into based on a delimiter.
Both functions are useful for string manipulation, but they are suited to different tasks.
#20
OxygenBasic Examples / Tally or Parse?
Last post by Frank Brübach - June 15, 2024, 11:06:54 PM
Hello..

I have translated an old Powerbasic example to oxygen.

But where is the Main difference between Tally and Parse?

'
' tally and parse function, oxygen
' hello charles, where is the main different between tally and parse?
'
' translation of a powerbasic example to oxygen
'
FUNCTION Tally(string search, datas ) AS INTEGER
    DIM count AS INTEGER = 0 ' Initialize the counter
    DIM i AS INTEGER ' Loop variable

    ' Loop through each occurrence of the delimiter in the data string
    FOR i = 1 TO LEN(datas) STEP LEN(search)
        ' If the substring at this position is equal to the search string, increment the counter
        IF MID(datas, i, LEN(search)) = search THEN
            count = count + 1
        END IF
    NEXT i

    ' Return the count
    RETURN count
END FUNCTION

' Example usage
DIM datas AS STRING = "BJF1, BJF2, BJF1, BJF3, BJF1, BJF4"
PRINT "The string 'BJF1' occurs " + STR(Tally("BJF1", datas)) + " times in the data." ' result 3 times


Powerbasic
'
''''''''''''''''''''' powerbasic ''''''''''''''''''''''''''''''''''''''''''
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN () AS LONG
DIM sData AS STRING
DIM lNumOccurences AS LONG
DIM lIndex AS LONG

sData = "BJF1, BJF2, BJF1, BJF3, BJF1, BJF4" ' This is your data string

lNumOccurences = 0 ' Initialize the counter

FOR lIndex = 1 TO PARSECOUNT(sData) ' Loop through each parsed element
    IF PARSE$(sData, lIndex) = "BJF1" THEN ' If the element is "BJF1"
        INCR lNumOccurences ' Increment the counter
    END IF
NEXT lIndex

MSGBOX "The string 'BJF1' occurs " + STR$(lNumOccurences) + " times in the data." ' result 3 times

END FUNCTION
''''''''''''''''''''' powerbasic ''''''''''''''''''''''''''''''''''''''''''