Re: String Array Insert



Just to be clear, I should have renamed the second subroutine differently from the first one I posted... leaving the word "Beginning" in its name is an erroneous indication of what it does. Here is the same subroutine, but with a more appropriate name....

Sub AddBlankElements(YourArray() As String, _
Optional AtIndexNumber As Long = 0, _
Optional Number As Long = 1)
Dim Position As Long
Dim Combined As String
Combined = Join(YourArray, Chr$(1))
Position = InStr(Replace(Chr$(1) & Combined, Chr$(1), _
Chr$(2), , AtIndexNumber), Chr$(1)) - 1
Combined = Left(Combined, Position) & String(Number, Chr$(1)) & _
Mid$(Combined, Position + 1)
YourArray = Split(Combined, Chr$(1))
End Sub

Sorry for any confusion my forgetting to change the second subroutine's name may have caused you.

Rick


"Rick Rothstein (MVP - VB)" <rick.newsNO.SPAM@xxxxxxxxxxxxxxxxxx> wrote in message news:utU%23LC8eIHA.5280@xxxxxxxxxxxxxxxxxxxxxxx
Sorry, I missed that aspect of your question in your original post. The following code, albeit somewhat slower than my original one-liner (which won't matter unless you are using this in a large loop against a very large array), will insert one or more empty elements (specified by the optional 3rd argument which has a default value of 1) at the element index number specified in the 2nd optional argument (whose default value is 0). Note, the code only will work with zero-based arrays.

Sub AddBlankElementsAtBeginning(YourArray() As String, _
Optional AtIndexNumber As Long = 0, _
Optional Number As Long = 1)
Dim Position As Long
Dim Combined As String
Combined = Join(YourArray, Chr$(1))
Position = InStr(Replace(Chr$(1) & Combined, Chr$(1), _
Chr$(2), , AtIndexNumber), Chr$(1)) - 1
Combined = Left(Combined, Position) & String(Number, Chr$(1)) & _
Mid$(Combined, Position + 1)
YourArray = Split(Combined, Chr$(1))
End Sub

Rick


Looking for the more general case where I can insert at any point in the array.


If you include the time that will be required for your ReDim Preserve
statement, and couple it with whatever copy memory code you might come up
with, I don't think it will be dramatically faster (although I could be
wrong since I have timed it) than this one-liner subroutine which does
**not** make use of a ReDim statement to accomplish the same end result....

Sub AddBlankElementsAtBeginning(YourArray() As String, _
Optional Number As Long = 1)
YourArray = Split(String$(Number, "`") & Join(YourArray, "`"), "`")
End Sub

Just pass your array into the subroutine along with an optionally specified
number of blank elements (default is one element) to add to the beginning of
the array. Note that I used the backward apostrophe character (found on the
same key as the tilde) for my delimiters as that character rarely ever
appears in any text; however, if you think that this character could ever
appear in one of your String array elements, simply substitute Chr$(1) for
the three appearances of "`" and all should work fine.

> Looking for a snippet to move string elements down in an array of > strings
> after additional empty elements have been added to the array e.g. > redim
> preserve x(ubound(x)+1).
> i.e. need to quickly insert using copymemory.


.



Relevant Pages

  • Re: Proper way to return a string
    ... PQresStatus returns a string version of the result status. ... Should I use a subroutine, I know how to easily deal with giving ... to consider array operations where the result is an array. ...
    (comp.lang.fortran)
  • Re: Proper way to return a string
    ... PQresStatus returns a string version of the result status. ... Should I use a subroutine, I know how to easily deal with giving ... to consider array operations where the result is an array. ... There are, of course, exceptions to the rules. ...
    (comp.lang.fortran)
  • Re: Proper way to return a string
    ... PQresStatus returns a string version of the result status. ... Should I use a subroutine, I know how to easily deal with giving ... to consider array operations where the result is an array. ...
    (comp.lang.fortran)
  • Re: LEN() of unallocated array
    ... > into a allocatable string array is going to work.. ... > To illustrate what you are doing wrong, I rewrote your subroutine ... > end program test ...
    (comp.lang.fortran)
  • Re: Proper way to return a string
    ... PQresStatus returns a string version of the result status. ... Should I use a subroutine, I know how to easily deal with giving ... to consider array operations where the result is an array. ...
    (comp.lang.fortran)