Focal Point
Substring and position from reverse

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/5051012532

July 02, 2007, 06:47 PM
getit
Substring and position from reverse
Hi ,
I have a field which is like
0110110011111001

I need to find the position of the second occurance of 1 from the end.
Is this possible..?


App Studio Version 8202
windows Platform
SQL Server 2008/2012
July 02, 2007, 11:05 PM
Leah
If the string is always the same length, you might create individual 1 character fields with edit, or possible using gettok or posit might help.


Leah
July 03, 2007, 03:07 AM
Alan B
There is no 1 step approach I think. Try:
DEFINE FILE CAR
NUM/A16='0110110011111001';
-* Remove trailing '0's
TRIM1/A16V= TRIMV('T',NUM,16,'0',1,'A16V');
-* Remove last character, which will be a '1'
TRIM2/A16V= SUBSTV(16,TRIM1,1,LENV(TRIM1,'I2')-1,'A16V');
-* Remove trailing '0's
TRIM3/A16V= TRIMV('T',TRIM2,16,'0',1,'A16V');
-* Length will be position of penultimate 1.
PLACE/I2=LENV(TRIM3,'I2');
END
TABLE FILE CAR
PRINT COUNTRY NUM TRIM1 TRIM2 TRIM3 PLACE
END

(Edit: Changed length 15 to 16 because, as Tony pointed out, I cannot count early in the morning!)

This message has been edited. Last edited by: Alan B,


Alan.
WF 7.705/8.007
July 03, 2007, 03:42 AM
Tony A
Alan, shouldn't the lengths be 16 and not 15? Wink (early morning)

Getit,

I did think about using GETTOK and setting the delimiter to '0' especially as the GETTOK allows search from end using -1 for the last and -2 for the second to last etc. But in the string supplied the second to last would actually be '' as there are two 0s together. The second to last 1 would need -3 and that would return 11111 Frowner

So, depending upon your file type the only thing that I can think of that may help is the POSITION attribute within a MFD (mas). From the DS help files -

FILENAME = EXAMPLE3, SUFFIX = FIX,$
SEGNAME = ONE, SEGTYPE=S0,$
FIELDNAME = A1 ,ALIAS= ,USAGE = A14 ,ACTUAL = A14 ,$
FIELDNAME = QFIL ,ALIAS= ,USAGE = A32 ,ACTUAL = A32 ,$
FIELDNAME = A2 ,ALIAS= ,USAGE = I2 ,ACTUAL = I2 ,$
FIELDNAME = A3 ,ALIAS= ,USAGE = A10 ,ACTUAL = A10 ,$
FIELDNAME = A4 ,ALIAS= ,USAGE = A15 ,ACTUAL = A15 ,$
SEGNAME = TWO, SEGTYPE=S0, PARENT = ONE, POSITION = QFIL, OCCURS = 4 ,$
FIELDNAME = Q1 ,ALIAS= ,USAGE = D8 ,ACTUAL = D8 ,$

You can see that this is a flat file and the repeating values are in field QFIL.

I'm sure that Alan will now go and knock up an example Wink for his library of useful tips

Good luck

T



In FOCUS
since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2  
WebFOCUS App Studio 8.2.06 standalone on Windows 10 
July 03, 2007, 08:43 AM
mgrackin
Here's another way to find the second to last occurance of the 1. The key to this techniaue is to reverse the string and then use POSIT to find the position of the second to last 1. This is probably not the best solution but it works.

DEFINE FILE CAR
ORGNUM/A16='0110110011111001';
REVNUM/A16=REVERSE(16,ORGNUM,'A16');
FSTONE/I2 =POSIT(REVNUM,16,'1',1,'I2');
NUMTWO/A16=SUBSTR(16,REVNUM,FSTONE+1,16,16-FSTONE,'A16');
SECONE/I2 =POSIT(NUMTWO,16,'1',1,'I2');
THEPOSITION/I2=(16-(FSTONE+SECONE))+1;
END
TABLE FILE CAR
PRINT ORGNUM REVNUM FSTONE NUMTWO SECONE
THEPOSITION AS 'The Second 1 is,Located at Position'
BY COUNTRY
WHERE RECORDLIMIT EQ 1
END


Thanks!

Mickey

FOCUS/WebFOCUS 1990 - 2011
July 03, 2007, 11:50 AM
getit
I came up with the exact solution as Alan Bs yesterday and it is working fine.
Mgrackin 's also is a good idea.

Thanks Guys!!


App Studio Version 8202
windows Platform
SQL Server 2008/2012
July 03, 2007, 03:45 PM
FrankDutch
I did not test either of the solutions, but what if the original number has only one "1" pe "0000000010" or "010000000" or "1"....

You can test this by first converting the number from binary to decimal.
If the decimal number is 2^0 till 2^15 than the binary number only holds one "1" and the other tests can be passed.

Just a thought...




Frank

prod: WF 7.6.10 platform Windows,
databases: msSQL2000, msSQL2005, RMS, Oracle, Sybase,IE7
test: WF 7.6.10 on the same platform and databases,IE7

July 04, 2007, 04:49 AM
Alan B
Frank,

The code I gave will return a PLACE value of 0, when the string contains only one '1'. The beauty of AnV fields. Smiler


Alan.
WF 7.705/8.007