Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - array mid function :)

Reply
 
Old 12-13-2008   #1 (permalink)
Benny Pedersen


 
 

array mid function :)

Here is a new sub named aMid. (written today).

Like the normal Mid: mid("abc",1,2)
this new one takes an array instead

Benny Pedersen,
www.fineraw.com


Example:

dim arr: arr= split("a b c")

aMid arr,1,2
msgBox """" & join(arr) & """"


sub aMid(byRef a, byVal M1, byVal M2)
dim b:b= uBound(a)+2-M2-M1: b= M2-1-(b<0)*b
a= split(split(join(a,chr(0)),chr(0),M1,0)(M1-1),chr(0),M2+1,0)
reDim preServe a(b)
end sub

My System SpecsSystem Spec
Old 12-13-2008   #2 (permalink)
Benny Pedersen


 
 

Re: array mid function :)

On Dec 13, 4:53*pm, Benny Pedersen <b.peder...@xxxxxx> wrote:
Quote:

> Here is a new sub named aMid. (written today).
>
> Like the normal Mid: mid("abc",1,2)
> this new one takes an array instead
>
> Benny Pedersen,www.fineraw.com
>
> Example:
>
> dim arr: arr= split("a b c")
>
> aMid arr,1,2
> msgBox """" & join(arr) & """"
>
> sub aMid(byRef a, byVal M1, byVal M2)
> * dim b:b= uBound(a)+2-M2-M1: b= M2-1-(b<0)*b
> * a= split(split(join(a,chr(0)),chr(0),M1,0)(M1-1),chr(0),M2+1,0)
> * reDim preServe a(b)
> end sub
Hmm, after more testing, I decided that
"aMid(Array,X,Y)" should be written better
than the normal "Mid(String, X, Y)", like this:

sub aMid(byRef A,byVal X,byVal Y)_
dim B:B= uBound(A) +1: if X<1or X>B then reDim A(0): exit sub'
A= split(split(join(A,chr(0)),chr(0),X,0)(X-1),chr(0),Y+1,0):_
B= B +1 -Y -X: B= Y -1 -(B<0)*B: reDim preServe A(-B*(Y>-1)):_
end sub

Benny Pedersen,
www.fineraw.com (my photo gallery)
E-mail is outdated. instead use: firstname+dot+lastname gmail.com
My System SpecsSystem Spec
Old 12-13-2008   #3 (permalink)
Benny Pedersen


 
 

Re: array mid function :)

On Dec 14, 3:09*am, Benny Pedersen <b.peder...@xxxxxx> wrote:
Quote:

> On Dec 13, 4:53*pm, Benny Pedersen <b.peder...@xxxxxx> wrote:
>
>
>
>
>
Quote:

> > Here is a new sub named aMid. (written today).
>
Quote:

> > Like the normal Mid: mid("abc",1,2)
> > this new one takes an array instead
>
Quote:

> > Benny Pedersen,www.fineraw.com
>
Quote:

> > Example:
>
Quote:

> > dim arr: arr= split("a b c")
>
Quote:

> > aMid arr,1,2
> > msgBox """" & join(arr) & """"
>
Quote:

> > sub aMid(byRef a, byVal M1, byVal M2)
> > * dim b:b= uBound(a)+2-M2-M1: b= M2-1-(b<0)*b
> > * a= split(split(join(a,chr(0)),chr(0),M1,0)(M1-1),chr(0),M2+1,0)
> > * reDim preServe a(b)
> > end sub
>
> Hmm, after more testing, I decided that
> "aMid(Array,X,Y)" should be written better
> than the normal "Mid(String, X, Y)", like this:
>
> sub aMid(byRef A,byVal X,byVal Y)_
> * dim B:B= uBound(A) +1: if X<1or X>B then reDim A(0): exit sub'
> * A= split(split(join(A,chr(0)),chr(0),X,0)(X-1),chr(0),Y+1,0):_
> * B= B +1 -Y -X: B= Y -1 -(B<0)*B: reDim preServe A(-B*(Y>-1)):_
> end sub
>
> Benny Pedersen,www.fineraw.com(my photo gallery)
> E-mail is outdated. instead use: firstname+dot+lastname gmail.com- Hide quoted text -
>
> - Show quoted text -

' Oops, forgot to test some of the numbers below zero.
' Benny Pedersen,
' PS. The code for testing is now included:


option explicit

