WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: oradba4u on March 23, 2015, 11:52:50 AM

Title: Scratching my head over this one... (String Extraction)
Post by: oradba4u on March 23, 2015, 11:52:50 AM
All:
I am reading a text file. Each line contains 7-18 groups of ââ,¬Ëœnumbersââ,¬â,,¢ (including zero) such as:

0     0     0     0     0     2     0     0     0     22
2     2     0     0     0     12     1     0     2     3     10
2     2     0     0     0     12     15
0     2     0     2     0     0     0     11     2     0     1     1     1     4

Regardless of the line length, there are always 5 blank spaces between each of the numbers. The first number always starts at character position 23 of the line.
Any help would be greatly appreciated, as I have spent a lot of time/effort failing this task.
1-) How can I extract each (numeric string) from the line?
2-) Convert this string to a number.
As always, your help is appreciated.

Banging my head in Bangor
Title: Re: Scratching my head over this one... (String Extraction)
Post by: td on March 23, 2015, 01:33:11 PM
Your description of the problem is missing a few important details but here is an example of a partial solution.  There are much more efficient ways to solve the problem but the example is hopefully simple to understand and should be view as a potential launching point for better solution.

Code (winbatch) Select

; Create test data.
strFile = DirScript():'TestDummy.txt'
if !FileExist(strFile)
   strRecs = '0     0     0     0     0     2     0     0     0     22':@LF
   strRecs :='2     2     0     0     0     12     1     0     2     3     10':@LF
   strRecs :='2     2     0     0     0     12     15':@LF
   strRecs :='0     2     0     2     0     0     0     11     2     0     1     1     1     4'
   FilePut(strFile, strRecs)
endif

; Sum of all integers repressented in the file.
nSum = 0
hTest = FileOpen(strFile, "Read")
while 1
   strLine = FileRead(hTest)
   If  strLine == "*EOF*" Then Break
   nItems = ItemCount(strLine, " ")
   for i = 1 to nItems
      Item = ItemExtract(i, strLine, " ")
      if strLen(Item) then nSum += Item
   next
endwhile
FileClose(hTest)

Message("Sum of all Integers", "Sum = ":nSum)
Title: Re: Scratching my head over this one... (String Extraction)
Post by: oradba4u on March 23, 2015, 04:27:05 PM
Yeah, after looking at what I sent, I can see that I certainly didn't give you enough information

There may be several thousand lines of data -   line=fileread(handle)
I mentioned that the first number in the data line always starts at character position 23, so I want to "skip" anything before that.
There can only be a total of 16 numbers on each data line (after column 23). There is always 5 spaces between the numbers (if that helps).
Also, I want to sum numbers vertically, not horizontally... By that I mean in the example I gave 4 lines of data from the file:
0     0     0     0     0     2     0     0     0     22
2     2     0     0     0     12     1     0     2     3     10
2     2     0     0     0     12     15
0     2     0     2     0     0     0     11     2     0     1     1     1     4

I'm trying to sum vertically into an array: value[1], value[2]... value[16]

So from the sample data I sent:
value[1]=0+2+2+0
value[2]=0+2+2+2
value[3]=0+0+0+2
value[6]=2+12+12+0  and so on.

Hope this helps...
Title: Re: Scratching my head over this one... (String Extraction)
Post by: td on March 24, 2015, 06:41:06 AM
It is relatively easy to modify the previously posted script to sum columns instead of rows.  Just create an array the size of the max number of columns and use a variable to track the current column.  You can use the StrSub function to remove the first 23 characters.