Compare a String to an array of Strings.

V3DigitimesV3Digitimes Posts: 3,150
edited December 1969 in Daz Script Developer Discussion

Hello,
I'm trying to create a function in order to modify a few parameters of surface properties on only surface names which would be picked up from an array of surface names.
I could write down all the "cases" of sID, but it would then be longer to create all the presets I want to do, and less flexible for further development.

I tried to use the "indexOf" function (I caught on the web) in order to :
1. compare the sID string to the ones of an array which would contain the names of all the surface I want to change and return -1 if they are not in the array.
2. change the parameters of sID in the case it is in the array ( >-1 ).

For this I worked on a simple example, saved the material once as dsa, and modified just the end of the file :
before the "DsActions.prototype.setMaterialProperties" I added the "array.prototype.indexOf" function. Then I added the surface list as an array of strings in "DsActions.prototype.setMaterialProperties", and I tried to use it in a "if" function.
Now when I run the dsa of my object, I have the following error :
//WARNING: TypeError: Result of expression 'Array.indexOf' [undefined] is not a function.
//WARNING: Stack Trace:

I do not know where it comes from. Is it because I'm using strings in the array? Am I supposed to declare the function elsewhere in the dsa file? Is there something even more obvious I have not made or seen?

I have no idea. And you'll surely see I have only a poor idea of how all this is working..

/*********************************************************************/


    Array.prototype.indexOf = function (searchElement, fromIndex) {
      if ( this === undefined || this === null ) {
        throw new TypeError( '"this" is null or not defined' );
      }

      var length = this.length >>> 0; // Hack to convert object.length to a UInt32

      fromIndex = +fromIndex || 0;

      if (Math.abs(fromIndex) === Infinity) {
        fromIndex = 0;
      }

      if (fromIndex < 0) {
        fromIndex += length;
        if (fromIndex < 0) {
          fromIndex = 0;
        }
      }

      for (;fromIndex < length; fromIndex++) {
        if (this[fromIndex] === searchElement) {
          return fromIndex;
        }
      }

      return -1;
    }


//////////////////////////////////////////////


DsActions.prototype.setMaterialProperties = function( oMaterial, oShape ){
 this.m_oElement = oMaterial;
 var sID = this.m_oElement.name;
 var SURFLIST = ["surfacename1", "surfacename2" ];
 
 if( oShape != undefined ){
  sID += "_" + oShape.getLabel();
 }
 //for( var i = 0; i < this.m_oElement.length; i++ ){
  for( var i = 0; i < 10; i++ ){
   intermvar=Array.indexOf( sID , SURFLIST ) ;
   
 if( intermvar > -1 ){
     
   //case "surface1":
    this.m_sMaterialType = "DzBrickMaterial";
    this.m_sMaterialName = "V3D_MyShader";
    this.prepareMaterial();
    g_oPresetHelper.setTargetElement( this.m_oElement );
    g_oPresetHelper.setNumericPropertyWithAttributes( "value1", true, 0, 100, [ 0 ] );
    g_oPresetHelper.setNumericPropertyWithAttributes( "value2", true, 0, 10, [ 0 ] );
    g_oPresetHelper.setNumericPropertyWithAttributes( "value3", true, 0, 10, [ 0 ] );
    break;
   
  //default:
   //break;
 }
}
}

g_bCONTROL_PRESSED ? g_oGui.doDialog() : g_oGui.doNoDialog();



//WARNING: TypeError: Result of expression 'Array.indexOf' [undefined] is not a function.
//WARNING: Stack Trace:

