How to convert "Shell.Params()" to a ChaiScript "Map" object?

The ModScript docs for the Dictionary object say:

A class for supporting dictionary objects in scripts that are converted from SBM AppScript to SBM ModScript. A dictionary is a case sensitive key-value container.  In general, use ChaiScript's Map instead.

So the guidance, which seems reasonable, indicates that I need to convert Shell.Params() directly to a Map.  A Map constructor that did this would be nice.  No such beast in this zoo.  I think using algorithms and lambdas might be able to accomplish this easily, but I can't understand those at this point, and the examples of algorithms/lambdas are ..... limited.  I know the bigger hammer approach is a "for" loop but I'm really trying to drink the ChaiScript koolaid.  I've studied Don's Modscript series but get derailed by lambdas.

Is using algorithm + lambda the right approach for a "Shell.Params() - to - Map" converter??

  • Suggested Answer

    0  

    You are getting deep now, Paul Slight smile. I haven't used Map yet, and I looked through a few examples I have and none of them use Map either. 

    Have you tried using the converter tool? Converting SBM AppScripts to SBM ModScript - Solutions Business Manager (microfocus.com)  I wonder what it would do if you made a small script that included Shell.Params() and then used the converter tool. Maybe it would convert this to a Map. That would give an example as a starting point. At least it's an idea.

  • Verified Answer

    +1

    Here's the code I came up with.  It's a "bigger hammer" loop approach instead of trying to understand lambdas.  This code creates both ModScript "Dictionary" and "Map" versions of the contents of "Shell.Params".  In the long run, the ModScript "Dictionary" is just for testing and verification purposes.

    // This is a ModScript Dictionary object -- Not a VBScript Dictionary.
    // It will contain the keys and values from Shell.Params
    var dict_Params = Dictionary() ;

    // This will be populated with ChaiScript "string" values of Shell.Params.
    var map_Params = Map() ;

    // If Params is available in this context and it isn't empty, do our stuff.
    if (Ext.ShellHasProp("Params") && !Shell.Params().empty()) {

       // Shell.Params() is a 'chai:Variant / VBScript VarType:9 (Automation object) / Object / Dictionary'
       // Dictionary( Shell.Params() ) is a 'chai:Dictionary'
       dict_Params = Dictionary(Shell.Params())

       // Update our Dictionary version of Shell.Params with the "string" versions of the keys and values from Shell.Parms.
       // This is just a temporary debug aid.
       // Also update our "Map" version of Shell.Params with string keys and values.
       for ( ky : Shell.Params() ) {

          var str_key = ky.first().to_string().toLowerASCII().to_string() ;
          var str_val = ky.second().to_string() ;

          // Add additional keys to our Dictionary object using the same text
          //   value as the original key. This
          //   essentially doubles the number of entries. After this loop,
          //   "dict_Params" will contain both ChaiScript "string" versions of the
          //   params plus the original ChaiScript "Variant / VBScript String"
          //   versions. This will allow matching on "string" keys or
          //   Variant/String keys.
          dict_Params[str_key] = str_val;

          // Add a pair to our Map. Use lower-cased versions of the keys.
          map_Params.insert([str_key:str_val]);
       }
    }

  • Suggested Answer

    0 in reply to 

    Here's the short version:

    // for ( ky : Shell.Params() ) { // This converts the Variant / VBScript Dictionary to a ChaiScript Dictionary.
    // ky is a 'chai:DictionaryPair'
    // ky.first() is a 'chai:class CUString'
    // ky.second() is a 'chai:Variant / VBScript VarType:8 (String) / String'
    // default value of ky is 'ky.first()'
    // }

    var map_shell_params = Map() ;

    for ( ky : Shell.Params() ) {
       map_shell_params.insert( [ ky.to_string().toLowerASCII().to_string() : ky.second().to_string() ] );
    }

  • 0   in reply to 

    Nice! Short and sweet.