Tue Mar 26, 2019 2:32 pm
Login Register Lost Password? Contact Us


Finding 1st occurrence of a character from a set in a strin

Questions around writing code and queries

Mon Dec 10, 2018 11:36 am Change Time Zone

Hi,

Maybe I've missed some functionality in the STD. But usually, in any harness, there is some capability to find an occurrence in a string of from a set of characters. (either 1st or last)

e.g. along the lines of:
Code: Select all
FindFirstOccurrenceOf([CBD],'Allan Bill Charlie')     <= returns 7 if counting from 1

.
I can do this in ECL, but in a very round about way, e.g.
Code: Select all
s := '../../dir/../Allan.exe';
LENGTH(s) - LENGTH(REGEXREPLACE('^([\\./]){1,}',s,''));

evaluates to '6', being the length of the wild-carded component to the pathname.
Is there a better way to find an occurrence in a string from a SET of characters?
And the NOT of above.

Yours
Allan
Allan
 
Posts: 351
Joined: Sat Oct 01, 2011 7:26 pm

Mon Dec 10, 2018 2:22 pm Change Time Zone

Allan,

Here's how I would do it:
Code: Select all
FindOccurrenceOf(SET OF STRING SetStr,STRING str,BOOLEAN FirstOne=TRUE) := FUNCTION
  RetDS := DATASET(LENGTH(str),
                   TRANSFORM({UNSIGNED2 Pos,BOOLEAN Fnd},
                             SELF.Pos := COUNTER,
                             SELF.Fnd := str[COUNTER] IN SetStr));
  RetVal := IF(FirstOne,MIN(RetDS(Fnd=TRUE),Pos),MAX(RetDS(Fnd=TRUE),Pos)); 
  RETURN RetVal;
END;

FindOccurrenceOf(['C','B','D'],'Allan Bill Charlie');         //  <= returns 7
FindOccurrenceOf(['C','B','D'],'Allan Bill Charlie',FALSE);   //  <= returns 12
FindOccurrenceOf(['W','X','Y'],'Allan Bill Charlie');         //  <= returns 0
FindOccurrenceOf(['W','X','Y'],'Allan Bill Charlie',FALSE);   //  <= returns 0

HTH,

Richard
rtaylor
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 1434
Joined: Wed Oct 26, 2011 7:40 pm

Mon Dec 10, 2018 3:28 pm Change Time Zone

And, of course, if you want to add case INsensitive searches to the mix, here's my "final" form for this function: :)
Code: Select all
//Find the first or last instance of any of a set of characters within a string,
// case sensitive or insensitive

IMPORT Std;
UpperIt(STRING s) := Std.Str.ToUpperCase(s);
FindCharFromSetInString(SET OF STRING SetStr,STRING str,
                        BOOLEAN FirstOne=TRUE,BOOLEAN Caseless=FALSE) := FUNCTION
  UpperSet := SET(DATASET(COUNT(SetStr),
                          TRANSFORM({STRING1 char},
                                    SELF.char := UpperIt(SetStr[COUNTER]))),
                  char);
  RetDS := DATASET(LENGTH(str),
                   TRANSFORM({UNSIGNED2 Pos,BOOLEAN Fnd},
                             SELF.Pos := COUNTER,
                             SELF.Fnd := IF(Caseless,
                                            UpperIt(str[COUNTER]) IN UpperSet,
                                            str[COUNTER] IN SetStr)));
  RetVal := IF(FirstOne,MIN(RetDS(Fnd=TRUE),Pos),MAX(RetDS(Fnd=TRUE),Pos)); 
  RETURN RetVal;
END;

FindCharFromSetInString(['a','b','d'],'Allan Bill Charlie');         //  <= returns 4 
FindCharFromSetInString(['a','b','d'],'Allan Bill Charlie',FALSE);   //  <= returns 14
FindCharFromSetInString(['c','b','d'],'Allan Bill Charlie',,TRUE);   //  <= returns 7 
FindCharFromSetInString(['C','B','D'],'Allan Bill Charlie');         //  <= returns 7
FindCharFromSetInString(['C','B','D'],'Allan Bill Charlie',FALSE);   //  <= returns 12
FindCharFromSetInString(['W','X','Y'],'Allan Bill Charlie');         //  <= returns 0
FindCharFromSetInString(['W','X','Y'],'Allan Bill Charlie',FALSE);   //  <= returns 0 

HTH,

Richard
rtaylor
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 1434
Joined: Wed Oct 26, 2011 7:40 pm

Fri Dec 21, 2018 4:25 pm Change Time Zone

Ah Richard,

I missed these replies.

Thanks very much. But this functionality still seems fundamental enough for it to have its own STD lib function?

Yours
Allan
Allan
 
Posts: 351
Joined: Sat Oct 01, 2011 7:26 pm

Fri Dec 21, 2018 4:51 pm Change Time Zone

Allan,

Sure. JIRA :)

Richard
rtaylor
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 1434
Joined: Wed Oct 26, 2011 7:40 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron