Windows Vista Forums

string manipulation
  1. #1


    karsagarwal Guest

    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

  2. #2


    mayayana Guest

    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.

    > 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

  3. #3


    ekkehard.horner Guest

    Re: string manipulation

    karsagarwal@xxxxxx schrieb:

    > 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.

    > 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

  4. #4


    ekkehard.horner Guest

    Re: string manipulation

    mayayana schrieb:
    [...]
    [OT]
    Look mayayana - no RegExp at all!

      My System SpecsSystem Spec

  5. #5


    mayayana Guest

    Re: string manipulation

    > [OT]

    > Look mayayana - no RegExp at all!
    Impressive!



      My System SpecsSystem Spec

  6. #6


    karsagarwal Guest

    Re: string manipulation

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

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

    Thnaks again!

    SA

      My System SpecsSystem Spec

string manipulation problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
string manipulation - join lines James VB Script 15 29 Jul 2009
Re: String manipulation using replace etc. karsagarwal VB Script 0 23 Sep 2008
Re: String manipulation using replace etc. karsagarwal VB Script 0 23 Sep 2008
string manipulation Justin Rich PowerShell 2 24 Jul 2008
string manipulation Anatoli PowerShell 8 21 Aug 2007