Function to process bytes in a string?
Moderator: DosItHelp
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
Thank you for responding avery_larry.
Well in the posted code Complete function, I would like to get every string that begins with a $ and up to the next white space. So for example
would be our string once I find the first $ that should be the starting point telling the script "ok we found a string that starts with a dollar sign." lets process this string char by char until we reach a white space. In the example however I just echo the strings to the console window when in reality they will be set to random variables within the script. I hope this clarifies a bit, if not please ask questions and I will do my best to answer. Thank you again.
Well in the posted code Complete function, I would like to get every string that begins with a $ and up to the next white space. So for example
Code: Select all
Hello $World The $Cat In The Hat
Let's see if this helps achieve what you are trying to do.
The IN_STR Function can basically be used to find the starting postition of a character in a string. (See usage for details within the code)
So we start by asking IN_STR for the position of the first $. Once we find that then we use IN_STR again to find the first white space after the position we found in our first search.
Now that we know where the $ starts and the first white space after that starts, you can simply parse that section of your string.
Hope the code makes sense and is helpful.
The IN_STR Function can basically be used to find the starting postition of a character in a string. (See usage for details within the code)
So we start by asking IN_STR for the position of the first $. Once we find that then we use IN_STR again to find the first white space after the position we found in our first search.
Now that we know where the $ starts and the first white space after that starts, you can simply parse that section of your string.
Hope the code makes sense and is helpful.
Code: Select all
:: START MAIN SCRIPT
@Echo Off
Set val=0
Set String=Hello $World The $Cat In The Hat
Call :IN_STR 0 "$" "%String%" /i
Set VAL1=%val%
Call :IN_STR %VAL1% " " "%String%" /i
Set /a VAL2=%val%-%Val1%
Call set "StringWord=%%String:~%val1%,%Val2%%%"
Echo StringWord = %StringWord%
Goto :EOF
:: END-MAIN SCRIPT
:: START FUNCTION
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: IN_STR : Used to return the position of a string
:: Usage:
:: CALL :IN_STR (Start Position) (String to search for) (String to search) (/i=case non-case-sensative)
:IN_STR
Set StartPosition=
Set StringFind=
Set StringSearch=
Set Case=
Set StartPosition=%1
Set StringFind=%2
Set StringSearch=%3
If /i "/i"=="%4" Set Case=%4
Set StringFind=%StringFind:"=%
Set StringSearch=%StringSearch:"=%
Set val=%StartPosition%
:IN_STR_LOOP
Call set "StringResult=%%StringSearch:~%val%,1%%"
Echo "%StringResult%"
If /i "%StringResult%"=="" Goto IN_STR_NOT_FOUND
If %Case% "%StringResult%"=="%StringFind%" Goto IN_STR_FOUND
Set /a val=%val%+1
Goto :IN_STR_LOOP
:IN_STR_FOUND
Set /a val=%val%+1
Echo String "%StringFind%" found at position %val%
Goto :EOF
:IN_STR_NOT_FOUND
Set val=0
Echo String "%StringFind%" could not be found.
Goto :EOF
:: END FUNCTION
-
- Posts: 319
- Joined: 12 May 2006 01:13
compared to the above whole bunch of code (and a very slow one too) like that, here's the better alternative.
output
of course, the above batch can be modified to do the same as my vbscript. just delims on space and check for $ in each token.
Code: Select all
str="Hello $World The $Cat In The Hat"
s = Split(str," ")
For i=LBound(s) To UBound(s)
If InStr(s(i),"$") > 0 Then
WScript.Echo s(i)
End If
Next
output
Code: Select all
C:\test>cscript /nologo test.vbs
$World
$Cat
of course, the above batch can be modified to do the same as my vbscript. just delims on space and check for $ in each token.
-
- Posts: 319
- Joined: 12 May 2006 01:13
isacmahad wrote:First off I don't know what you benchmark with but you are absolutely wrong.
oh is it? firstly, the method of testing is wrong. the vbscript displays those words that have $ in them... whereas your first batch only is able to display $World. and have you thoroughly tested your second batch? Your second batch fails (hang) if the string contains more words that begin with $. Try the second batch with this string
Code: Select all
set MAINStr=Hello $World $Hello World $cat.
It hangs at my side. In conclusion, your benchmark is ambiguous as both produces different results. Oh, have i told you development time counts as well, not just execution time?
-
- Posts: 319
- Joined: 12 May 2006 01:13
isacmahad wrote:Im quite aware that my script hangs however if a white space is left as the last char at the end of that string it will not hang which is why I added the for loop to take them out at the end, and then after you digest that maybe next you will read the beginning lines of the post right above your last post. I am no pro at batch scripting hence why I am here you neanderthal.
so if you are aware of that, then your benchmark is 100% wrong. It is not apple to apple comparison. If you want to compare them properly, at least amend your code and test them out, before spouting nonsense.
-
- Posts: 319
- Joined: 12 May 2006 01:13