473,395 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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?!...
Expand|Select|Wrap|Line Numbers
  1. var list = [
  2. "1|359|No a Test xxxxxxxxxxxxxxx|0|08/18/2003|Approved",
  3. "2|268|test character fields for 500|129|07/14/2003|Approved",
  4. "3|288|textarea filled XXXXXXXx|0|07/15/2003|Approved"];
  5.  
  6.  
  7. WScript.Echo(list.join("\n"));
  8. WScript.Echo(list[0]);
  9. //~ myarray.sort(mysortfn);
  10. ListSorted=SortArray(list);
  11. WScript.Echo(ListSorted.join("\n"));
  12. //~ ;==========================================
  13. //~ ;==========================================
  14. function SortArray(arrArray) {
  15.     WScript.Echo(arrArray[0]);
  16.     var tempArray = arrArray[0].split('|');
  17.     WScript.Echo(tempArray[0]);
  18.     var NewArray  = new Array();
  19.  //~ var square = new Array();
  20.     var count;
  21.     for (count = 0; count < tempArray.length; count++) ;{
  22.      NewArray[count] = new Array();  // This declares each column in turn
  23.     }
  24. //~ var NewArray[arrArray.length][tempArray.length];
  25.     for (var i = 0; i < arrArray.length; i++) {
  26.         var tempArray = arrArray[i].split('|');
  27.         WScript.Echo(tempArray[0]);
  28.         for (var j = 0; j < tempArray.length; j++) {
  29.             NewArray[i][j]=tempArray[j]++;
  30.         }
  31.     }
  32. WScript.Echo(NewArray.join("\n"));
  33.  
  34. //~ ;==========================================
  35.     NewArray.sort(value);
  36.     return NewArray;
  37.     //~ return arrArray[2][1];
  38. }
  39. function value(a,b) {
  40.    a = a[1]+a[2];
  41.    b = b[1]+b[2];
  42. return a == b ? 0 : (a < b ? -1 : 1);
  43. }
>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\SearchEngine\sortvbsjs.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
Sep 12 '07 #1
16 2534
JosAH
11,448 Expert 8TB
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
Sep 12 '07 #2
yes, thanks;
Sorry, lost track of the forums; I'll try to get the right one..
Best, Randalll
Sep 12 '07 #3
JosAH
11,448 Expert 8TB
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
Sep 12 '07 #4
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

Expand|Select|Wrap|Line Numbers
  1. function SortArray(arrArray) {
  2. var list = [
  3. ["1","359","No a Test xxxxxxxxxxxxxxx","0","08/18/2003","Approved"],
  4. ["2","268","test character fields for 500","129","07/14/2003","Approved"],
  5. ["3","288","textarea filled XXXXXXXx","0","07/15/2003","Approved"]];
  6.      list.sort(value);
  7.     return list[2][1];
  8.   }
  9.   function value(a,b) {
  10.        a = a[3]+a[1];
  11.        b = b[3]+b[1];
  12.   return a == b ? 0 : (a < b ? -1 : 1);
  13.         }
So "return list[2][1];" works,
but not "return list;"?
Sep 12 '07 #5
pbmods
5,821 Expert 4TB
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.
Sep 12 '07 #6
I through this together fairly quickly. I think it addresses your needs. Tested in FF and IE 7

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <head>
  3.     <script type="text/javascript" >
  4.       function SortArray(arrArray) {
  5.         copyOfArray = arrArray.slice(0);
  6.         copyOfArray.sort(value);
  7.         return copyOfArray;
  8.       }
  9.       function value(a,b) {
  10.         a = a[3]+a[1];
  11.         b = b[3]+b[1];
  12.         return a == b ? 0 : (a < b ? -1 : 1);
  13.       }
  14.       var list = [
  15.         ["1","359","No a Test xxxxxxxxxxxxxxx","0","08/18/2003","Approved"],
  16.         ["2","268","test character fields for 500","129","07/14/2003","Approved"],
  17.         ["3","288","textarea filled XXXXXXXx","0","07/15/2003","Approved"]];
  18.     </script>
  19.   </head>
  20.   <body> 
  21.   <script type="text/javascript" >
  22.   sorted_list = SortArray(list);
  23.  
  24.   document.write("Before Sort<br />");
  25.   document.write(list[0][0]+"<br />");
  26.   document.write(list[1][0]+"<br />");
  27.   document.write(list[2][0]+"<br />");  
  28.  
  29.   document.write("After Sort<br />");
  30.   document.write(sorted_list[0][0]+"<br />");
  31.   document.write(sorted_list[1][0]+"<br />");
  32.   document.write(sorted_list[2][0]+"<br />");
  33.   </script>
  34.   </body>
  35. </html>
  36.  
