I have a sub that is part of an include file & I want to process a string from the main code, so the sub won't know in advance the string variable name, unless it can be passed or set somehow from main.
The string might be very long so not really practical to pass the variable itself.
It would be possible to specify the String name the main code has to use but would be preferable to let the programmer choose.
or pass a string of the VarName but I don't think think this can be applied to a variable name in the sub. ( I tried)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Pass the string from main to your sub. In the sub use ( in localarray() ). So, you treat the the string as an array in the sub. This should work up to 254 string length.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I was thinking a false assumption...
Passing a string to a Sub already behaves as I want, just needed to get the structure right.
RAM is only allocated for the source string (not duplicated by the sub as I first thought)
Here's the quick test code
#OPTION explicit#chip 12F683,8#config Osc = IntDimStringVarasString*36StringVar="0123456789abcdefghijklmnopqrstuvwxyz"Main:ExtractASCII_2(StringVar)'; ExtractASCII_2("0123456789abcdefghijklmnopqrstuvwxyz") '<-sameMem.tableGotoMainReturn' <- Just 4 IDESub ExtractASCII_2 (SubStr as string)Dim i, StrPos, StrChr StrPos = SubStr(0) For i = 1 to SubStr(0) StrChr = SubStr(StrPos) ';processStrChr...StrPos--NextEndSub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have a sub that is part of an include file & I want to process a string from the main code, so the sub won't know in advance the string variable name, unless it can be passed or set somehow from main.
The string might be very long so not really practical to pass the variable itself.
It would be possible to specify the String name the main code has to use but would be preferable to let the programmer choose.
or pass a string of the VarName but I don't think think this can be applied to a variable name in the sub. ( I tried)
Try this.
Pass the string from main to your sub. In the sub use ( in localarray() ). So, you treat the the string as an array in the sub. This should work up to 254 string length.
I was thinking a false assumption...
Passing a string to a Sub already behaves as I want, just needed to get the structure right.
RAM is only allocated for the source string (not duplicated by the sub as I first thought)
Here's the quick test code
Looks good to me. But, if you treat the string like an array it will be a lot faster.
Recommend not to use single letter variables as some MCUs have one letter bits.
" if you treat the string like an array it will be a lot faster"
I thought I was here...
SubStr(0)
SubStr(StrPos)
Do you mean reference the string as an array ?
I can only get (SubStr as string) to work
(SubStr as Array) Does not compile.
This will work, but, it is any faster? It may not make any difference.
Note: I am using
_Substr
as there is function calledSubstr
Last edit: Anobium 2022-05-29
Thanks .
Looking in Help under
... Subroutines, Using subroutines:
... Arrays:
I don't see any mention of empty brackets () as reference to an Array.
Search engine doesn't like () but searching the PDF help shows examples.
cheers