Split string with empty entry VEX

   4444   4   2
User Avatar
Member
42 posts
Joined: March 2014
Offline
Hello,

I'm trying to split a string into an array by doing this:

s[]@turnlanes = split(s@turn_lanes,"|");

But it's not taking into account the fields that are left empty with just ||,
"left||" is split into just "left", where i would like it to insert "through" after each | and have it be split into "left,through,through".

How do I go about making this with vex?

Included file with OSM file, you might have to hook it up in the OSM Import.

Edited by stinzen - April 23, 2021 11:44:50

Attachments:
turnlane_split.JPG (34.4 KB)
split_turnlanes.hiplc (103.9 KB)
Boston_SouthWaterfront_small.osm (1.7 MB)

User Avatar
Member
9376 posts
Joined: July 2007
Offline
use re_split() to get empty strings inbetween separator
if you also want empty string after the last separator you have to check for it individually
s[]@turnlanes_split= re_split(r"\|", s@turn_lanes);
if (endswith(s@turn_lanes, "|")) append(s[]@turnlanes_split, "");

this will give you

left|| -> [ left, "", "" ]

then you can replace those empty strings with "through" or whatever
Edited by tamte - April 23, 2021 18:52:40
Tomas Slancik
CG Supervisor
Framestore, NY
User Avatar
Member
42 posts
Joined: March 2014
Offline
Thank you tamte!

Now I'm trying to figure out how to replace the empty strings, the last one in the if statement is easy of cource but I don't know how to change both of them. I tried with re_findall and re_replace but I can't seem to get the syntax to work.
User Avatar
Member
9376 posts
Joined: July 2007
Offline
since it's an array now, just loop through it and if the string is empty replace with whatever
s[]@turnlanes_split = re_split(r"\|", s@turn_lanes);
if (endswith(s@turn_lanes, "|")) append(s[]@turnlanes_split, "");

foreach(int i; string lane; s[]@turnlanes_split){
    if (lane == "") s[]@turnlanes_split[i] = "through";
}
Edited by tamte - April 26, 2021 13:31:43
Tomas Slancik
CG Supervisor
Framestore, NY
User Avatar
Member
42 posts
Joined: March 2014
Offline
Thank you so much! I have to practice a bit more with arrays and loops.
  • Quick Links