Function to process bytes in a string?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#16 Post by avery_larry » 03 Nov 2009 12:43

Still, I'm not following well what you're trying to do. Your code could probably be cleaned up, but it would help to start from scratch in my mind.

isacmahad
Posts: 44
Joined: 29 Oct 2009 23:08

#17 Post by isacmahad » 03 Nov 2009 13:49

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

Code: Select all

Hello $World The $Cat In The Hat
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.

DaveNamou
Posts: 1
Joined: 03 Nov 2009 10:27

#18 Post by DaveNamou » 03 Nov 2009 15:17

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.

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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#19 Post by ghostmachine4 » 03 Nov 2009 18:52

compared to the above whole bunch of code (and a very slow one too) like that, here's the better alternative.

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.

isacmahad
Posts: 44
Joined: 29 Oct 2009 23:08

#20 Post by isacmahad » 03 Nov 2009 23:24

-Removed by author.
Last edited by isacmahad on 04 Nov 2009 02:32, edited 1 time in total.

isacmahad
Posts: 44
Joined: 29 Oct 2009 23:08

#21 Post by isacmahad » 03 Nov 2009 23:48

-Removed
Last edited by isacmahad on 04 Nov 2009 03:24, edited 2 times in total.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#22 Post by ghostmachine4 » 04 Nov 2009 00:40

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?

isacmahad
Posts: 44
Joined: 29 Oct 2009 23:08

#23 Post by isacmahad » 04 Nov 2009 00:51

-Removed by author.
Last edited by isacmahad on 04 Nov 2009 02:30, edited 1 time in total.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#24 Post by ghostmachine4 » 04 Nov 2009 00:57

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.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#25 Post by ghostmachine4 » 04 Nov 2009 00:58

isacmahad wrote:Completely portable

portable in what sense ? can it work on linux?

isacmahad
Posts: 44
Joined: 29 Oct 2009 23:08

#26 Post by isacmahad » 04 Nov 2009 01:02

-Removed by author.
Last edited by isacmahad on 04 Nov 2009 02:29, edited 1 time in total.

isacmahad
Posts: 44
Joined: 29 Oct 2009 23:08

#27 Post by isacmahad » 04 Nov 2009 05:09

I STARTED THIS POST WHEN THE ADMIN COMES AROUND CAN YOU PLEASE SCRAP THIS THREAD. IT SERVES NO USABLE PURPOSE AND HAS ATTRACTED WAY TO MANY POINTLESS POST. THE SUBJECT MATTER WILL BE RE-POSTED ONCE THE T'S ARE CROSSED AND THE I'S ARE DOTTED.
ADMIN SCRAP THIS THREAD.
POST CLOSED-

Post Reply