Conditional based on partial product code
Collapse
X
-
That will find products that contain "custom" anywhere in their code, not just at the beginning. If you only want to find it at the beginning of the code, add the parentheses and "EQ 1" as in my earlier example.
Leave a comment:
-
So while goofing around I came up with this
It works. I don't want to take the "it works for me attitude", I do want to use correctly formatted code, so should I not use my snippet?Code:<mvt:if expr="'custom' CIN l.settings:product:code">
Leave a comment:
-
Hmmm, that code should work correctly, even if it takes a few extra nanoseconds :^) . But 'custom' is 6 characters long, not 7.
Leave a comment:
-
Okay, so now back to my original question
if I use this
is there a mvt:assign that goes before this?Code:<mvt:if expr="('custom' CIN g.myVariable) EQ 1">
I tried this and didn't get any results
Code:<mvt:assign name="g.checkThis" value="tolower(substring(l.settings:product:code, 1, 7))" /> <mvt:if expr="g.checkThis EQ 'custom'"> yes, this product code starts with custom </mvt:if>
Leave a comment:
-
Not off topic - this help me and probably many, many others learn.Originally posted by Kent Multer View Post
Excuse me for going off-topic, but as an old-school programmer*, I'm always intrigued by performance talk. Where did you learn that tolower() is faster? That solution uses substring(), so there are two function calls. I would have handled this case with-- which does use CIN, but has zero function calls instead of 2Code:<mvt:if expr="('custom' CIN g.myVariable) EQ 1">
Thanks -- Kent
Leave a comment:
-
CIN may be slower than IN, but the difference is internal to the VM. I suspect that it's less than the overhead for two function calls instead of one ... but excuse me for nit-picking, I learned to program back in the days when memory was measured in kilobytes, and clock speeds in MHz. My college buddies and I spent every lunch and dinner talking about that kind of stuff. Old habits die hard :^) .
Leave a comment:
-
Yea, that's right forgot about that...still think CINs are pretty slow...but unless we are talking about huge amount of code, not worth fretting over either way.Originally posted by Kent Multer View PostI'm pretty sure that CIN is not a Boolean operator; it returns a number representing the position of the substring. So it will only return 1 if the specified substring appears at the start of the larger string.
Leave a comment:
-
LOL, according to a certain developer's book on page 35, you would be correct Kent.
Scott
Leave a comment:
-
I'm pretty sure that CIN is not a Boolean operator; it returns a number representing the position of the substring. So it will only return 1 if the specified substring appears at the start of the larger string.
Leave a comment:
-
Not sure there was one place. But I certainly recall some of this is from work Ivo did waaayyy back in the day.
I'd think the problem with <mvt:if expr="('custom' CIN g.myVariable) EQ 1"> would be that it evaluates true for custom_product_a and product_customs_of_my_people. (I.e., its not tight enough).
CIN is definitely slower than IN as it has to test ever character twice.
But yea, it'd be great to have a 'compiled' resource of validated 'best practices'. At least that way we see less of things like:
<mvt:if expr="thisVar EQ 'something'">
<mvt:else>
Here's where we do things
</mvt:if>
:)
Leave a comment:
-
Excuse me for going off-topic, but as an old-school programmer*, I'm always intrigued by performance talk. Where did you learn that tolower() is faster? That solution uses substring(), so there are two function calls. I would have handled this case withOriginally posted by Bruce - PhosphorMedia View PostUsing tolower() is faster than having miva use CIN, and EQ again is faster than either CIN or IN).-- which does use CIN, but has zero function calls instead of 2Code:<mvt:if expr="('custom' CIN g.myVariable) EQ 1">
Thanks -- Kent
* I just realized: I wrote my first code in high school, in 1970. I'm only about a year from the 50th anniversary! I still have a paper-tape copy of one of the programs I wrote. I wonder if I can get an old Teletype 33 somewhere ... :^)
Leave a comment:
-
The technique depends on what you are actually looking for. If, it's your example, then this works
<mvt:assign name="g.checkThis" value="tolower(substring(l.settings:product:code, 1, 7))" />
<mvt:if expr="g.checkThis EQ 'custom'">
yes, this product code starts with custom
</mvt:if>
(Note, I always condition (tolower) inputs that are human entered because you cannot assume humans will be consistent and product codes are not case sensitive. Using tolower() is faster than having miva use CIN, and EQ again is faster than either CIN or IN).
Leave a comment:
-
Thanks Colin, that's information overload to me.Originally posted by dreamingdigital View Posthttp://www.mivascript.com/item/strings/indexof.html does that help?
Leave a comment:
-
Leave a comment: