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?!... - 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\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
16 2534
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
yes, thanks;
Sorry, lost track of the forums; I'll try to get the right one..
Best, Randalll
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
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 - function SortArray(arrArray) {
-
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"]];
-
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);
-
}
So "return list[2][1];" works,
but not "return list;"?
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.
I through this together fairly quickly. I think it addresses your needs. Tested in FF and IE 7 -
<html>
-
<head>
-
<script type="text/javascript" >
-
function SortArray(arrArray) {
-
copyOfArray = arrArray.slice(0);
-
copyOfArray.sort(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","textarea 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>
-
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
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
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.
Heya, Randall.
So what you're trying to do is pass a VB object from JavaScript to VB. Is this correct?
Thanks;
I can see by this exercise that what I don't understand is the nature of the return from "Array.sort"? - code = code& vblf & " return arrArray.toArray().sort();"
works n ""JSort1D" - code = code& vblf & " return arrArray;"
works in "JSort1Da"
but - code = code& vblf & " return arrArray.sort();"
does not work in "JSort1Db"
Best, Randall - ' 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
-
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; - NewArray[i][j]=tempArray[j]++;
or - NewArray[i][j]=tempArray[j];
, so I think my declaration of the arrays or sub-arrays is incorrect?....
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
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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. ...
|
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...
|
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...
|
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...
|
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:
...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |