Conditional based on partial product code

Collapse
X
 
  • Time
  • Show
Clear All
new posts

  • lesliekirk
    replied
    Thanks, everyone. File this for future reference.

    Leave a comment:


  • Kent Multer
    replied
    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:


  • lesliekirk
    replied
    So while goofing around I came up with this

    Code:
    <mvt:if expr="'custom' CIN l.settings:product:code">
    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?

    Leave a comment:


  • Kent Multer
    replied
    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:


  • lesliekirk
    replied
    Okay, so now back to my original question

    if I use this

    Code:
     
     <mvt:if expr="('custom' CIN g.myVariable) EQ 1">
    is there a mvt:assign that goes before this?

    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:


  • lesliekirk
    replied
    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
    Code:
    <mvt:if expr="('custom' CIN g.myVariable) EQ 1">
    -- which does use CIN, but has zero function calls instead of 2

    Thanks -- Kent
    Not off topic - this help me and probably many, many others learn.

    Leave a comment:


  • Kent Multer
    replied
    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:


  • Bruce - PhosphorMedia
    replied
    Originally posted by Kent Multer View Post
    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.
    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.

    Leave a comment:


  • ids
    replied
    LOL, according to a certain developer's book on page 35, you would be correct Kent.

    Scott

    Leave a comment:


  • Kent Multer
    replied
    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:


  • Bruce - PhosphorMedia
    replied
    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:


  • Kent Multer
    replied
    Originally posted by Bruce - PhosphorMedia View Post
    Using tolower() is faster than having miva use CIN, and EQ again is faster than either CIN or IN).
    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
    Code:
    <mvt:if expr="('custom' CIN g.myVariable) EQ 1">
    -- which does use CIN, but has zero function calls instead of 2

    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 ... :^)
    Last edited by Kent Multer; 12-11-18, 10:15 AM. Reason: error on date

    Leave a comment:


  • Bruce - PhosphorMedia
    replied
    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:


  • lesliekirk
    replied
    Originally posted by dreamingdigital View Post
    Thanks Colin, that's information overload to me.

    Leave a comment:


  • dreamingdigital
    replied
    http://www.mivascript.com/item/strings/indexof.html does that help?

    Leave a comment:

Working...
X