Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 02:57 18 Sep 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 : Unexpected text error

Author Message
Bizzie
Senior Member

Joined: 06/07/2014
Location: Australia
Posts: 192
Posted: 06:15pm 05 Nov 2015
Copy link to clipboard 
Print this post

I am trying to write a small function which will tell me if a particular character is present in a string. The function is to return the position of the first occurrence or 0 if NOT present.

I am getting the following error "Error: Unexpected text"!

Can someone shed some light on what I am doing wrong please.


list


Test$ = ">06-11-2015~15:09:21,367,396,366,346,0,321,<"

Print contains(Test$,">")
Print contains(Test$,">")


Function Contains(ThisStr$,Char$)
Local i
For i = 1 To Len(ThisStr$)
If Mid$(ThisStr$,i,1) = Char$ Then Return i
Next
Return 0
End Function
> ? mm.ver
4.07
> run
[12] If Mid$(ThisStr$,i,1) = Char$ Then Return i
Error: Unexpected text
>

Rob White
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 06:28pm 05 Nov 2015
Copy link to clipboard 
Print this post

from the manual:
[code]
INSTR( [start-position,] stringsearched$, string-pattern$ )
[/code]


Returns the position at which 'string-pattern$' occurs in 'string-searched$', beginning at 'start-position'.


A function needs its return value set before exiting it.
[code]
Function Contains(ThisStr$,Char$)
Local i
Contains = 0
For i = 1 To Len(ThisStr$)
If Mid$(ThisStr$,i,1) = Char$ Then
Contains = i
EXIT FOR
Endif
Next
End Function
[/code]

Edited by MicroBlocks 2015-11-07
Microblocks. Build with logic.
 
Justplayin

Guru

Joined: 31/01/2014
Location: United States
Posts: 330
Posted: 06:58pm 05 Nov 2015
Copy link to clipboard 
Print this post

  Bizzie said   I am trying to write a small function which will tell me if a particular character is present in a string. The function is to return the position of the first occurrence or 0 if NOT present.


Why not just use INSTR function? It does exactly what you want and it's already part of MMBASIC.

Test$ = ">06-11-2015~15:09:21,367,396,366,346,0,321,<"

Print INSTR(Test$, ">")
Print INSTR(Test$, "<")
Print INSTR(Test$, "X")


This will output:
1
44
0


--Curtis
I am not a Mad Scientist...  It makes me happy inventing new ways to take over the world!!
 
Bizzie
Senior Member

Joined: 06/07/2014
Location: Australia
Posts: 192
Posted: 07:35pm 05 Nov 2015
Copy link to clipboard 
Print this post

Thanks Microblocks and Justplayin, another case of "not seeing the wood for the trees" or something.

I would still like to know what is causing the error (bug?)


Rob White
 
Justplayin

Guru

Joined: 31/01/2014
Location: United States
Posts: 330
Posted: 07:47pm 05 Nov 2015
Copy link to clipboard 
Print this post

The problem with your original code is the incorrect usage of the RETURN command. RETURN is used with GOSUB and does not "return" any value. For a function to return a value you must assign your return value to the function name. That is what is happening with MicroBlocks example. contains = i will cause the function to return the value of i.

--Curtis


I am not a Mad Scientist...  It makes me happy inventing new ways to take over the world!!
 
Bizzie
Senior Member

Joined: 06/07/2014
Location: Australia
Posts: 192
Posted: 07:52pm 05 Nov 2015
Copy link to clipboard 
Print this post

Thanks Curtis,

Will try to remember that for when ever I need to use a function in the future.


Rob White
 
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