Sep 12 '07 #7
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 "ScriptControl" 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 "scriptControl" returning no array was expected...
Best, randall
Sep 13 '07 #8
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
Sep 13 '07 #9
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.
Sep 13 '07 #10
pbmods
5,821 Expert 4TB
Heya, Randall.

So what you're trying to do is pass a VB object from JavaScript to VB. Is this correct?
Sep 13 '07 #11
acoder
16,027 Expert Mod 8TB
Threads merged.
Sep 13 '07 #12
Thanks;
I can see by this exercise that what I don't understand is the nature of the return from "Array.sort"?
Expand|Select|Wrap|Line Numbers
  1. code = code& vblf & "            return arrArray.toArray().sort();"
works n ""JSort1D"
Expand|Select|Wrap|Line Numbers
  1. code = code& vblf & "            return arrArray;"
works in "JSort1Da"
but
Expand|Select|Wrap|Line Numbers
  1. code = code& vblf & "            return arrArray.sort();"
does not work in "JSort1Db"
Best, Randall
Expand|Select|Wrap|Line Numbers
  1. ' sortvbsjs2.vbs
  2. dim ArListCopy:  list = Split("1,9,2,7,4,9,4,8", ",")
  3. Dim objFSO: Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  4. Dim objOutput: Set objOutput = objFSO.CreateTextFile( "C:\Programs\SearchEngine\sort.txt", True )
  5. ArListCopy=ArrayCopy( list)
  6. ListSorted=JSort1D(ArListCopy)
  7. ArrayDisplay1 ListSorted,"ListSorted After sort "
  8. ArListCopy=ArrayCopy( list)
  9. ListvbsChange=VbSort1D(ArListCopy)
  10. ArrayDisplay1 ListvbsChange,"ListvbsChange "
  11. ArListCopy=ArrayCopy( list)
  12. ListJS=JSort1Da(ArListCopy)
  13. ArrayDisplay1 ListJS,"ListJS"
  14. ArListCopy=ArrayCopy( list)
  15. ListSorted=JSort1Db(ArListCopy)
  16. ArrayDisplay1 ListSorted,"ListSorted After sort"
  17.  
  18. function JSort1D(ByRef arEither2D1D)
  19.     dim code: code = ""
  20.     code = code&"   function SortArray(arrArray) {"
  21.     code = code& vblf & "            return arrArray.toArray().sort();"
  22.     code = code& vblf & "        }"
  23.     Dim jvs: Set jvs = CreateObject( "ScriptControl" )
  24.     jvs.language = "jscript"
  25.     jvs.Timeout = -1
  26.     jvs.addcode (code)
  27.     dim arEither2D1DSt: arEither2D1DSt = jvs.Run("SortArray", arEither2D1D)
  28.     JSort1D = Split(arEither2D1DSt, ",")
  29.     jvs = ""
  30. End function   
  31. function VbSort1D(ByRef arEither2D1D)
  32.     dim code: code = ""
  33.     code = code&"   function SortArray(arrArray) "
  34.     code = code& vblf & "            arrArray(0)= 20"
  35.     code = code& vblf & "            SortArray= arrArray"
  36.     code = code& vblf & "end function"
  37.     Dim jvs: Set vbs= CreateObject( "ScriptControl" )
  38.     vbs.language = "vbscript"
  39.     vbs.Timeout = -1
  40.     vbs.addcode (code)
  41.     VbSort1D = vbs.Run("SortArray", arEither2D1D)
  42.     vbs= ""
  43. End function   
  44. function JSort1Da(ByRef arEither2D1D)
  45.     dim arEither2D1DSt,code: code = ""
  46.     code = code&"   function SortArray(arrArray) {"
  47.     code = code& vblf & "            return arrArray;"
  48.     code = code& vblf & "}"
  49.     Dim jvs: Set jvs = CreateObject( "ScriptControl" )
  50.     jvs.language = "jscript"
  51.     jvs.Timeout = -1
  52.     jvs.addcode (code)
  53.     arEither2D1DSt = jvs.Run("SortArray", arEither2D1D)
  54.     JSort1Da=arEither2D1DSt 
  55.     jvs = ""
  56. End function   
  57. function JSort1Db(ByRef arEither2D1D)
  58.     dim arEither2D1DSt,code: code = ""
  59.     code = code&"   function SortArray(arrArray) {"
  60.     code = code& vblf & "            return arrArray.sort();"
  61.     code = code& vblf & "}"
  62.     Dim jvs: Set jvs = CreateObject( "ScriptControl" )
  63.     jvs.language = "jscript"
  64.     jvs.Timeout = -1
  65.     jvs.addcode (code)
  66.     arEither2D1DSt = jvs.Run("SortArray", arEither2D1D)
  67.     JSort1Db=arEither2D1DSt 
  68.     jvs = ""
  69. End function   
  70. ' ;==========================================
  71. ' ;==========================================
  72. function ArrayDisplay1(byref arraylist,Title)
  73.     dim i,stringlist:stringlist=ucase(Title)&vbCrLf&vbCrLf
  74.     for i = 0 to ubound(arraylist)
  75.         stringlist=stringlist&"arraylistview("&i&")="&arraylist(i)&vbCrLf
  76.     next
  77.     wscript.echo stringlist
  78. End function   
  79. function ArrayCopy( arraylist)
  80.     dim i,ArrayCopy1()
  81.     redim preserve ArrayCopy1(ubound(arraylist))
  82.     for i = 0 to ubound(arraylist)
  83.         ArrayCopy1(i)=arraylist(i)
  84.     next
  85.     ArrayCopy=ArrayCopy1
  86. End function   
  87.  
