Concat string and int in VEX

   21569   8   2
User Avatar
Member
82 posts
Joined: April 2019
Offline
Hi Everybody……..

The solution to the problem I am having can be very simple, but not being able to solve it.
How to Concat a string and an integer variable say “i” in VEX.

I wanted to create a SOP creating number of groups given by user.
i tried different things like,

newgroup(“group”i);
newgroup(“group”&i);
newgroup(“group”+i);
newgroup(“group”$i);

But nothing is working. Plz Help.
Thanking in advance.

—–iamjaideep80
iamjaideep80 (Jaideep Khadilkar)
User Avatar
Member
639 posts
Joined: July 2005
Offline
Hello,

There is a concat(). I haven't tried this yet, but should do it:

newgroup(concat(“group”, i, j, k));

Where i, j, k will concatenate to “group”.

-Alex
User Avatar
Member
82 posts
Joined: April 2019
Offline
Hi TheUsualAlex,

Thanks for responding so quickly.
But concat doesn't work.
The actual problem is, there is no function to convert a number to string.
Looking forward to your answer.

——-iamjaideep80
iamjaideep80 (Jaideep Khadilkar)
User Avatar
Member
941 posts
Joined: July 2005
Offline
See my response over at the od forum.
Mario Marengo
Senior Developer at Folks VFX [folksvfx.com] in Toronto, Canada.
User Avatar
Member
7024 posts
Joined: July 2005
Offline
You're looking for “sprintf()” which will do what you want, all in one function. It lets you print strings, floats, ints etc, and it returns a single string.

Cheers,

Peter B
User Avatar
Member
639 posts
Joined: July 2005
Offline
woops… I guess I read too fast earlier…. ops:
User Avatar
Member
82 posts
Joined: April 2019
Offline
Thank u very much, all of u.
I got it, and its working.

But there came another problem.
As far as my knowledge, In case of SOP, Houdini executes all of the Vex program for all the points, one point at a time, And with “ptnum” variable we can get current point being processed. Right? Then why following code is not working.
Don't ask me the use, but i want to write a code that will take all the points, and will put them in different groups, with one point in each group. And the code is…..

sop Test03()
{
string this_grp = sprintf(“group%d”,ptnum);
newgroup(this_grp);
addgroup(this_grp,ptnum);
}
So suppose i have a grid with 100 points , from point number 0 to 99, it should give me 100 groups with corresoponding points in it. But when I did it, only one group “group0” is created and all the 100 points are added to it. Why??????????

When I tried to play with the logic, and wrote something like this……

sop Test03()
{
int i = 0;
for(i = 1;i<=Npt;i=i+1)
{
string this_grp = sprintf(“group%d”,i);
newgroup(this_grp);
if(ptnum==i)
{
addgroup(this_grp,ptnum);
}
}
}

This one is working fine, but is too expensive,
Why the First one is not working, when it is logically correct?????????????/
Waiting for your answer.

——–iamjaideep80
iamjaideep80 (Jaideep Khadilkar)
User Avatar
Member
941 posts
Joined: July 2005
Offline
iamjaideep80
But there came another problem.

Ah, right. I think that's because, in VEX, strings must be uniform (not varying). Still… there seems to be a little kink in VEX's armour , and you can trick it like this:
int i;
for(i=0;i<Npt;i++) {
if(i==ptnum) {
string gname = sprintf(“group%d”,i);
newgroup(gname);
addgroup(gname,i);
}
}
I think that works because it thinks that you're declaring Npt uniform strings – note that gname is built with i, not ptnum. In any case, I believe this is what you'd call a “major hack” and there's no guarantee it might work in the future

However, you can get the exact same functionality you're after (one group per point) by using the Partition SOP. Set the “Entity” to “Points”, and the “Rule” to “group$PT”, and you're done – much safer than that nasty code fragment above.
Mario Marengo
Senior Developer at Folks VFX [folksvfx.com] in Toronto, Canada.
User Avatar
Member
82 posts
Joined: April 2019
Offline
Hi Mario…..

Thanks Again. Nice explanation.
I completly fogot to think abt uniform and varying variables.
Again, Partition SOP is good. I hadn't used it before. But now I will.

Thanks.

————iamjadieep80
iamjaideep80 (Jaideep Khadilkar)
  • Quick Links