Returning 2d ScriptObject array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • randallc
    New Member
    • Sep 2007
    • 11

    Returning 2d ScriptObject array

    Hi,
    I need to take an input of 1D array, each item pipe delimited, and make a 2D array for column sorting.. Can anyone please help as my first ady with jscript, and haven't a clue why i get this error; I thought I was using other scripts exactly?!...
    Code:
    var list = [
    "1|359|No a Test xxxxxxxxxxxxxxx|0|08/18/2003|Approved",
    "2|268|test character fields for 500|129|07/14/2003|Approved",
    "3|288|textarea filled XXXXXXXx|0|07/15/2003|Approved"];
    
    
    WScript.Echo(list.join("\n"));
    WScript.Echo(list[0]);
    //~ myarray.sort(mysortfn);
    ListSorted=SortArray(list);
    WScript.Echo(ListSorted.join("\n"));
    //~ ;==========================================
    //~ ;==========================================
    function SortArray(arrArray) {
    	WScript.Echo(arrArray[0]);
    	var tempArray = arrArray[0].split('|');
    	WScript.Echo(tempArray[0]);
    	var NewArray  = new Array();
     //~ var square = new Array();
    	var count;
    	for (count = 0; count < tempArray.length; count++) ;{
    	 NewArray[count] = new Array();  // This declares each column in turn
    	}
    //~ var NewArray[arrArray.length][tempArray.length];
    	for (var i = 0; i < arrArray.length; i++) {
    		var tempArray = arrArray[i].split('|');
    		WScript.Echo(tempArray[0]);
    		for (var j = 0; j < tempArray.length; j++) {
    			NewArray[i][j]=tempArray[j]++;
    		}
    	}
    WScript.Echo(NewArray.join("\n"));
      
    //~ ;==========================================
    	NewArray.sort(value);
    	return NewArray;
    	//~ return arrArray[2][1];
    }
    function value(a,b) {
       a = a[1]+a[2];
       b = b[1]+b[2];
    return a == b ? 0 : (a < b ? -1 : 1);
    }
    >cscript /nologo sortvbsjs.js
    1|359|No a Test xxxxxxxxxxxxxxx |0|08/18/2003|Approved
    2|268|test character fields for 500|129|07/14/2003|Approved
    3|288|textarea filled XXXXXXXx|0|07/15/2003|Approved
    1|359|No a Test xxxxxxxxxxxxxxx |0|08/18/2003|Approved
    1|359|No a Test xxxxxxxxxxxxxxx |0|08/18/2003|Approved
    1
    1
    C:\Programs\Sea rchEngine\sortv bsjs.js(29, 4) Microsoft JScript runtime error: Object expected
    Any idea why?; or is there a better way anyway (in ScriptObject, I can't seem to pass 2D arrays... so need to pass 1D.)best, Randall
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    This is Javascript right? Javascript and Java (the forum in which you posted your
    question) have nothing to do with each other. Do you want me to move your question
    to the correct forum?

    kind regards,

    Jos

    Comment

    • randallc
      New Member
      • Sep 2007
      • 11

      #3
      yes, thanks;
      Sorry, lost track of the forums; I'll try to get the right one..
      Best, Randalll

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by randallc
        yes, thanks;
        Sorry, lost track of the forums; I'll try to get the right one..
        Best, Randalll
        You've just been beamed up Scotty ;-)

        kind regards,

        Jos

        Comment

        • randallc
          New Member
          • Sep 2007
          • 11

          #5
          Returning 2d ScriptObject array

          Hi,
          Complete noob to jscript, sorry for adding to wrong forum before..
          It seems to me that I can return a 1D array if I call a func in scriptcontrol for lang java, or an item of 2D array, but apparently not the 2D array, that I can see... eg back to vbscript.
          Is that expected?
          Best, randall

          [Code=javascript]function SortArray(arrAr ray) {
          var list = [
          ["1","359"," No a Test xxxxxxxxxxxxxxx ","0","08/18/2003","Approved "],
          ["2","268"," test character fields for 500","129","07/14/2003","Approved "],
          ["3","288","text area filled XXXXXXXx","0"," 07/15/2003","Approved "]];
          list.sort(value );
          return list[2][1];
          }
          function value(a,b) {
          a = a[3]+a[1];
          b = b[3]+b[1];
          return a == b ? 0 : (a < b ? -1 : 1);
          }[/code]

          So "return list[2][1];" works,
          but not "return list;"?
          Last edited by pbmods; Sep 12 '07, 11:08 PM. Reason: Changed [CODE] to [CODE=javascript].

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, Randall.

            What do you want your code to do? Give an example.
            What is your code doing that you don't want it to do? Give an example.
            What is your code *not* doing that it is supposed to? Give an example.

            Comment

            • JeremyMiller
              New Member
              • Sep 2007
              • 69

              #7
              I through this together fairly quickly. I think it addresses your needs. Tested in FF and IE 7

              [code=html]
              <html>
              <head>
              <script type="text/javascript" >
              function SortArray(arrAr ray) {
              copyOfArray = arrArray.slice( 0);
              copyOfArray.sor t(value);
              return copyOfArray;
              }
              function value(a,b) {
              a = a[3]+a[1];
              b = b[3]+b[1];
              return a == b ? 0 : (a < b ? -1 : 1);
              }
              var list = [
              ["1","359"," No a Test xxxxxxxxxxxxxxx ","0","08/18/2003","Approved "],
              ["2","268"," test character fields for 500","129","07/14/2003","Approved "],
              ["3","288","text area filled XXXXXXXx","0"," 07/15/2003","Approved "]];
              </script>
              </head>
              <body>
              <script type="text/javascript" >
              sorted_list = SortArray(list) ;

              document.write( "Before Sort<br />");
              document.write( list[0][0]+"<br />");
              document.write( list[1][0]+"<br />");
              document.write( list[2][0]+"<br />");

              document.write( "After Sort<br />");
              document.write( sorted_list[0][0]+"<br />");
              document.write( sorted_list[1][0]+"<br />");
              document.write( sorted_list[2][0]+"<br />");
              </script>
              </body>
              </html>
              [/code]

              Comment

              • randallc
                New Member
                • Sep 2007
                • 11

                #8
                Originally posted by JeremyMiller
                I through this together fairly quickly. [/code]
                Thanks so much for looking at the code; as I said, though, the code was working OK...
                The question is this..
                1. When I call this code in a "ScriptCont rol" as an object, from another program altogether, ScriptControl does not return a 2D array; I have looked further; it seems only a string will be retruned (This is not a problem, say, in using vbscript... within a ScriptConrol object).
                I have started looking into jscript itself; it looks as though a 2D array is not just 1 object, but an array of arrays or objects?) - so I gues it makes sense it could not return one object; although it doesn't explain why it coul dnot return a 1D array, I suppose... I'll have to look further.
                My question was "whether the behaviour of "scriptCont rol" returning no array was expected...
                Best, randall

                Comment

                • randallc
                  New Member
                  • Sep 2007
                  • 11

                  #9
                  Originally posted by JosAH
                  You've just been beamed up Scotty ;-)Jos
                  OK, thanks; I presume this is correct forum now as it was moved; can anyone help? i see that jscript 2D arrays are "arrays of arrays"; so maybe that willl help me; but I thouhght I copied most of this from someone's working script! Why still that error "no object" etc?
                  Best, Randall

                  Comment

                  • JeremyMiller
                    New Member
                    • Sep 2007
                    • 69

                    #10
                    hmm...Not too sure what you mean by calling it from another program altogether. Can you provide some code which doesn't work as intended?

                    And, I had a good laugh at myself -- should have said "threw", not "through". lol.

                    Off to bed for tonight, though. I'll check again in the morning for your sample code.

                    Comment

                    • pbmods
                      Recognized Expert Expert
                      • Apr 2007
                      • 5821

                      #11
                      Heya, Randall.

                      So what you're trying to do is pass a VB object from JavaScript to VB. Is this correct?

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Threads merged.

                        Comment

                        • randallc
                          New Member
                          • Sep 2007
                          • 11

                          #13
                          Thanks;
                          I can see by this exercise that what I don't understand is the nature of the return from "Array.sort "?
                          Code:
                          code = code& vblf & "	        return arrArray.toArray().sort();"
                          works n ""JSort1D"
                          Code:
                          code = code& vblf & "	        return arrArray;"
                          works in "JSort1Da"
                          but
                          Code:
                          code = code& vblf & "	        return arrArray.sort();"
                          does not work in "JSort1Db"
                          Best, Randall
                          Code:
                          ' sortvbsjs2.vbs
                          dim ArListCopy:  list = Split("1,9,2,7,4,9,4,8", ",")
                          Dim objFSO: Set objFSO = CreateObject( "Scripting.FileSystemObject" )
                          Dim objOutput: Set objOutput = objFSO.CreateTextFile( "C:\Programs\SearchEngine\sort.txt", True )
                          ArListCopy=ArrayCopy( list)
                          ListSorted=JSort1D(ArListCopy)
                          ArrayDisplay1 ListSorted,"ListSorted After sort "
                          ArListCopy=ArrayCopy( list)
                          ListvbsChange=VbSort1D(ArListCopy)
                          ArrayDisplay1 ListvbsChange,"ListvbsChange "
                          ArListCopy=ArrayCopy( list)
                          ListJS=JSort1Da(ArListCopy)
                          ArrayDisplay1 ListJS,"ListJS"
                          ArListCopy=ArrayCopy( list)
                          ListSorted=JSort1Db(ArListCopy)
                          ArrayDisplay1 ListSorted,"ListSorted After sort"
                          
                          function JSort1D(ByRef arEither2D1D)
                          	dim code: code = ""
                          	code = code&"   function SortArray(arrArray) {"
                          	code = code& vblf & "	        return arrArray.toArray().sort();"
                          	code = code& vblf & "	    }"
                          	Dim jvs: Set jvs = CreateObject( "ScriptControl" )
                          	jvs.language = "jscript"
                          	jvs.Timeout = -1
                          	jvs.addcode (code)
                          	dim arEither2D1DSt: arEither2D1DSt = jvs.Run("SortArray", arEither2D1D)
                          	JSort1D = Split(arEither2D1DSt, ",")
                          	jvs = ""
                          End function   
                          function VbSort1D(ByRef arEither2D1D)
                          	dim code: code = ""
                          	code = code&"   function SortArray(arrArray) "
                          	code = code& vblf & "	        arrArray(0)= 20"
                          	code = code& vblf & "	        SortArray= arrArray"
                          	code = code& vblf & "end function"
                          	Dim jvs: Set vbs= CreateObject( "ScriptControl" )
                          	vbs.language = "vbscript"
                          	vbs.Timeout = -1
                          	vbs.addcode (code)
                          	VbSort1D = vbs.Run("SortArray", arEither2D1D)
                          	vbs= ""
                          End function   
                          function JSort1Da(ByRef arEither2D1D)
                          	dim arEither2D1DSt,code: code = ""
                          	code = code&"   function SortArray(arrArray) {"
                          	code = code& vblf & "	        return arrArray;"
                          	code = code& vblf & "}"
                          	Dim jvs: Set jvs = CreateObject( "ScriptControl" )
                          	jvs.language = "jscript"
                          	jvs.Timeout = -1
                          	jvs.addcode (code)
                          	arEither2D1DSt = jvs.Run("SortArray", arEither2D1D)
                          	JSort1Da=arEither2D1DSt 
                          	jvs = ""
                          End function   
                          function JSort1Db(ByRef arEither2D1D)
                          	dim arEither2D1DSt,code: code = ""
                          	code = code&"   function SortArray(arrArray) {"
                          	code = code& vblf & "	        return arrArray.sort();"
                          	code = code& vblf & "}"
                          	Dim jvs: Set jvs = CreateObject( "ScriptControl" )
                          	jvs.language = "jscript"
                          	jvs.Timeout = -1
                          	jvs.addcode (code)
                          	arEither2D1DSt = jvs.Run("SortArray", arEither2D1D)
                          	JSort1Db=arEither2D1DSt 
                          	jvs = ""
                          End function   
                          ' ;==========================================
                          ' ;==========================================
                          function ArrayDisplay1(byref arraylist,Title)
                          	dim i,stringlist:stringlist=ucase(Title)&vbCrLf&vbCrLf
                          	for i = 0 to ubound(arraylist)
                          		stringlist=stringlist&"arraylistview("&i&")="&arraylist(i)&vbCrLf
                          	next
                          	wscript.echo stringlist
                          End function   
                          function ArrayCopy( arraylist)
                          	dim i,ArrayCopy1()
                          	redim preserve ArrayCopy1(ubound(arraylist))
                          	for i = 0 to ubound(arraylist)
                          		ArrayCopy1(i)=arraylist(i)
                          	next
                          	ArrayCopy=ArrayCopy1
                          End function

                          Comment

                          • randallc
                            New Member
                            • Sep 2007
                            • 11

                            #14
                            Originally posted by pbmods
                            Heya, Randall.

                            So what you're trying to do is pass a VB object from JavaScript to VB. Is this correct?
                            Hi,
                            In the example, that is so...
                            But I am actually just giving an example; for whatever program runs jscript in the scripting object; and looking at the return from that ScriptObject... . not necessarily to vbscript; only used as example.. [see above]
                            Thanks, Randall

                            So which of
                            1. arrArray.toArra y().sort()
                            2. arrArray
                            3. arrArray.sort()
                            is an array, which is a string, which is an object?
                            Aren't they all objects when returned through the return of script object?


                            =============== =============== =============== ====

                            PS2 I can't follow here; "merged" threads; the other question is how to slit the arrays in first post... just a straight jscript question; I guess 'll just have to learn more jscript than I had wanted to bother with!
                            My error in the first post occurs with the line;
                            [CODE=javascript] NewArray[i][j]=tempArray[j]++;[/CODE] or
                            [CODE=javascript] NewArray[i][j]=tempArray[j];[/CODE] , so I think my declaration of the arrays or sub-arrays is incorrect?....

                            Comment

                            • randallc
                              New Member
                              • Sep 2007
                              • 11

                              #15
                              Hi,
                              i can't edit this; my addendum problem is solved; I see I was getting array length wrong at declaration; but I still need to understand the other problem?
                              thanks, randall

                              Comment

                              Working...