Sun Apr 21, 2019 8:15 am
Login Register Lost Password? Contact Us


Splitting of a string in a dataset.

Comments and questions related to the Enterprise Control Language

Wed Feb 13, 2019 6:38 pm Change Time Zone

This is what I have
'PERSON|1$GEO|1'
'PERSON|2$GEO|2'

And this is what I want... Split on the '$'
'PERSON|1'
'GEO|1'
'PERSON|2'
'GEO|2'
Code: Select all
LineRec := RECORD 
  STRING x;
END;

all_lines := DATASET([{'PERSON|1$GEO|1'},
                      {'PERSON|2$GEO|2'}],LineRec);
wjblack
 
Posts: 21
Joined: Mon Jul 11, 2016 12:45 pm

Thu Feb 14, 2019 10:52 am Change Time Zone

wjblack,

Take a look at the Std.Str.SplitWords() standard library function. That should give you exactly what you want.

HTH,

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

Fri Feb 15, 2019 5:20 pm Change Time Zone

Thanks Richard. I threw this together once I was aware of std.str.splitwords

Code: Select all
LineRec := RECORD 
  STRING str;
END;

ds := DATASET([{'PERSON|1$GEO|1'},{'PERSON|2$GEO|2'}],LineRec);

outlay := {dataset({string split}) splits};

dsNew := project(ds,transform(outlay,
                                          self.Splits :=    dataset([std.str.splitwords((string)left.str,'$')],{string split})
                                          ));
                                          
normalize(dsNew,left.splits,transform({string Split},self:=right))
wjblack
 
Posts: 21
Joined: Mon Jul 11, 2016 12:45 pm

Fri Feb 15, 2019 5:50 pm Change Time Zone

Or, just a bit simpler:
Code: Select all
IMPORT Std;
ds := DATASET([{'PERSON|1$GEO|1'},{'PERSON|2$GEO|2'}],{STRING str});
SplitEm(STRING str) := DATASET(std.str.splitwords(str,'$'),{STRING s});
dsNew := PROJECT(ds,
                 TRANSFORM({DATASET({STRING s}) Splits},
                           SELF.Splits := SplitEm(LEFT.str))).splits;
dsNew;

;)

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

Tue Mar 19, 2019 12:56 am Change Time Zone

What about if it was like this? This is what I have

Code: Select all
'1','PERSON|1$GEO|1'
'2','PERSON|2$GEO|2'


And this is what I want... Split on the '$' in the second field.
Code: Select all
'1','PERSON|1'
'1','GEO|1'
'2','PERSON|2'
'2','GEO|2'
wjblack
 
Posts: 21
Joined: Mon Jul 11, 2016 12:45 pm

Tue Mar 19, 2019 1:30 pm Change Time Zone

wjblack,

This way produces what you want in a nested child dataset format:
Code: Select all
IMPORT Std;
ds := DATASET([{1,'PERSON|1$GEO|1'},{2,'PERSON|2$GEO|2'}],{UNSIGNED1 UID,STRING str});
SplitEm(STRING str) := DATASET(std.str.splitwords(str,'$'),{STRING s});
dsNew := PROJECT(ds,
                 TRANSFORM({UNSIGNED1 UID, DATASET({STRING s}) Splits},
                           SELF.UID := LEFT.UID;
                           SELF.Splits := SplitEm(LEFT.str)));
dsNew;

HTH,

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


Return to ECL

Who is online

Users browsing this forum: No registered users and 1 guest

cron