sub aMid(byRef A,byVal X,byVal Y)
dim B:B= uBound(A) +1
if X<1 or Y<1 or X>B then reDim A(0): exit sub'
A= split(split(join(A,chr(0)),chr(0),X,0)(X-1),chr(0),Y+1,0)
B= B +1 -Y -X: B= Y -1 -(B<0)*B: reDim preServe A(B)
end sub

'// Testing aMid:

sub BeginCScript(debug, maxWin)
if wScript.arguments.count<>1 then
createObject("wScript.shell").run"cScript.exe //nologo" _
& left(" //d", -4 * debug) & " """_
& wScript.scriptFullName &""" ""!""", 1-maxWin*2, false
wScript.quit
end if
end sub

BeginCScript true, true

dim m1,m2, s, arr
s= "a b c d"
arr= split(s)
wScript.echo string(18," ") & """" & join(arr) & """"
for m1= -2 to 5: wScript.echo string(80,"_")
for m2= -2 to 5
arr= split(s)
aMid arr, m1, m2
wScript.echo "aMid(arr," & m1 & "," & m2 & ") is: """ &_
join(arr) & """" & vbLf
next
next
msgBox "End of Test" & vbLf & s, 4096, ""
wScript.quit
My System SpecsSystem Spec
Old 12-14-2008   #4 (permalink)
Monitor


 
 

Re: array mid function :)


"Benny Pedersen" <b.pedersen@xxxxxx> wrote in message
news:902c9084-f6f5-47fe-b0b8-8ac33046fc8c@xxxxxx
On Dec 14, 3:09 am, Benny Pedersen <b.peder...@xxxxxx> wrote:
Quote:

> On Dec 13, 4:53 pm, Benny Pedersen <b.peder...@xxxxxx> wrote:
>
Quote:

> > Here is a new sub named aMid. (written today).
>
Quote:

> > Like the normal Mid: mid("abc",1,2)
> > this new one takes an array instead
>
Quote:

> > Benny Pedersen,www.fineraw.com
>
> Hmm, after more testing, I decided that
> "aMid(Array,X,Y)" should be written better
> than the normal "Mid(String, X, Y)", like this:
>
> sub aMid(byRef A,byVal X,byVal Y)_
> dim B:B= uBound(A) +1: if X<1or X>B then reDim A(0): exit sub'
> A= split(split(join(A,chr(0)),chr(0),X,0)(X-1),chr(0),Y+1,0):_
> B= B +1 -Y -X: B= Y -1 -(B<0)*B: reDim preServe A(-B*(Y>-1)):_
> end sub
>
> Benny Pedersen,www.fineraw.com(my photo gallery)
> E-mail is outdated. instead use: firstname+dot+lastname gmail.com- Hide
quoted text -
Quote:

>
> - Show quoted text -
' Oops, forgot to test some of the numbers below zero.
' Benny Pedersen,
' PS. The code for testing is now included:

Thinking aloud?


My System SpecsSystem Spec
Old 12-14-2008   #5 (permalink)
Benny Pedersen


 
 

Re: array mid function :)

On Dec 14, 10:47*am, "Monitor" <nos...@xxxxxx> wrote:
....SNIP...
Quote:

> ' Oops, forgot to test some of the numbers below zero.
> ' Benny Pedersen,
> ' PS. The code for testing is now included:
>
> Thinking aloud?
No, I just didn't wanted the misplaced numbers to return an error,
only the numbers above zero would make some sense (obvious)...

The aMid procedure was written for speed testing some other code, but
failed to speed up the single array version of BennySort.

sub BennySort(byRef arr, iBegin)
dim e, H, A, L, i, u: u= uBound(arr)
if iBegin<1 then reDim preServe arr(u+1): arr(u+1)= arr(0)'
for i= 2 to u -(iBegin<1): H= i: A= i\2: L= 0
e= arr(H)
do: if e < arr(A) then
H= A
else L= A
end if: A= (L+H)\2
loop until A=L
for H= i to H +1 step-1
arr(H)= arr(H-1)
next: arr(H)= e
next
if iBegin<1 then for i= 0 to u: arr(i)= arr(i+1): next: _
reDim preServe arr(u)'
end sub

' Note: The argument iBegin should be 0 or 1.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Logon script - function array and select case not working VB Script
VBscript Array Split Function VB Script
Fast copy method of sub array (=array range) possible? VB Script
Stupid Array Tricks: Initializing an Array to a Certain Size PowerShell
adding function return to an array PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46