vbs没有自定义排序函数。无需自己造轮子,可以用其他语言来完成这个任务(在传递数据比较简单的情况下,例如只传递数组)。

首先用5分钟写一个C++排序的代码。命名为“mysort.cpp”:

#include<bits/stdc++.h>

using namespace std;

int main(int argc,char * argv[]){
freopen("val.txt","w",stdout) ; //重定向输出
vector<int> v;
int t;
for(int i=;i<argc;i++){ //从命令行参数中获取变量
sscanf(argv[i],"%d",&t);
v.push_back(t);
}
sort(v.begin(),v.end());
for(int i=;i<v.size();i++){
printf("%d ",v[i]);
}
return ;
}

编译并测试之后,开始编写vbs代码。

这里借用昨天编写的vbs的vector类:

class Vector
Private length
public data()
Sub Class_Initialize()
length=
End Sub
'插入元素'
public Function Add (byval x)
length=length+
redim preserve data(length-)
data(length-)=x
End Function
'快速展示'
public Function display()
dim i
dim s
for i= to length-
s=s & cstr(data(i)) & " "
next
msgbox s
End Function
'返回大小'
public Function size()
size= length
End Function
'获取元素'
public Function getAt (byval pos)
if pos< or pos>length- then
getAt=
exit Function
end if
getAt=data(pos)
End Function
'删除元素'
public Function delAt (byval pos)
if pos< or pos>length- then exit Function '注意退出函数的表达式'
dim i
for i=pos to length-
data(i)=data(i+)
next
length=length-
redim preserve data(length-)
End Function
'插入元素'
public Function insert (byval pos,byval elem)
if pos< or pos>length- then exit Function
dim i
length=length+
redim preserve data(length-)
for i=length- to pos+ step -
data(i)=data(i-)
next
data(pos)=elem
End Function
end class

vector类

注意到外存传参这个操作可以抽象拿出来作为一个函数。编写extendProcess函数

public Function extendProcess (byval exe,byval arr) '程序名、数组'
dim i
dim cmd
cmd=exe
For i= To ubound(arr)
cmd=cmd & " " & arr(i)
Next
dim objShell '执行命令'
Set objShell = CreateObject("Wscript.Shell")
objShell.Run(cmd)
'读入输出文件val.txt'
Set fs = CreateObject("Scripting.FileSystemObject")
Set file = fs.OpenTextFile("val.txt", , false)
info=file.readall
file.close
set fs=nothing
'处理信息'
extendProcess=split(info) '用空格分隔'
End Function

(在这里,程序名可以是cpp编译出来的exe,也可以是java字节码,调用方法:java 主类,也可以是python代码,调用方法: python scrip.py,甚至可以是MATLAB脚本,这里不赘述)

然后我们来愉快的测试吧,看看效果:

测试代码:

set v = new Vector
v.Add
v.Add
v.Add
v.Add
v.Add
v.Add
v.Add
v.Add
ans=extendProcess("mysort",v.data) '如果函数有返回,就必须要有括号。如果无返回,并且参数数目大于1,不能带括号'
set v2 = new Vector
for each x in ans
v2.Add x
next
v2.display

测试效果:

vbs与其他语言进行交互编程(外存传参)-LMLPHP

完整代码:

class Vector
Private length
public data()
Sub Class_Initialize()
length=
End Sub
'插入元素'
public Function Add (byval x)
length=length+
redim preserve data(length-)
data(length-)=x
End Function
'快速展示'
public Function display()
dim i
dim s
for i= to length-
s=s & cstr(data(i)) & " "
next
msgbox s
End Function
'返回大小'
public Function size()
size= length
End Function
'获取元素'
public Function getAt (byval pos)
if pos< or pos>length- then
getAt=
exit Function
end if
getAt=data(pos)
End Function
'删除元素'
public Function delAt (byval pos)
if pos< or pos>length- then exit Function '注意退出函数的表达式'
dim i
for i=pos to length-
data(i)=data(i+)
next
length=length-
redim preserve data(length-)
End Function
'插入元素'
public Function insert (byval pos,byval elem)
if pos< or pos>length- then exit Function
dim i
length=length+
redim preserve data(length-)
for i=length- to pos+ step -
data(i)=data(i-)
next
data(pos)=elem
End Function
end class public Function extendProcess (byval exe,byval arr) '程序名、数组'
dim i
dim cmd
cmd=exe
For i= To ubound(arr)
cmd=cmd & " " & arr(i)
Next
dim objShell '执行命令'
Set objShell = CreateObject("Wscript.Shell")
objShell.Run(cmd)
'读入输出文件val.txt'
Set fs = CreateObject("Scripting.FileSystemObject")
Set file = fs.OpenTextFile("val.txt", , false)
info=file.readall
file.close
set fs=nothing
'处理信息'
extendProcess=split(info) '用空格分隔'
End Function set v = new Vector
v.Add
v.Add
v.Add
v.Add
v.Add
v.Add
v.Add
v.Add
ans=extendProcess("mysort",v.data) '如果函数有返回,就必须要有括号。如果无返回,并且参数数目大于1,不能带括号'
set v2 = new Vector
for each x in ans
v2.Add x
next
v2.display ' dim objShell
' Set objShell = CreateObject("Wscript.Shell")
' objShell.Run("%comspec% /k ipconfig /all")

完整vbs代码

05-28 00:40