# Convert Group Array Suffix to Integer Value

## Recommended Posts

Ok, I have many objects separated into point groups by unique suffixes.

I'd like to split the suffix integer value from the array of group names.

Then assign that integer to a custom attribute.

I'm terrible at VEX so any suggestions would be greatly appreciated.

##### Share on other sites

you need to realize that one point can belong to multiple groups

that's why it's much easier to use name attribute to separate geometries within one detail

you can use name SOP to convert groups into name attrib

then you can use opdigits() vex function to extract number from the name

if you still decide to use groups, here is example how you can get array of all groups each point belongs to:

http://forums.odforce.net/topic/23822-how-to-get-the-group-name-from-points/?p=140187

Edited by anim

##### Share on other sites

Yeah, it certainly isn't an ideal setup but I'm working from a cache; so it's a bit wonky from the start.

I had no idea there was a name node, that's awesome. Thanks for that bit.

The point group array setup is super helpful too.

I'm just stuck on the opdigits part though. From within the Point Wrangle node, opdigits isn't a recognized function...? Would you apply it from another node?

##### Share on other sites

opdigits() is part of VEX since H15

##### Share on other sites

if you are using H14, you could still write something yourself using the find() function to search for 0-9 and then split the string based on the index found using the python notation: groupname[idx:], that should return you the end of the string where the numbers are (unless you also use numbers halfway into your group)

and afterwards simply use a atoi() function.

If you are still on H13, Im afraid you are basically out of luck, string functions back then were not really great

so in that case you probably want to use a python SOP

Edited by acey195

##### Share on other sites
Yeah, I'm in H14.

Ok, so I apologize in advance for my stupidity. So here's what I have but it basically just spits out a matrix.

I'd ideally like a for loop within a for loop, if that makes any sense.

Or is there a way to store/reference the original split string array without splitting the group array?

string ptGrps[] = detailintrinsic(0, "pointgroups");

s[]@ptinGrps = {};

foreach(string grp;ptGrps){

if (inpointgroup(0, grp, @ptnum)) append(s[]@ptinGrps, grp);

split(s[]@ptinGrps, grp, "_");

}

s[]@indexAttr = {};

foreach(int i; string s; ptGrps){

s@indexAttr = @ptinGrps[1];

}

/* String to Int

string indexString[] = {};

foreach(int i; string s; indexString){

i@objectID = atoi(s);

}

*/

Group_ID_Array_01.hipnc

##### Share on other sites

for extracting last digits you can for example use regular expressions:

```// this is mostly for <=H14, since H15 you can use native opdigits() function instead, however that will return 0 if digit not found
int getTrailDigits(string name)
{
// returns trailing digits of the name
// returns -1 if no triling digits found
int digit = -1;
string sdigit = re_find(r"\d*\$", name);
if (sdigit != "") digit = atoi(sdigit);
return digit;
}```

- so what's wrong with name attribute? If you use Name SOP to create name attrib from group matching your pattern (group*) then you can just additionally to defining above getTrailDigits() function do

`i@digit = getTrailDigits(s@name);`

- or if you really want all in VEX you can go through point groups belonging to the point and if group matches pattern, extract digit, so in addition to defining the getTrailDigits() function you can do:

```// finds first group that matches pattern that point belongs to and extracts it's digit, -1 otherwise
string grppattern = "group*";
string ptgrps[] = detailintrinsic(0, "pointgroups");

i@digit = -1;
foreach(string grp;ptgrps)
{
if (inpointgroup(0, grp, @ptnum) && match(grppattern, grp))
{
i@digit = getTrailDigits(grp) ;
break;
}
}```

- if you really want to go with gathering all the groups that the point is in and then extracting all the digits from them, which I don't think you really care about but just for completness, again in addition to defining the getTrailDigits() function (notice no nested loops necessary):

```// gets array of groups that point belongs to and extract array of digits from them
string ptgrps[] = detailintrinsic(0, "pointgroups");
s[]@ptingrps = {};
i[]@grpdigits = {};
foreach(string grp;ptgrps)
{
if (inpointgroup(0, grp, @ptnum))
{
append(s[]@ptingrps, grp);
append(s[]@grpdigits, getTrailDigits(grp) );
}
}```

all methods in attached fixed example

Group_ID_Array_01_fix.hipnc

• 3

##### Share on other sites

There is also random_shash function available from H14. Return random integer determined by string seed. Somewhat related.

##### Share on other sites

Tomas. You're amazing. Thank you sooo much for taking the time to do all this. I can't emphasize how much I appreciate it. Just so awesome.

##### Share on other sites

There is also random_shash function available from H14. Return random integer determined by string seed. Somewhat related.

I'm really trying to learn VEX so anything that gets thrown my way helps. Thanks so much!

## Create an account

Register a new account