ifs() ?

   17815   23   3
User Avatar
Member
12455 posts
Joined: July 2005
Offline
Hi all,

I am running into a limitation of the if() command because it will only return a float type, where it would useful to be able to return a string type at at times.

e.g., I want to do this:

`if( $F==1, “*”, “” )`

..so can I propose the addtion of a function called ifs() - a string return type if() function?

`ifs( $F==1, “*”, “” )`

Luckily, because I need only one character in this case, I can do this…

`substr(“ *”, $F==1, 1)`

But I've run into this situation a LOT. Does anyone have any other methods for conditionally selecting strings inside of expressions? The above method falls over with variable length strings.

Thanks,
Jason
Jason Iversen, Technology Supervisor & FX Pipeline/R+D Lead @ Weta FX
also, http://www.odforce.net [www.odforce.net]
User Avatar
Member
1631 posts
Joined: July 2005
Offline
Hey Jason,

I agree it will be extremely useful to have a ifs() function or modifying the if() function to return any data instead of just float.

RFE! RFE! RFE!

Cheers!
steven
User Avatar
Member
941 posts
Joined: July 2005
Offline
stevenong
RFE! RFE! RFE!
Ditto for me. Good one!
Mario Marengo
Senior Developer at Folks VFX [folksvfx.com] in Toronto, Canada.
User Avatar
Member
543 posts
Joined: July 2005
Offline
stevenong
RFE! RFE! RFE!

Double Ditto!!
RFE if you please! Yes, indeed!


:-)
========================================================
You are no age between space
User Avatar
Member
7714 posts
Joined: July 2005
Online
Why can't you use evals()?
User Avatar
Member
543 posts
Joined: July 2005
Offline
edward
Why can't you use evals()?
Doh! You're right, that should suffice.
========================================================
You are no age between space
User Avatar
Member
12455 posts
Joined: July 2005
Offline
I think my brain has frozen, but how do you think we can use evals() to do it?
Jason Iversen, Technology Supervisor & FX Pipeline/R+D Lead @ Weta FX
also, http://www.odforce.net [www.odforce.net]
User Avatar
Member
321 posts
Joined: July 2005
Offline
Mario Marengo
stevenong
RFE! RFE! RFE!
Ditto for me. Good one!

How about a simple expression function:

string ifs(float i; string s1; string s2)
{
if (i == 0) {
return s2;
} else {
return s1;
}
}


hscript> echo `ifs(1, “hello”, “there”)`

– Antoine
Antoine Durr
Floq FX
antoine@floqfx.com
_________________
User Avatar
Member
941 posts
Joined: July 2005
Offline
Antoine Durr
How about a simple expression function <snip>

Yep; that's a nice solution Antoine

Still…
It'd be nice to have it as a builtin function instead of having to parse and interpret that every time., no?

I'd let the RFE stand, and… oh, what the heck; how about extending it to the other nasty types?

vector ifv(bool,vector,vector)
matrix ifm(bool,matrix,matrix)
and so on… 8)


Good one Antoine!
Mario Marengo
Senior Developer at Folks VFX [folksvfx.com] in Toronto, Canada.
User Avatar
Member
12455 posts
Joined: July 2005
Offline
Ah yes, didn't think of that myself
For some reason, the expression editor completely bypasses me.

Thanks for the solution, Antoine.. I'd still like to see it be built-in as well though.

(Also a regsub would be nice, anyone?..)
Jason Iversen, Technology Supervisor & FX Pipeline/R+D Lead @ Weta FX
also, http://www.odforce.net [www.odforce.net]
User Avatar
Member
7714 posts
Joined: July 2005
Online
Err, nevermind evals() doesn't work. I vote for Antoine's solution.
User Avatar
Member
321 posts
Joined: July 2005
Offline
jason_iversen
(Also a regsub would be nice, anyone?..)

What's regsub()? What parameters would it have?

– Antoine
Antoine Durr
Floq FX
antoine@floqfx.com
_________________
User Avatar
Member
941 posts
Joined: July 2005
Offline
Antoine Durr
What's regsub()? What parameters would it have?

It's a hypothetical function that would perform pattern substitutions based on regular expression matching -> regsub.
It's a big part of most scripting languages; and a very handy thing indeed 8)
Regular expressions are pretty dense little things… not simple at all…

The Boost [boost.org] library has a full regex implementation (by John Maddock), so….


Anybody with access to the HDK and a few weeks to kill?


