Parse example o2

Started by Frank Brübach, June 17, 2024, 12:10:46 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frank Brübach

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

Charles Pegge

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)

Frank Brübach

Hey Charles sorry No Go with your Index=2 Idea but its Not so important for me at this Moment thx

Charles Pegge

Provide index with a default value and the syntax should work.

But I have now updated Oxygen, so empty params should work now, though I don't think it is a good idea for code readability.

Zlatko Vid

empty params is a bad idea for anything
and i don't know any PL which use empty args ?

Charles Pegge

It was a very small adjustment to the default-params system. I'm interested in anything that makes the language more flexible, but it puts more responsibility on the user to produce nice code, like playing a violin :)