Parsing Items of Varying Length

Recommended Posts

In the data our press system pulls in, there is a job number in the format AXXXX. Lately the people running the presses that generate the data have been adding certain codes or a date to indicate something was done differently, and it tends to look something like AXXXX-ZZZ or AXXXX-08/21/2011. I didn't write the sequence and i'm fairly inexperienced in the program, but I was able to find the section of the code. I'm fairly sure it scans to the right end and reads 5 characters to the right:

datain = evaluate("device.Press" + pressnum + ".ReadUntil(10)")
datain = ltrim(datain)
thetime = systime()

...(irrelevant code)...

execute(pcode + "Job.AddValue(inserttime('" + 
left(right(datain,7),5) + "', thetime, -1))")

Here is a standard data sample:

Rx:   A 17172	 B  636.3	C  000.0	D  236.3	
E  409.5	F  042.8	G  001.9	Q  001.909  
R  07.8	 Z JUN 1   11:24:45 AM  A1526\x0D\x0A

With the job number as A1526, however it may come out as this:

Rx:   A 688	   B  676.3	C -001.4	D  001.1	
E  114.4	F  000.0	G  492.1	H  001.9339 
I  001.0410 J  000.7260 K  001.9312 L  000.7398 M -000.0665 
N -000.0363 O  001.3041 Q  003.911  R  04.0	 T  0000	 
Z JUL 1   11:18:32 AM  A1534-6.73SPL\x0D\x0A

With the job number A1534-6.73SPL. In this format, the job number would be read as 73SPL.

I have been playing with the "left(right(datain,7),5)" but changing the numbers alone doesn't work, as if you fix it for the long ones it will run into the data before it(the time) on the 5 character job numbers. Any ideas on how to change it so it can work right with varying lengths?

Link to comment
Share on other sites

You'll have to find the last space, and go from there. Use reverseFind() to find that last space. Then grab the next 5 characters. It looks something like:

left(mid(x, reverseFind(datain, " ") + 1,100),5)

reverseFind() returns the index (from the beginning of the string) of the last space is the string. We add one to the result to get the character after the space. Then we use the mid() function to extract from that point to the end (by specifying a really long number of characters, 100). Then we get the left most 5 characters.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.