Sep 13 '07 #13
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.toArray().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;
Expand|Select|Wrap|Line Numbers
  1.             NewArray[i][j]=tempArray[j]++;
or
Expand|Select|Wrap|Line Numbers
  1.             NewArray[i][j]=tempArray[j];
, so I think my declaration of the arrays or sub-arrays is incorrect?....
Sep 13 '07 #14
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
Sep 13 '07 #15
Hi again,
I think I know the answers to my questions;
scriptObject usually does not return anything other than strings except perhaps to vbscript...

And, in any case, sorting 2D arrays seems no quicker [slower?] than a quicksort in vbscript? [although 1D sorting inbuilt is probably 2x as fast?]
Any other opinions?
thanks, randall
Sep 13 '07 #16
Hi,
Thanks for those who answered my thread.
1. I am not sure why I needed Array.slice(0), as the sort works OK without it, just on original array.
2. Are you able to look at my test script, as suggested? (post #13 I think) . I think it shows how vbscript Array object passing seems to be different to jscript... but no-one has confirmed that. Do you have any thoughts on that?

I feel as though I am missing something here.. I shouldn't have to treat the Array object as a string to extract the 2D array?..
Best, randall
Sep 15 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
3
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
13
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns...
0
by: anuptosh | last post by:
Hi, I have been trying to run the below example to get a Oracle Array as an output from a Java code. This is an example I have found on the web. But, the expected result is that the code should...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.