Announcement

Collapse
No announcement yet.

Increment a string - dohhhh!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dale Hubbard
    replied
    Thanks again for such fantastic help. I'm going to buy the latest versions of PBCC and PBWin on the strength of this great forum!

    Cheers!

    Leave a comment:


  • Donald Darden
    replied
    Here is a very simple technique for any string length:

    Oops! Left out a very important increment step:
    Code:
    DO
       FOR a = LEN(aa$) TO 1 STEP -1
         MID$(aa$, a) = CHR$(ASC(aa$, a) + 1)
         IF MID$(aa$, a, 1) <="Z" THEN EXIT FOR
         MID$(aa$, a) = "A"
       NEXT
       IF a = 0 THEN EXIT DO
       'Do what you want with the string here
    LOOP
    This might be slightly faster:
    Code:
    DO
       FOR a = LEN(aa$) TO 1 STEP -1
         b = ASC(aa$, a)
         INCR b
         IF b > ASC("Z") THEN b = ASC("A")
         MID$(aa$, a) = CHR$(b)
         IF b > ASC("A") THEN EXIT FOR
       NEXT
       IF a = 0 THEN EXIT DO
       'Do what you want with the string here
    LOOP
    ------------------
    Old Navy Chief, Systems Engineer, Systems Analyst, now semi-retired

    [This message has been edited by Donald Darden (edited September 19, 2005).]

    Leave a comment:


  • Jim Martin
    replied
    Try this:

    Assuming an input string "InputString$", a lower ASCII value for
    string values LowerLimit% and an upper value UpperLimit%,
    declare the following variables as shared:

    k%, w%, LowerLimit%, UpperLimit%, StartLocation%, FinalLocation%,
    WorkingString$

    Set the following values:

    LowerLimit% = 65
    UpperLimit% = 90 'Upper case A to Z, modify as you will

    WorkingString$ = "X" + InputString$ + "X"
    k% = Len(InputString$)
    StartLocation% = k% + 1
    w% = StartLocation%
    FinalLocation% = w%

    Now call the subroutine ModifyString. When it exits, WorkingString$
    will be modified by one increment, except that you need to strip off
    the leading and trailing character (which were inserted to avoid
    zero-length right and left fragments). If FinalLocation% is zero,
    the sequence of modifications is complete. After each run of
    ModifyString, do NOT change the values assigned above until you want
    to deal with a new string.


    SUB ModifyString

    ModificationComplete% = 0

    Do While (ModificationComplete% = 0)

    RightFragment$ = Right$(WorkingString$,(w% - 1))
    MiddleFragment$ = Mid$(WorkingString$,w%,1)
    LeftFragment$ = Left$(WorkingString$,(k% + 2 - w%)

    x% = asc(MiddleFragment$)
    incr x%

    If (x% > UpperLimit%) then
    x% = LowerLimit%
    decr w%
    ModificationComplete% = 0
    Else
    If (ModificationComplete% = 0) then
    FinalLocation% = w%
    w% = StartLocation%
    End if
    ModificationComplete% = 255
    End If

    MiddleFragment$ = Chr$(x%)

    WorkingString$ = LeftFragment$ + MiddleFragment$ + RightFragment$

    Wend

    END SUB

    Jim Martin


    ------------------

    Leave a comment:


  • Dale Hubbard
    replied
    You are so kind.

    Exactly what I needed. Just had a complete blank on it.

    Thanks again!

    Leave a comment:


  • Mel Bishop
    replied
    Code:
    for a = 65 to 90
    for b = 65 to 90
    for c = 65 to 90
    for d = 65 to 90
    '
    TextString$ = chr$(a) + chr$(b) + chr$(c) + chr$(d)
    '
    do something with the text string
    '
    next d
    next c
    next b 
    next a
    This what you had in mind?


    ------------------


    [This message has been edited by Mel Bishop (edited September 13, 2005).]

    Leave a comment:


  • Dale Hubbard
    started a topic Increment a string - dohhhh!

    Increment a string - dohhhh!

    Hi,

    I've been sitting in front of PB35 for an hour now, scratching my head...

    All I want to do is start with a string like "AAAA" - and increment
    it so that I get

    AAAA
    AAAB
    ... (skipped for obvious brevity)
    AABA
    AABB
    ... (skip)
    ZZZZ

    In simple terms, "AAAA" thru "ZZZZ" -- all combinations.

    Can some kind person put me out of my misery -- I did search here
    and found a subroutine which I couldn't fathom out...

    Thanks!
Working...
X