Comments

  • Richard HaseltineRichard Haseltine Posts: 100,926
    edited December 1969

    There's a bit too much cryptic code in there for me to follow in my current state of brain, but why are you trying to make the new function a method of array in the first place? The only things that matter are the string you are checking and the array of possible matches, and you are passing both of those as inputs.

  • V3DigitimesV3Digitimes Posts: 3,150
    edited December 1969

    There's a bit too much cryptic code in there for me to follow in my current state of brain, but why are you trying to make the new function a method of array in the first place? The only things that matter are the string you are checking and the array of possible matches, and you are passing both of those as inputs.

    Well, it's just because it is my first hours coding, and I'm lost : lost in the language itself, and as a consequence in the structure, and in what it can and what cannot do.
    But I'm the kind of people which learns by failing, during months if necessary.. So the more I fail, the more I learn.
    I'm gonna have a look at what you propose here. Thanks.

  • rbtwhizrbtwhiz Posts: 2,250
    edited December 1969

    Array already has an indexOf method.

    // Define the list of names you care about
    var aSurfaceNames = ["surfacename1", "surfacename2", "surfacename3", "surfacename4"];
    
    // Define the name you want to check
    var sFind = "surfacename3";
    
    // Check if the name is in the list
    var nIndex = aSurfaceNames.indexOf( sFind );
    // If the name is in the list
    if( nIndex > -1 ){
     // Tell me where
     print( sFind, "@", nIndex );
    // If the name is not in the list
    } else {
     // Tell me it wasn't
     print( sFind, "not found" );
    }

    That aside... In your sample, you're using the function as if it is static... but it isn't. You would need to call it on an instance of an Array object, like in the sample above.

    -Rob

  • V3DigitimesV3Digitimes Posts: 3,150
    edited December 1969

    rbtwhiz said:
    Array already has an indexOf method.

    // Define the list of names you care about
    var aSurfaceNames = ["surfacename1", "surfacename2", "surfacename3", "surfacename4"];
    
    // Define the name you want to check
    var sFind = "surfacename3";
    
    // Check if the name is in the list
    var nIndex = aSurfaceNames.indexOf( sFind );
    // If the name is in the list
    if( nIndex > -1 ){
     // Tell me where
     print( sFind, "@", nIndex );
    // If the name is not in the list
    } else {
     // Tell me it wasn't
     print( sFind, "not found" );
    }

    That aside... In your sample, you're using the function as if it is static... but it isn't. You would need to call it on an instance of an Array object, like in the sample above.

    -Rob

    That simple!?
    You could even not guess how many complex things I made and which failed!!
    Thank you Rob.
    I have spent my day reading thing about ECMA code, as well as some topics and examples here, and everything is well mixed and crushed in my head tonight. I'm gonna have a sleep and tomorrow morning I'll have a look at 'static and dynamic' functions', 'call on an instance of an Array object'... and things like that.
    Good news : I've just discovered the "print" this afternoon, and this is going to help me a lot : before I was totally blind and ignorant....
    Now I'm just ignorant : this is much better ;)


    ********
    revelation time : ON
    Oh!? Oh? Wow?!
    ********
    Oh!!!! I'm just understanding something re-reading your code... when you write "aSurfaceNames", the "a" stands for array, "nIndex", "n" must stand for integer," sFind" "s" stands for string.... so everything beginning with "o" must stand for.. object... I was wondering why I saw so many "o" in the code... Well I just gotta understand what is an object now..
    *******
    revelation time : OFF
    Enter Sleepmode. Pause (12hours).
    *******

  • rbtwhizrbtwhiz Posts: 2,250
    edited December 1969

    Yes, I use a form of Hungarian Notation on variable and parameter identifiers to make reading the code much more efficient; the prefix provides a hint to the intended type of a parameter/variable in a weakly typed language. This and other code stylings that are used internally, for scripts, is posted for the benefit of anyone who cares to adopt them.

    -Rob

  • V3DigitimesV3Digitimes Posts: 3,150
    edited December 1969

    rbtwhiz said:
    Yes, I use a form of Hungarian Notation on variable and parameter identifiers to make reading the code much more efficient; the prefix provides a hint to the intended type of a parameter/variable in a weakly typed language. This and other code stylings that are used internally, for scripts, is posted for the benefit of anyone who cares to adopt them.

    -Rob


    They are already adopted, and it is indeed much more readable! Those little things making your life more simple ;)
Sign In or Register to comment.