Parse function

New to FreeBASIC? Post your questions here.
Post Reply
Löwenherz
Posts: 89
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Parse function

Post by Löwenherz »

Hello all.. I tried to build a Parse function for freebasic. Hope it doesn't exist yet .. one result IS OK two gives empty result. Any Help is Welcome. Thx

Code: Select all

' PARSE example try freebasic by frank bruebach, 15-06-2024
'
FUNCTION PARSE(expression AS STRING, delimiter AS STRING = ",", index AS INTEGER) AS STRING
    DIM count AS INTEGER = 0
    DIM temp AS STRING = ""

    FOR i AS INTEGER = 1 TO LEN(expression)
        IF MID$(expression, i, 1) <> delimiter THEN
            temp += MID$(expression, i, 1)
        ELSE
            count += 1
            IF count = index THEN
                EXIT FOR
            END IF
            temp = ""
        END IF
    NEXT

    IF count = index - 1 THEN
        PARSE = temp
    ELSEIF count >= index THEN
        PARSE = temp
    ELSE
        PARSE = ""
    END IF
END FUNCTION

Dim a As String
Dim b As String
Dim c As String

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

Print a
Print b

Sleep
fxm
Moderator
Posts: 12222
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Parse function

Post by fxm »

The procedure body can be simplified/improved but it works.
It is only its test that is not correct:

Code: Select all

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

Print a
Print b
Print c
Löwenherz
Posts: 89
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Parse function

Post by Löwenherz »

Thank you fxm all results are running Well.
Yes, you can improve my procedure Body

New edit have forgotten to print c all OK here
fxm
Moderator
Posts: 12222
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Parse function

Post by fxm »

First of all, by keeping your principle of a sequential pass character by character, one can improve your procedure body a little:

Code: Select all

FUNCTION PARSE(expression AS STRING, delimiter AS STRING = ",", index AS INTEGER) AS STRING
    DIM count AS INTEGER = 1
    DIM temp AS STRING = ""

    FOR i AS INTEGER = 1 TO LEN(expression) + 1
        DIM character AS STRING = MID(expression, i, 1)
        IF (character <> delimiter) AND (character <> "") THEN
            temp &= character
        ELSE
            IF count = index THEN
                EXIT FOR
            END IF
            count += 1
            temp = ""
        END IF
    NEXT
    PARSE = temp
END FUNCTION

Dim a As String
Dim b As String
Dim c As String

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

Print "'" & a & "'"
Print "'" & b & "'"
Print "'" & c & "'"

Sleep
fxm
Moderator
Posts: 12222
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Parse function

Post by fxm »

Finally, a sequential pass searching only for occurrences of the delimiter:
(this version supports delimiters of one character or more)

Code: Select all

FUNCTION PARSE(expression AS STRING, delimiter AS STRING = ",", index AS INTEGER) AS STRING
    DIM count AS INTEGER = 1
    DIM d As INTEGER = 1
    DIM f AS INTEGER = 0
    DIM i AS INTEGER = 0
    
    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
    PARSE = MID(expression, d, f - d)
END FUNCTION

Dim a As String
Dim b As String
Dim c As String

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

Print "'" & a & "'"
Print "'" & b & "'"
Print "'" & c & "'"

Sleep
Josep Roca
Posts: 575
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Parse function

Post by Josep Roca »

hi Löwenherz,

If you use my Afx framework, there are many string procedures implemented in it. They also work with Unicode.

https://github.com/JoseRoca/WinFBX/blob ... cedures.md
Löwenherz
Posts: 89
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Parse function

Post by Löwenherz »

@fxm many thanks for your both improved examples .. I appreciate that for fast Help

@jose yes thanks I will Check These String procedures examples in winfbx folder
Löwenherz
Posts: 89
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Parse function

Post by Löwenherz »

@fxm: hello fxm. I dont understand this Line in Second example how its possible to call Parse function with only one command 'parse' ? Its another way possible? Your example is Working fine but I cannot understand why?

Code: Select all

PARSE = MID(expression, d, f - d)
Found an alternative solution for it without Parse command

Code: Select all

Function=mid(Expression,d,f-d]
fxm
Moderator
Posts: 12222
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Parse function

Post by fxm »

|Programmer's Guide / Procedures / Returning Value| wrote: The value of a function can be returned in three ways:
- 'Return' keyword followed by a value exits the function immediately, and returns that value to the caller.
- Functions can also return values by assigning the 'Function' keyword or the 'function_identifier' to the desired return value (but 'Function' keyword or 'function_identifier' does not allow to evaluate the current assigned value). The latter two methods do not cause the function to exit, however.

Code: Select all

'' Using the name of the function to set the return value and continue executing the function:
Function myfunc1() As Integer
   myfunc1 = 1
End Function

'' Using the keyword 'Function' to set the return value and continue executing the function:
Function myfunc2() As Integer
   Function = 2
End Function

'' Using the keyword 'Return' to set the return value and immediately exit the function:
Function myfunc3() As Integer
   Return 3
End Function

In my above example, 'PARSE' is the function name, so there is equivalence between:
PARSE = MID(expression, d, f - d)
and:
FUNCTION = MID(expression, d, f - d)
and even:
RETURN MID(expression, d, f - d) '' if this is the last line of the function body
Löwenherz
Posts: 89
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Parse function

Post by Löwenherz »

Many thanks for explanations and examples fxm I have Got it :)

Code: Select all

'' thanks fxm :-)
'' I have got it
'' Using the name of the function to set the return value and continue executing the function:
Function myfunc1() As Integer
   myfunc1 = 1
End Function

Print myfunc1

'' Using the keyword 'Function' to set the return value and continue executing the function:
Function myfunc2() As Integer
   Function = 2
End Function

Print myfunc2

'' Using the keyword 'Return' to set the return value and immediately exit the function:
Function myfunc3() As Integer
   Return 3
End Function

Print myfunc3

Function myfunc4() As string
   Dim s As String = "Hello Earth "
   Print "42"
   Return s
End Function

Print myfunc4()

Function myfunc5() As Integer
   Return 5
   Print "5"
End Function

Print myfunc5()

Function myfunc6(abc As Integer) As Integer
   abc=123
   Dim d As Long
   d=42
   'Return abc*d '5166
   Function=abc*d '5166
End Function

Print myfunc6(1)

Sleep
Post Reply