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 - string manipulation

Reply
 
Old 11-22-2008   #1 (permalink)
karsagarwal


 
 

string manipulation

Hi,

I am including a part of a code below with the output I got. Inn this
code I read in 2 chars at a time from the "opt" string and divide the
string opt into odd and even numbers. It works great but for some
reason (I don't understand why) it does not read the "10" in the char2
string itself.

The basic purpose is to divide the opt string into odd and even
strings taking 2 chars at a time starting from the left. Pleeaze help
me !!!!!


Smaple Code:
===========
optEven=""
optOdd=""
opt="02030501070911131506080410121416"
if len(opt) > 0 then 'gives the len of the string to be parsed as it
varies for diffent applications
fileCount = (len(opt))/2 'fileCount holds info about the #
of files to be created
end if

For i= 1 to fileCount
'1 extract 2 numeric chars from the string until end of string
char2=left(opt,2)
opt=Replace(opt,left(opt,2),"")
result = (CINT(char2)) mod 2
if result = 0 then
optEven=optEven & char2
else
optOdd=optOdd & char2
end if
next


output
======
opt=02030501070911131506080410121416
char2=02
char2=03
char2=05
char2=01
char2=07
char2=09
char2=11
char2=13
char2=15
char2=06
char2=08
char2=04
char2=12
char2=14
char2=16



My System SpecsSystem Spec
Old 11-22-2008   #2 (permalink)
mayayana


 
 

Re: string manipulation

opt = Replace(opt, left(opt, 2), "")

That line is replacing any instance of the 2 characters
at left of the opt string. Since 1012 appears later, it
erases the 01, contracting 1012 to 12 and thereby losing
the 10.

A better way to do it, which is also more efficient:

i2 = 1
For i = 1 to fileCount
char2 = Mid(opt, i2, 2)
result = (CINT(char2)) mod 2
If result = 0 Then
optEven = optEven & char2
Else
optOdd = optOdd & char2
End If
i2 = i2 + 2
Next

i2 is used to hold the position in the string for
the next read. That method of getting char2
also saves on string allocations, since it's not
recreating the opt string with each iteration.

Another thought: This won't matter if your string
is always guaranteed to have an even length,
but the \ operator returns an integer and is therefore
probably better to use. If you have a string with, say,
21 characters then \ will return a file count of 10
but / will return a file count of 10.5. So a script
using \ can complete, dropping the last character. But
a script using / will fail.
Quote:

> Hi,
>
> I am including a part of a code below with the output I got. Inn this
> code I read in 2 chars at a time from the "opt" string and divide the
> string opt into odd and even numbers. It works great but for some
> reason (I don't understand why) it does not read the "10" in the char2
> string itself.
>
> The basic purpose is to divide the opt string into odd and even
> strings taking 2 chars at a time starting from the left. Pleeaze help
> me !!!!!
>
>
> Smaple Code:
> ===========
> optEven=""
> optOdd=""
> opt="02030501070911131506080410121416"
> if len(opt) > 0 then 'gives the len of the string to be parsed as it
> varies for diffent applications
> fileCount = (len(opt))/2 'fileCount holds info about the #
> of files to be created
> end if
>
> For i= 1 to fileCount
> '1 extract 2 numeric chars from the string until end of string
> char2=left(opt,2)
> opt=Replace(opt,left(opt,2),"")
> result = (CINT(char2)) mod 2
> if result = 0 then
> optEven=optEven & char2
> else
> optOdd=optOdd & char2
> end if
> next
>
>
> output
> ======
> opt=02030501070911131506080410121416
> char2=02
> char2=03
> char2=05
> char2=01
> char2=07
> char2=09
> char2=11
> char2=13
> char2=15
> char2=06
> char2=08
> char2=04
> char2=12
> char2=14
> char2=16
>
>

My System SpecsSystem Spec
Old 11-22-2008   #3 (permalink)
ekkehard.horner


 
 

Re: string manipulation

karsagarwal@xxxxxx schrieb:
Quote:

> Hi,
>
> I am including a part of a code below with the output I got. Inn this
> code I read in 2 chars at a time from the "opt" string and divide the
> string opt into odd and even numbers. It works great but for some
> reason (I don't understand why) it does not read the "10" in the char2
> string itself.
>
> The basic purpose is to divide the opt string into odd and even
> strings taking 2 chars at a time starting from the left. Pleeaze help
> me !!!!!
>
>
> Smaple Code:
> ===========
> optEven=""
> optOdd=""
> opt="02030501070911131506080410121416"
> if len(opt) > 0 then 'gives the len of the string to be parsed as it
> varies for diffent applications
> fileCount = (len(opt))/2 'fileCount holds info about the #
> of files to be created
> end if
>
> For i= 1 to fileCount
> '1 extract 2 numeric chars from the string until end of string
> char2=left(opt,2)
> opt=Replace(opt,left(opt,2),"")
If you insert a diagnostic .Echo like

WScript.Echo Len( opt ), char2, opt

here and look at the output

30 02 030501070911131506080410121416
28 03 0501070911131506080410121416
26 05 01070911131506080410121416
22 01 0709111315060804121416
20 07 09111315060804121416
18 09 111315060804121416
....

you'll see that *replacing* 01 in

26 05 01-07091113150608041-01-21416

will destoy your opt string.
Quote:

> result = (CINT(char2)) mod 2
> if result = 0 then
> optEven=optEven & char2
> else
> optOdd=optOdd & char2
> end if
> next
[...]

There is no need to change opt to process it in steps of two
characters:

Dim sEven : sEven = ""
Dim sOdd : sOdd = ""
Dim sOpt : sOpt = "02030501070911131506080410121416"
Dim nPos
For nPos = 1 To Len( sOpt ) Step 2
Dim sPair : sPair = Mid( sOpt, nPos, 2 )
If 0 = CInt( sPair ) Mod 2 Then
sEven = sEven & sPair
Else
sOdd = sOdd & sPair
End If
Next
WScript.Echo sOpt
WScript.Echo sEven
WScript.Echo sOdd

output:

=== parsePairs: parse pairs from string ==
02030501070911131506080410121416
0206080410121416
0305010709111315
=== parsePairs: 0 done (00:00:00) ========


My System SpecsSystem Spec
Old 11-22-2008   #4 (permalink)
ekkehard.horner


 
 

Re: string manipulation

mayayana schrieb:
[...]
[OT]
Look mayayana - no RegExp at all!
My System SpecsSystem Spec
Old 11-22-2008   #5 (permalink)
mayayana


 
 

Re: string manipulation

> [OT]
Quote:

> Look mayayana - no RegExp at all!
Impressive!


My System SpecsSystem Spec
Old 11-22-2008   #6 (permalink)
karsagarwal


 
 

Re: string manipulation

On Nov 22, 1:30*pm, "mayayana" <mayaXXy...@xxxxxx> wrote:
Quote:
Quote:

> > [OT]
> > Look mayayana - no RegExp at all!
>
> * *Impressive!
Your help is very much appreciated !!

Thnaks again!

SA
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
string manipulation - join lines VB Script
Re: String manipulation using replace etc. VB Script
Re: String manipulation using replace etc. VB Script
string manipulation PowerShell
string manipulation 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