String manipulation

Started by wlodekd, October 15, 2014, 07:22:17 AM

Previous topic - Next topic

wlodekd

Dear Forum

Please help to find the most effective way of stripping N sequence numbers form text file below

This:
N8 T1 M6
N9 M53
N10 G18
N11 G90 S611 M3
N12 G54
N13 M11
N14 M23
N15 G0 A0. C0.
N16 X0. Z11.3417 A11. C-90. M8

Into this
T1 M6
M53
G18
G90 S611 M3
G54
M11
M23
G0 A0. C0.
X0. Z11.3417 A11. C-90. M8


Thank you

JTaylor

One option would be to use ArrayFileGet() and then loop through using ItemRemove() with a "space" as a delimiter to remove the first item and then write back out using ArrayFilePut().

Jim

wlodekd

Thank you for your help. Do you have idea how to remove NXXX if space is not present? Delimiter would be any letter.

N8T1M6
N9M53
N10G18
N11G90S611M3
N12G54
N13M11
N14M23
N15G0A0.C0.
N16X0.Z11.3417A11.C-90.M8
N17Y8.1841

JTaylor

Now you are making it complicated  ;)

Sounds like a job for RegEx but I don't know that well enough to help.   Maybe someone else will chime in or perhaps look in the Tech Database for Regular Expressions.

Jim

wlodekd

You are right that Regular Expression is a solution, unfortunately I do not see direct support in Winbatch.


Thank you for your help

JTaylor

Search for "Email Address Validator" in the tech database for one example using a VBScript.RegEx object.

Search for "email checker regex" for an example using DotNet, assuming you have a fairly recent version of WinBatch.

If you know how to use Regular Expressions that should get you started.

Jim





td

Quote from: wlodekd on October 15, 2014, 11:32:14 AM
You are right that Regular Expression is a solution, unfortunately I do not see direct support in Winbatch.


Thank you for your help

WinBatch supports Regular expression through COM ( progid -"VBScript.RegExp" ) or dotNet  ( FCL class - Regex.)  Both are readily available since they are part of the OS.  However, there are multiple other ways to perform  the task in WinBatch using the built in string functions.

Here is but  one of many possibilities:

Code (winbatch) Select

;; Removes leading Nxxx prefix where x is a single digit (0-9).
#DefineFunction RemoveNxxx(_strText)
   nPos = 1
   if StrUpper(StrSub(_strText, nPos, 1)) == "N"
      nPos += 1
      ;; Position past digits.
      while StrTypeInfo( StrSub(_strText, nPos, 1), 1) & 4
         nPos += 1
      endwhile
   endif
   return StrSub(_strText, nPos, -1)
#EndFunction


; Test
strTest = "N11G90S611M3"
strNoNxxx = RemoveNxxx(strTest)

Message(strTest, strNoNxxx)
 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

Interesting....don't recall seeing StrTypeInfo() before.

Jim

George Vagenas

Assuming the list is delimited with @crlf:
Code (winbatch) Select
List = strreplace(List, @crlf, @lf)
; Create a proper item list (no trailing separator)
while strindex(List, @lf, 0, @backscan)==strlen(List)
   List = strsub(List, 1, strlen(List)-1)
endwhile                       
NewList = ''
Cnts = itemcount(List, @lf)
for Cnt = 1 to Cnts
   Old = itemextract(Cnt, List, @lf)
   Ndx = 3
   while   isint(strsub(Old, Ndx, 1))
      Ndx = Ndx+1
   endwhile
   NewList = iteminsert(strsub(Old, Ndx, -1), -1, NewList, @lf)
next ;Cnt

clipput(NewList)
       
:cancel   
   return

Thanks

George

wlodekd

I tried example and received error illegal syntax nPos += 1. It works great with  nPos=nPos+1 instead.

Post #6 - Very nice and compact solution to the problem (string without delimiters).

Please explain  "while StrTypeInfo( StrSub(_strText, nPos, 1), 1) & 4"
StrSub(_strText, nPos, 1) returns single digit, then StrTypeInfo should return 4, then 4 & 4 is 1 - condition is TRUE

I tried "while StrTypeInfo( StrSub(_strText, nPos, 1), 1) == 4"  and it did not work



Thank you to everybody for the help and great ideas.

stanl

nPos += 1.   This refers to the new operations available in the lastest WB version. It would replace having to use nPos = nPos+1

The & 4 -  is a check that the function retruns a numeric.

Tonys function is great. To make it less specific to your situation, could probably have

#DefineFunction Removetxt(txt,leadchar,chk)  or
Code (WINBATCH) Select

#DefineFunction Removetxt(txt,leadchar,chk)
   nPos = 1
   if StrUpper(StrSub(txt, nPos, 1)) == leadchar
      nPos += 1
      ;; Position past digits.
      while StrTypeInfo( StrSub(_strText, nPos, 1), 1) & chk
         nPos += 1
      endwhile
   endif
   return StrSub(txt, nPos, -1)
#EndFunction

newtxt = Removetxt(txt,"N",4)



 







td

Quote from: wlodekd on October 16, 2014, 04:39:15 AM
I tried example and received error illegal syntax nPos += 1. It works great with  nPos=nPos+1 instead.

That is because you are not using the latest version of WinBatch.

Quote
Post #6 - Very nice and compact solution to the problem (string without delimiters).

It is fine as long as all the assumptions made by the script hold.  It has compactness at the price of flexibility which is not necessarily either a bad or good thing.  It depends on your immediate and long term goals.

Quote
Please explain  "while StrTypeInfo( StrSub(_strText, nPos, 1), 1) & 4"
StrSub(_strText, nPos, 1) returns single digit, then StrTypeInfo should return 4, then 4 & 4 is 1 - condition is TRUE

I tried "while StrTypeInfo( StrSub(_strText, nPos, 1), 1) == 4"  and it did not work

We go to great effort to provide help files that explain both WIL functions and operators.  You can find detailed explanations of both the function StrTypeInfo, and bitwise operators like '&' and relational operators like '==' there.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade