Want to get rid of Google Ads, click here.
+ Reply to Thread
Results 1 to 7 of 7

Thread: Anybody know if and/or how I can compare (starts with) to an array using javascript?

  1. #1
    Junior Member
    Join Date
    Apr 2008
    Location
    Ohio
    Posts
    11

    Default Anybody know if and/or how I can compare (starts with) to an array using javascript?

    I have a script in SC that evaluates the following:

    field in $file # array in $file

    if the value of field starts with any value in the array SC returns true.

    i.e "MMX" # {"MM", "NN", "PP"} will return true.

    I am trying to do the same in a javascript (in SM7.11) but have not been successful. Javascript does not appear to support the startswith functionality when comparing to an array.

    To complicate it a bit more the SC code actually is building the expression and then does:

    return_variable=evaluate(parse("expression to be evaluated", 4))=true

    Can anybody help?

  2. #2
    Senior Member
    Join Date
    Apr 2006
    Location
    United States
    Posts
    178

    Default

    //below is a function that would work and calling it example
    var
    newarr=newSCDatum();

    newarr.setType
    (8);

    newarr.push
    ("MM");

    newarr.push
    ("NN");

    newarr.push
    ("PP");

    var
    newfield="MMX";

    print
    (ValueStartsWith(newfield,newarr));


    function
    ValueStartsWith(field, arr)

    {
    for (x in arr)
    if (field.indexOf(arr[x])==0)
    returntrue;

    returnfalse;

    }

  3. #3
    Junior Member
    Join Date
    Apr 2008
    Location
    Ohio
    Posts
    11

    Default

    jochocki,
    Thanks for that. However my issue is a bit more complex. I have a control table that contains an array of structures. There are 3 fields in the structure (field_name, operator, array of values). So for example the elements of the structured array may be:
    struct 1: category, Equal, {"HW", "SW", "TELE"}
    struct 2: subcategory, Starts With, {"ABC", "DEF", "GHI"}

    What I want to do is loop through the structure and evaluate if the field in the file passed in equal to (for the first struct) and starts with (for the second struct) and if so to return a true (if both are true) or a false if (either is false). I somehow need to parse and evaluate to build the expression. Here is how I do that in SC:

    The arrayed structure name is "definitions" and $script variable contains a probsummary record. $L.acntr is the position of the arrayed structure as I loop through it. The $L.aoper variable is set to "=" or "#" depending on the literal in the second field.

    $L.match=evaluate(parse("toupper("+1 in $L.acntr in definitions in $L.aafile+" in $script)"+$L.aoper+str(3 in $L.acntr in definitions in $L.aafile), 4))=true

    The result is:
    $L.match=cateogry in $script={"HW", "SW", "TELE"}=true (for the first one)
    $L.match=subcateogry in $script#{"ABC", "DEF", "GHI"}=true (for the second one)

    Any insight would be greatly appreciated.

    What I ne

  4. #4
    Junior Member
    Join Date
    Apr 2008
    Location
    Ohio
    Posts
    11

    Default

    Jochocki,
    My post was truncated:
    What I need is to be able to loop trough the definitions (arrayed structure) and compare the value of ((1st field in x of definitions) in $script) to the array of values in the third field of definitions using either equal to or starts with.

    Hope this makes sense.

    Thanks in advance.

    Steve Woulf

  5. #5
    Senior Member
    Join Date
    Apr 2006
    Location
    United States
    Posts
    178

    Default

    works very similar, you will just need nested for loops. to access something in a struct:
    record.structname[x].structfield
    or
    record.structname[x].structfield[y]
    for your array within the struct

    to evaluate the fieldname in the main record:
    eval("thefile."+record.structname[x].structfield)

    Depending on your version, building JS code to work with structured arrays can be very temperamental

  6. #6
    Senior Member
    Join Date
    Apr 2006
    Location
    United States
    Posts
    178

    Default

    feel free to contact me privately if you need the work done...it would take a couple hours to do.

  7. #7
    Senior Member mateuszk's Avatar
    Join Date
    Nov 2006
    Posts
    302

    Default

    Quote Originally Posted by Woulf View Post
    $L.match=evaluate(parse("toupper("+1 in $L.acntr in definitions in $L.aafile+" in $script)"+$L.aoper+str(3 in $L.acntr in definitions in $L.aafile), 4))=true

    The result is:
    $L.match=cateogry in $script={"HW", "SW", "TELE"}=true (for the first one)
    $L.match=subcateogry in $script#{"ABC", "DEF", "GHI"}=true (for the second one)
    Hi there, this sounds like an easy job for RegExp.

    PHP Code:
    /*
    @txt {String} - text to compare
    @arrIn {Array} - an array of strings to compare to
    @equal {Boolean} - "is exactly" - true, "starts with" - false
    */
    regTest = function(txt,arrIn,equal) {
         
    /*
         * "starts with" is ^
         * join the array with | chars, the | is 'or'
         */
         
    arrInJoin '^'+arrIn.join('|');
         
    /* if "needs to be equal" 
         * the string has to match from the start '^' to finish '$' 
         */
        
    if(equalarrInJoin+='$';
         return 
    RegExp(arrInJoin,'ig').test(txt); 

    Of course, this is not a full solution as you have the data in an structured array. Well, let's try ...

    PHP Code:
    /* simulate the SCFile array of structure */
    function CatDefs() {
     
    this.definitions = [];
     
    this.cats = [];
     
    this.subcats = [];
     
     
    this.regTest = function(txt,arrIn,equal) {
         
    arrInJoin '^'+arrIn.join('|');
         if(
    equalarrInJoin+='$';
         return 
    RegExp(arrInJoin,'ig').test(txt); 
     }
      
    /* fill the "definitions" field */
     
    this.init = function() {
      for(
    i=0;i<4;i++) {
       
    this.definitions.push(['cat'+i,'empty','subcat'+i]);
      }
      
      
    /* here's the trick, iterate through definitions
      *  get the correct data out to "array of char" type
      *  you can always put that in a "reusable", thread array 
      *  so that it happens only once */
      
    for(a in this.definitions) {
       
    this.cats.push(this.definitions[a][0]);
       
    this.subcats.push(this.definitions[a][2]);
      }
     }
    }

    var 
    cDefs=new CatDefs();
    cDefs.init();

    print( 
    cDefs.regTest('cat1',cDefs.cats,true) )
    print( 
    cDefs.regTest('subcat1',cDefs.subcats,false) ) 
    Hope this helps a bit. RegExp is fun!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Tags for this Thread

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts