Convert a string array into an integer array

   4295   4   2
User Avatar
Member
40 posts
Joined: March 2018
Offline
Hello, I have got here the @neighbours that contains the neighbour primitives for each primitive. It happens to be a string.

I need an integer array with the neighbour primitives.

So, how can I convert a string “1 2 3 4 5 3 4 7” (whatever it is) into an integer array?

As you can see I could already split it into different characteres but need some help to finish it.

Thanks in advance.

Edited by Dav_Rod - June 29, 2018 19:18:01

Attachments:
asdffdas.JPG (118.2 KB)

User Avatar
Member
2036 posts
Joined: Sept. 2015
Offline
After your code line with the split function put this:

int a_rray[], Count;

for(Count = 0; Count < len(letras); Count++)
{
push(a_rray, atoi(letras[Count]));
}
Edited by BabaJ - June 29, 2018 21:18:54
User Avatar
Member
40 posts
Joined: March 2018
Offline
Thanks Babaj for the fast answer. I get the idea. I tried that but could not reach a good result, maybe because of some syntax error that I can not clean.

I also tried this:
if(@balcon == 1){
    string vecinos = primattrib(0, "neighbours", @primnum, 1);
    string letras[] = split(vecinos);
    int numbers[];
    foreach(string asdfg; letras)
        {
        push(numbers, atoi(asdfg));
        }
}


I attach the file in the case it can help.
Thanks in advance.

Attachments:
Davinia_file.hiplc (258.8 KB)

User Avatar
Member
2036 posts
Joined: Sept. 2015
Offline
I noticed your printf line.

Is that where your having issues?

Your running over primitives yet only creating an array of two ints for only when @balcon == 1.

But your still running over all the prims with the printf command, so when it tries to print for the prims with @balcon == 0,

it doesn't see an array, because you didn't create one for those prims.

If your ‘numbers’ array variable was an attribute, then a numbers attribute array would have been created for all prims, but since you still will only have values for that array on prims that are @balcon == 1, the other prims with that array would be empty and you still would get an error with your print command as is.

Have to remember that when ‘running over’ with wrangles it's multi-threading, so sometimes you need to do a second wrangle that follows if what your trying to accomplish can't be done in the one wrangle - or as a last option, run in detail mode.

I've changed your wrangle node and made the numbers variable into an attribute, so you can see that the values indeed get stored. And another wrangle that follows with the print command, just as another way so you can see.

Attachments:
Davinia_file-1.hiplc (238.2 KB)

User Avatar
Member
40 posts
Joined: March 2018
Offline
Thanks a lot for your answer Bajaj, that really clarified everything.
I appreciate your time and effort. Helped a lot.
  • Quick Links