Transform string : "c;b;a;e;d" -> "a;b;c;d;e" ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Indrid Colt

    Transform string : "c;b;a;e;d" -> "a;b;c;d;e" ?

    Thank's for your help !


    Indrid


  • Tom van Stiphout

    #2
    Re: Transform string : "c;b;a;e;d " -> "a;b;c;d;e " ?

    On Mon, 11 Jul 2005 22:47:26 -0400, "Indrid Colt" <indrid@colt.co m>
    wrote:

    Well, you could use the Split function to create an array with 5
    elements. Then use any suitable sorting algorithm to sort these items.
    Then use the Join function to stitch the elements back together.

    -Tom.

    [color=blue]
    >Thank's for your help !
    >
    >
    >Indrid
    >[/color]

    Comment

    • David W. Fenton

      #3
      Re: Transform string : &quot;c;b;a;e;d &quot; -&gt; &quot;a;b;c;d;e &quot; ?

      Tom van Stiphout <no.spam.tom774 4@cox.net> wrote in
      news:fkc6d1thua dliq28absbqrjpc 535d4m1p0@4ax.c om:
      [color=blue]
      > On Mon, 11 Jul 2005 22:47:26 -0400, "Indrid Colt"
      > <indrid@colt.co m> wrote:
      >
      > Well, you could use the Split function to create an array with 5
      > elements. Then use any suitable sorting algorithm to sort these
      > items. Then use the Join function to stitch the elements back
      > together.[/color]

      For a case where the values are all one character, why not just use
      a ReverseString() function like the one posted after my sig?

      --
      David W. Fenton http://www.bway.net/~dfenton
      dfenton at bway dot net http://www.bway.net/~dfassoc

      Public Function ReverseString(s trInput As String) As String
      Dim i As Integer
      Dim tmpString As String
      Dim tmpOutput As String

      tmpString = strInput
      For i = 1 To Len(strInput)
      tmpOutput = tmpOutput & Right(tmpString , 1)
      tmpString = Left(tmpString, Len(tmpString) - 1)
      Next i
      ReverseString = tmpOutput
      End Function

      Comment

      • jimfortune@compumarc.com

        #4
        Re: Transform string : &quot;c;b;a;e;d &quot; -&gt; &quot;a;b;c;d;e &quot; ?


        David W. Fenton wrote:
        [color=blue]
        > For a case where the values are all one character, why not just use
        > a ReverseString() function like the one posted after my sig?[/color]

        Huh? Please explain what you're suggesting. I'll probably call myself
        an idiot for not seeing it. Anyway, perhaps the individual letters
        represent strings or numbers?

        James A. Fortune

        Comment

        • Tom van Stiphout

          #5
          Re: Transform string : &quot;c;b;a;e;d &quot; -&gt; &quot;a;b;c;d;e &quot; ?

          On Tue, 12 Jul 2005 22:07:46 GMT, "David W. Fenton"
          <dXXXfenton@bwa y.net.invalid> wrote:

          ?ReverseString( "c;b;a;e;d" )
          d;e;a;b;c
          The OP wanted "a;b;c;d;e"

          -Tom.

          [color=blue]
          >Tom van Stiphout <no.spam.tom774 4@cox.net> wrote in
          >news:fkc6d1thu adliq28absbqrjp c535d4m1p0@4ax. com:
          >[color=green]
          >> On Mon, 11 Jul 2005 22:47:26 -0400, "Indrid Colt"
          >> <indrid@colt.co m> wrote:
          >>
          >> Well, you could use the Split function to create an array with 5
          >> elements. Then use any suitable sorting algorithm to sort these
          >> items. Then use the Join function to stitch the elements back
          >> together.[/color]
          >
          >For a case where the values are all one character, why not just use
          >a ReverseString() function like the one posted after my sig?[/color]

          Comment

          • David W. Fenton

            #6
            Re: Transform string : &quot;c;b;a;e;d &quot; -&gt; &quot;a;b;c;d;e &quot; ?

            Tom van Stiphout <no.spam.tom774 4@cox.net> wrote in
            news:m0s8d191ss lejq4va16lqdlt0 q14e0l77i@4ax.c om:
            [color=blue]
            > On Tue, 12 Jul 2005 22:07:46 GMT, "David W. Fenton"
            ><dXXXfenton@bw ay.net.invalid> wrote:
            >
            > ?ReverseString( "c;b;a;e;d" )
            > d;e;a;b;c
            > The OP wanted "a;b;c;d;e"[/color]

            This is the main reason I don't like posts that put the question in
            the subject, and don't repeat it in the body of the post.

            Yes, I have two chances to read the subject, but in this case, I
            thought my first reading was correct, so I didn't check. Obviously,
            it wasn't.

            I don't know why the subject shouldn't have been "sort string..."
            since that's what's being asked about.

            --
            David W. Fenton http://www.bway.net/~dfenton
            dfenton at bway dot net http://www.bway.net/~dfassoc

            Comment

            • jimfortune@compumarc.com

              #7
              Re: Transform string : &quot;c;b;a;e;d &quot; -&gt; &quot;a;b;c;d;e &quot; ?

              Tom van Stiphout wrote:[color=blue]
              > On Tue, 12 Jul 2005 22:07:46 GMT, "David W. Fenton"
              > <dXXXfenton@bwa y.net.invalid> wrote:
              >
              > ?ReverseString( "c;b;a;e;d" )
              > d;e;a;b;c
              > The OP wanted "a;b;c;d;e"
              >
              > -Tom.[/color]

              You're suggesting late night coding induced dyslexia. I've been there.
              I'll take a look at the OP's problem.

              James A. Fortune

              Comment

              • jimfortune@compumarc.com

                #8
                Re: Transform string : &quot;c;b;a;e;d &quot; -&gt; &quot;a;b;c;d;e &quot; ?

                Tom van Stiphout wrote:[color=blue]
                > On Mon, 11 Jul 2005 22:47:26 -0400, "Indrid Colt" <indrid@colt.co m>
                > wrote:
                >
                > Well, you could use the Split function to create an array with 5
                > elements. Then use any suitable sorting algorithm to sort these items.
                > Then use the Join function to stitch the elements back together.
                >
                > -Tom.[/color]

                I am assuming you don't want to use ADO to put the items in a table for
                Access to sort :-). Based on Tom's suggestion:

                'Begin test code behind command button
                Dim varList As Variant
                Dim varOut As Variant

                varList = Array("10", "2", "1")
                varOut = SortMe(varList)
                MsgBox ("varOut(0) = " & varOut(0))
                MsgBox ("varOut(1) = " & varOut(1))
                MsgBox ("varOut(2) = " & varOut(2))
                'End test code behind command button

                'Begin code behind the same form
                Private Function SortMe(ByVal varArray As Variant) As Variant
                Dim N As Integer
                Dim I As Integer
                Dim J As Integer
                Dim FoundAt As Integer
                Dim varMin As Variant
                Dim varOutput As Variant

                'Assuming the number of items is small, say < 100, an
                'inefficient sort method such as one used by a card
                'player (insertion sort) is adequate and easy to understand
                varOutput = varArray
                For N = UBound(varArray ) To 0 Step -1
                varMin = varArray(0)
                FoundAt = 0
                For I = 1 To N
                If varArray(I) < varMin Then
                varMin = varArray(I)
                FoundAt = I
                End If
                Next I
                varOutput(UBoun d(varArray) - N) = varArray(FoundA t)
                For J = FoundAt To N - 1
                varArray(J) = varArray(J + 1)
                Next J
                Next N
                SortMe = varOutput
                End Function
                'End code behind the same form


                Output when the command button is clicked:
                1 10 2

                When varList = Array(10, 2, 1) the output becomes:
                1 2 10

                You can probably use the Split function to get the input string into
                varList directly. The sort function works by finding the minimum from
                the first N elements, placing it in the output, then sliding the array
                elements down starting where the minimum was found, then decrementing N
                by one. The time for this sort is proportional to the number of
                elements squared. Test this code well before using.

                James A. Fortune

                Comment

                • lylefair@yahoo.ca

                  #9
                  Re: Transform string : &quot;c;b;a;e;d &quot; -&gt; &quot;a;b;c;d;e &quot; ?

                  You could try this:

                  Public Function SortedList(ByVa l vList As String, _
                  Optional ByVal vDelimiter As String = ",") _
                  As String
                  Dim WorkingList() As String
                  WorkingList = Split(vList, vDelimiter)
                  With WizHook
                  .Key = 51488399
                  .SortStringArra y WorkingList
                  End With
                  SortedList = Join(WorkingLis t, vDelimiter)
                  End Function

                  Sub test()
                  Dim TestString As String
                  TestString = "c;b;a;e;d"
                  Debug.Print SortedList(Test String, ";")
                  End Sub

                  Comment

                  • Indrid Colt

                    #10
                    Re: Transform string : &quot;c;b;a;e;d &quot; -&gt; &quot;a;b;c;d;e &quot; ?

                    Wow !

                    Thank a lot, it's work perfectly !

                    Indrid

                    <lylefair@yahoo .ca> a écrit dans le message de news:
                    1121262744.0471 44.38590@z14g20 00...legro ups.com...[color=blue]
                    > You could try this:
                    >
                    > Public Function SortedList(ByVa l vList As String, _
                    > Optional ByVal vDelimiter As String = ",") _
                    > As String
                    > Dim WorkingList() As String
                    > WorkingList = Split(vList, vDelimiter)
                    > With WizHook
                    > .Key = 51488399
                    > .SortStringArra y WorkingList
                    > End With
                    > SortedList = Join(WorkingLis t, vDelimiter)
                    > End Function
                    >
                    > Sub test()
                    > Dim TestString As String
                    > TestString = "c;b;a;e;d"
                    > Debug.Print SortedList(Test String, ";")
                    > End Sub
                    >[/color]


                    Comment

                    • David W. Fenton

                      #11
                      Re: Transform string : &quot;c;b;a;e;d &quot; -&gt; &quot;a;b;c;d;e &quot; ?

                      "lylefair@yahoo .ca" <lylefair@yahoo .ca> wrote in
                      news:1121262744 .047144.38590@z 14g2000cwz.goog legroups.com:
                      [color=blue]
                      > You could try this:
                      >
                      > Public Function SortedList(ByVa l vList As String, _
                      > Optional ByVal vDelimiter As String = ",") _
                      > As String
                      > Dim WorkingList() As String
                      > WorkingList = Split(vList, vDelimiter)
                      > With WizHook
                      > .Key = 51488399
                      > .SortStringArra y WorkingList
                      > End With
                      > SortedList = Join(WorkingLis t, vDelimiter)
                      > End Function
                      >
                      > Sub test()
                      > Dim TestString As String
                      > TestString = "c;b;a;e;d"
                      > Debug.Print SortedList(Test String, ";")
                      > End Sub[/color]

                      So, Lyle, why do you make a fetish of posting things like this and
                      making no effort to explain them?

                      Specifically, the WizHook WITH block, and the meaning of the
                      property and method you've used.

                      How did you find this?

                      And what is the meaning of the key?

                      --
                      David W. Fenton http://www.bway.net/~dfenton
                      dfenton at bway dot net http://www.bway.net/~dfassoc

                      Comment

                      • jimfortune@compumarc.com

                        #12
                        Re: Transform string : &quot;c;b;a;e;d &quot; -&gt; &quot;a;b;c;d;e &quot; ?


                        David W. Fenton wrote:
                        [color=blue]
                        > So, Lyle, why do you make a fetish of posting things like this and
                        > making no effort to explain them?
                        >
                        > Specifically, the WizHook WITH block, and the meaning of the
                        > property and method you've used.
                        >
                        > How did you find this?
                        >
                        > And what is the meaning of the key?
                        >
                        > --
                        > David W. Fenton http://www.bway.net/~dfenton
                        > dfenton at bway dot net http://www.bway.net/~dfassoc[/color]

                        Start at about post #7 of:



                        where Lyle uses a recordset to do the sorting (maybe I'm psychic) then
                        gets a wizhook solution from peter walker. It looks like Lyle started
                        using the .Key = 51488399 around late 2003. I don't know what it means
                        since that's the nature of undocumented methods. Anyway, it won't work
                        for the users who have A97 so for me it will stay undocumented unless
                        Lyle tells us more. I'm all for unorthodox methods but this one seems
                        a little more fragile than most.

                        James A. Fortune

                        Comment

                        • Trevor Best

                          #13
                          Re: Transform string : &quot;c;b;a;e;d &quot; -&gt; &quot;a;b;c;d;e &quot; ?

                          jimfortune@comp umarc.com wrote:[color=blue]
                          > where Lyle uses a recordset to do the sorting (maybe I'm psychic) then
                          > gets a wizhook solution from peter walker. It looks like Lyle started
                          > using the .Key = 51488399 around late 2003. I don't know what it means
                          > since that's the nature of undocumented methods. Anyway, it won't work
                          > for the users who have A97 so for me it will stay undocumented unless
                          > Lyle tells us more. I'm all for unorthodox methods but this one seems
                          > a little more fragile than most.[/color]

                          or:

                          Sub ShellSort(pstrA rray() As String, plngMaxItem As Long)

                          ' Sort Array of string into order
                          Dim lngSpan As Long
                          Dim i As Long, j As Long
                          Dim strDummy As String

                          ' Get to middle of list
                          lngSpan = plngMaxItem \ 2

                          Do While lngSpan > 0
                          For i = lngSpan To plngMaxItem - 1
                          j = i - lngSpan + 1
                          For j = (i - lngSpan + 1) To 1 Step -lngSpan
                          If pstrArray(j) <= pstrArray(j + lngSpan) Then
                          Exit For
                          End If
                          ' Swap array elements that are out of order.
                          strDummy = pstrArray(j + lngSpan)
                          pstrArray(j + lngSpan) = pstrArray(j)
                          pstrArray(j) = strDummy
                          Next j
                          Next i
                          lngSpan = lngSpan \ 2
                          Loop

                          End Sub

                          Yeah I know the code could be updated to use lbound and ubound instead
                          of assuming starting at 1 and using a parameter for the number of
                          elements but it was written yonks ago (back in the DOS days).

                          I did try a few algorithms including bubble and quick, quicksort was
                          supposed to be the fastest but that was only true in a programming
                          language that used pointers for strings like C. Bubble sort was quickest
                          if the array was only slightly out of order, for most other
                          circumstances I found the shell Sort to be quickest in BASIC, and the
                          second simplest in complexity. The Bubble sort is the simplest and for
                          small lists like the one in the subject you wouldn't notice a difference
                          in speed. Bubble Sort below:

                          Sub BubbleSort(pstr Array() As String, plngMaxItem As Long)
                          Dim i As Long
                          Dim fSwitched As Boolean
                          Dim strTemp As String

                          Do
                          fSwitched = False
                          For i = 1 To plngMaxItem - 1
                          If pstrArray(i) > pstrArray(i + 1) Then
                          ' item out of place, move it
                          fSwitched = True
                          ' swap items
                          strTemp = pstrArray(i)
                          pstrArray(i) = pstrArray(i + 1)
                          pstrArray(i + 1) = strTemp
                          End If
                          Next
                          Loop While fSwitched
                          End Sub




                          --
                          [OO=00=OO]

                          Comment

                          • lylefair@yahoo.ca

                            #14
                            Re: Transform string : &quot;c;b;a;e;d &quot; -&gt; &quot;a;b;c;d;e &quot; ?

                            Access/VBA has many hidden members.

                            One can see them by opening a module, clicking on the object browser,
                            right clicking in the object browser window and selecting "Show Hidden
                            Members"

                            Some of these can be very useful. My favourite is Collect, a member of
                            both DAO and ADO recordsets. Collect returns the value of a recordest
                            field referenced by an ordinal or name. TTBOMK Collect is the second
                            fastest (and much the simplest) way to retrieve the value of a
                            recordset field. Creating a field DAO or ADO object and referencing its
                            Value property is minimally faster (demonstrated by Dimitri Furman).
                            The dot and Recordset.Field s("FieldName" OR Ordinal) are appreciably
                            slower.

                            Wizhook and its seeming clone _Wizhook are hidden objects. To use any
                            if its methods one must first enter a key: 51488399. I can only
                            speculate as to Why; perhaps it is because MS does not want us to use
                            them? Wizhook was first available in Access 2000.
                            I first learned of Wizhook in the thread James Fortune cites, in a
                            discussion with Peter Walker about sorting arrays. Michael Kaplan
                            discourages the use of Wizhook, for the reason that most of its members
                            do not work, do not work well, or work in an unpredictable way.
                            The are a few of Wizhook's members that I find useful:

                            FileExists(File As String) As Boolean

                            FullPath(Relati vePath As String, FullPath As String) As Integer

                            IsValidIdent(Id entifier As String) As Boolean

                            SortStringArray (Array() As String)

                            SplitPath(Path As String, Drive As String, Dir As String, File As
                            String, Ext As String)

                            TableFieldHasUn iqueIndex(Table As String, Columns As String) As Boolean

                            WizMsgBox(bstrT ext As String, bstrCaption As String, wStyle As Long,
                            idHelpID As Long, bstrHelpFileNam e As String) As Long

                            I have not had problems with these not working; that is no guarantee
                            there never will be problems.

                            Some of these require passing parameters that acquire values wanted, by
                            reference, something that is not so common in VBA scripting.

                            IMO Wizhook should be used only by experienced, capable, professional
                            developers, and, I suspect, many of these would recommend against its
                            use alrogether.

                            Comment

                            • David W. Fenton

                              #15
                              Re: Transform string : &quot;c;b;a;e;d &quot; -&gt; &quot;a;b;c;d;e &quot; ?

                              "lylefair@yahoo .ca" <lylefair@yahoo .ca> wrote in
                              news:1121351712 .595897.319420@ g49g2000cwa.goo glegroups.com:
                              [color=blue]
                              > Access/VBA has many hidden members.[/color]

                              My question was not so much a question as a comment about posting
                              style. My preference is that someone posting about undocumented
                              methods using hidden members should endeavor to explain the meaning
                              of them when offering them as a solution.

                              --
                              David W. Fenton http://www.bway.net/~dfenton
                              dfenton at bway dot net http://www.bway.net/~dfassoc

                              Comment

                              Working...