pattern matching

   4904   7   1
User Avatar
Member
176 posts
Joined: July 2005
Offline
I want to detect a single specific character…
How can you use pattern matching to if $str would be just x, y or z

I thought this would work:

if ($str=
message !
endif


thanks! 8)
User Avatar
Member
639 posts
Joined: July 2005
Offline
if (`strmatch($str, “xyz”)` == 1)
message !
endif
User Avatar
Member
321 posts
Joined: July 2005
Offline
TheUsualAlex
if (`strmatch($str, “xyz”)` == 1)
message !
endif

I don't have Houdini in front of me,but iirc, strmatch() matches sequences of characters, not single characters. You could do “*xyz” for example, and it will match anything that ends in xyz.

But to match single letters, if the quantity is small, you'll need to make individual ‘if’ statements:

if ( ($str == “x”) || ($str == “y”) || ($str == “z”) ) then
….
endif

Now, if you have many more letters to compare against, and if this test is not inside a tight loop (translation: you can afford the overhead), then you could do:

if ( `system(“echo \”$str\“ | perl -ne 'print ”1" if m//'")` ) then

endif

anything inside the square brackets are treated as matching characters.

– Antoine
Antoine Durr
Floq FX
antoine@floqfx.com
_________________
User Avatar
Member
166 posts
Joined: Feb. 2006
Offline
It will be so nice if someone or SESI :wink: will implement a cmd using the Boost's Regex library
http://www.boost.org/libs/regex/doc/index.html [boost.org]
Time to get out of this messy world.
User Avatar
Member
176 posts
Joined: July 2005
Offline
thanks for clearing things up Antoine! I was a little confused at first and was wondering why I wasnt getting any results with strmatch.

Only problem is, perl isn't on any of the machines here…but I asked about it, and I should be able to start testing your method soon as it is installed!

thanks!
User Avatar
Member
321 posts
Joined: July 2005
Offline
lynch_ppl
thanks for clearing things up Antoine! I was a little confused at first and was wondering why I wasnt getting any results with strmatch.

Only problem is, perl isn't on any of the machines here…but I asked about it, and I should be able to start testing your method soon as it is installed!

thanks!

Though Perl is the “easy” way to do it, you could write an expr function that sort-of looks like this:

string matchChars(string sourcestr; string testchars)
{

float i = 0;
string testc = “”;
float matched = 0;

for(i=0; i<strlen(testchars); i++) {
testc = substr(testchars, i, i+1);
if (index(sourcestr, testc) > -1) then
matched = 1;
endif
}

return matched;
}


I just wrote this, so it is completely untested. It *should* return ‘1’ if any of the characters from the testchars are present in the source string.

– Antoine
Antoine Durr
Floq FX
antoine@floqfx.com
_________________
User Avatar
Member
176 posts
Joined: July 2005
Offline
Thanks again Antoine…just trying to understand this, as I've recently wanted to start learning how to use expression functions in houdini….

If I understand this correctly…

# if testc=no value, then matched=0
float i = 0;
string testc = “”;
float matched = 0;

the second one…
# for i=0 to….a number that is smaller than strlen(testchars); the i++… is that increment i by i?
for(i=0; i<strlen(testchars); i++) {

# set testc = substr()
#so each time testc is run, would that return one more character on the string it returns each time the loop runs?
testc = substr(testchars, i, i+1);

# as long as the index returns higher than -1, there is a match in the pattern
# if index()>-1; set matched to 1
if (index(sourcestr, testc) > -1) then
matched = 1;
endif
}

I just want to understand expression language which is why I'm trying to break this down…
:wink:
User Avatar
Member
321 posts
Joined: July 2005
Offline
lynch_ppl
Thanks again Antoine…just trying to understand this, as I've recently wanted to start learning how to use expression functions in houdini….

If I understand this correctly…

# if testc=no value, then matched=0
float i = 0;
string testc = “”;
float matched = 0;

the second one…
# for i=0 to….a number that is smaller than strlen(testchars); the i++… is that increment i by i?
for(i=0; i<strlen(testchars); i++) {

Yes, ++ is the increment operator, and – is the decrement operator. I believe the expression function language (and Vex, for that matter) honor pre-incrementing (++i) versus post-incrementing (i++) (these are all from the C language). As far as the for/next loop goes, yes, it starts at zero and keeps going until the test in the middle fails, which will happen when the incrementing variable (i) exceeds the length of the testchars string.
# set testc = substr()
#so each time testc is run, would that return one more character on the string it returns each time the loop runs?
testc = substr(testchars, i, i+1);
Yes, the substr is the same substr() as when you do an ‘exhelp’. iirc, substr()'s arguments are the source string, the starting position, and the ending position. Hmmm, I just looked it up, and that is *not* correct: substr() takes string, starting position, and *number of characters*, so the correct code should have been:

testc = substr(testchars, i, 1);

Sorry 'bout that.

– Antoine
Antoine Durr
Floq FX
antoine@floqfx.com
_________________
  • Quick Links