Cheers!
Mario Marengo
Senior Developer at Folks VFX [folksvfx.com] in Toronto, Canada.
User Avatar
Member
321 posts
Joined: July 2005
Offline
Mario Marengo
Antoine Durr
What's regsub()? What parameters would it have?

It's a hypothetical function that would perform pattern substitutions based on regular expression matching -> regsub.
It's a big part of most scripting languages; and a very handy thing indeed 8)
Regular expressions are pretty dense little things… not simple at all…


Cheers!

Well, how about:

string regsub(string source; string from; string to
{
string perlstr = “”;
perlstr = “echo ” + source + “ | perl -pe s/” + from + “/” + to + “/g”;
return system(perlstr);
}


hscript> echo `regsub(“hello”, “ll”, “foobar”)`
hefoobaro

– Antoine
Antoine Durr
Floq FX
antoine@floqfx.com
_________________
User Avatar
Member
941 posts
Joined: July 2005
Offline
Antoine Durr
Well, how about:

string regsub(string source; string from; string to
{
string perlstr = “”;
perlstr = “echo ” + source + “ | perl -pe s/” + from + “/” + to + “/g”;
return system(perlstr);
}


Yes. That's a nice example, Antoine.

Or you could use awk -> gsub(), or TCL -> regsub(), or … even grep will do in a pinch.

As I mentioned; you'll find it in most (all?) scripting languages, just pick the cross-platform poison of your choice.

But I think he was asking (hoping? ) for a built-in version… though I could be wrong.
It would be quite a bit “snappier” than a system call.

Cheers!
Mario Marengo
Senior Developer at Folks VFX [folksvfx.com] in Toronto, Canada.
User Avatar
Member
543 posts
Joined: July 2005
Offline
jason_iversen
I think my brain has frozen, but how do you think we can use evals() to do it?

You're right, I read through your posting a little too quick.

Returning matrices and vector would be nice too.


–Mark
========================================================
You are no age between space
User Avatar
Member
12455 posts
Joined: July 2005
Offline
Hi again,

Thanks for the snippet on using PERL for the substitution. Just FYI, I changed it a teeny bit to allow spaces in the parameters (it choked when I was trying to remove spaces)


string regsub(string source; string from; string to
{
string perlstr = “”;
perlstr = “echo ‘” + source + “’ | perl -pe ‘s/” + from + “/” + to + “/g’”;
return system(perlstr);
}


But, yes; to have some of these builtin would be great.

By that matter, what are the thoughts of binding in an established scripting language sometime? Say, having PHP native or something else that allows one to do file i/o, GUI toolkits, register procedures? I realise JavaScript and TCL/Tk can do this, but to have to communicate via hcommand is awkward and slow.
Jason Iversen, Technology Supervisor & FX Pipeline/R+D Lead @ Weta FX
also, http://www.odforce.net [www.odforce.net]
User Avatar
Member
12455 posts
Joined: July 2005
Offline
Here is another handy thing - this snippet will strip leading numbers, spaces, punctuation (except underbars); ultimately yeilding a name that could be a valid Houdini node name.


# legalize node names
#
string legalize_name(string source)
{
string perlstr = “”;
perlstr = “echo ‘” + source + "’ | perl -pe 's/^[]*|[^^_]//g'";
return system(perlstr);
}
Jason Iversen, Technology Supervisor & FX Pipeline/R+D Lead @ Weta FX
also, http://www.odforce.net [www.odforce.net]
User Avatar
Staff
2540 posts
Joined: July 2005
Offline
By that matter, what are the thoughts of binding in an established scripting language sometime? Say, having PHP native or something else that allows one to do file i/o, GUI toolkits, register procedures? I realise JavaScript and TCL/Tk can do this, but to have to communicate via hcommand is awkward and slow.

My gut tells me that building up hscript is the best solution of them all. Give it the functionality it needs to do what you want, especially handling of strings, vectors, matricies, and arrays. The overlap between this and VEX will get blurred pretty quickly.


-jeff
There's at least one school like the old school!
User Avatar
Member
12455 posts
Joined: July 2005
Offline
It'd be beautiful to have hscript become VEX-like with procedures and strong datatyping. Add arrays and File I/O and you're golden. This would also mean the the Expression language could be tossed and expression would be written in this new “VScript”

I'd love to see this too.

RIP, PERL support in Houdini. :? We miss you.
Jason Iversen, Technology Supervisor & FX Pipeline/R+D Lead @ Weta FX
also, http://www.odforce.net [www.odforce.net]
  • Quick Links