RE: ReDim Preserve code almost working



You might like my SegList function. It returns a segment of a list by
specifying beginning and ending elements. And, if the last arg is negative
it means element position from the end.

Examples:

NewList = SegList(OldList, 2, 5) ' returns the lesser of elements 2 to 5 or
elements 2 to the end

NewList = SegList(OldList, 3, -2) ' returns the third element to the
second-to-last element (unless the last element specified is to the left of
the first element specified)

NewList = SegList(OldList, 4) ' returns the fourth element to the end
(unless there are less than four elements)

(Any error will simply return an empty array)

NOTE: I always use Option Base 1 so you may have to modify it if necessary

SegList will, however, accept a zero-based array (because Option Base 1 does
not guarantee that functions such as Split return a 1-based array). In fact,
I have a Split1 function that utilizes Split and SegList to return a 1-based
array.

I included the necessary helper functions, but don't ask why I named them
ending with zero (it has to do with Fortran.)


Option Explicit
Option Base 1

Public Function SegList(List() As String, Optional iBeg As Long = 1,
Optional iEnd As Long = -1) As String()
'
' returns a segment of a list (string array) - accepts a zero-based array
as input but iBeg
' and iEnd refer to positions of the input array as if it were a one-based
array, i.e. if
' List() is zero-based then SegList(List, 1, 4) moves List(0 To 3) into
SegList(1 to 4)
'
Dim tmp() As String
Dim nMove As Long
Dim istElem As Long
Dim lstElem As Long
Dim ist As Long
Dim lst As Long
Dim i As Long
'
' get the actual first element number (0 or 1) and the last element number
as if 1-based
'
On Error GoTo NothingToMove
istElem = LBound(List)
lstElem = UBound(List) - istElem + 1
'
' get the first and last input element positions (0- or 1-based) and move
elements
' to the output array starting at position one
'
If iBeg <> 0 And iEnd <> 0 And iBeg <= lstElem Then
If iBeg < 0 Then
ist = Max0(lstElem + iBeg + 1, 1) + istElem - 1
Else
ist = iBeg + istElem - 1
End If
If iEnd < 0 Then
lst = lstElem + istElem + iEnd
Else
lst = Within0(iEnd, 1, lstElem) + istElem - 1
End If
nMove = lst - ist + 1
If nMove > 0 Then
ReDim tmp(nMove)
For i = 1 To nMove
tmp(i) = List(ist + i - 1)
Next i
SegList = tmp
End If
End If
'
NothingToMove:
'
End Function

Public Function Max0(Num1 As Long, Num2 As Long) As Long
'
' returns the largest of two long numbers
'
Max0 = IIf(Num1 > Num2, Num1, Num2)
'
End Function

Public Function Min0(Num1 As Long, Num2 As Long) As Long
'
' returns the smallest of two long numbers
'
Min0 = IIf(Num1 < Num2, Num1, Num2)
'
End Function

Public Function Within0(Num As Long, MinVal As Long, MaxVal As Long) As Long
'
' returns a long within a specified min-max range, i.e. if the number is
outside the
' range will return the min or max allowed (NOTE - MinVal MUST be less
than MaxVal)
'
Within0 = Min0(Max0(Num, MinVal), MaxVal)
'
End Function

Public Function Split1(txt As String, Optional Delimiter As String = " ", _
Optional iBeg As Long = 1, Optional iEnd As Long = -1) As String()
'
' splits a delimited string into a one-based string array
'
Dim tmp() As String
'
' Split returns a zero-based array (even when Option Base 1 is used) so
move the
' elements into a one-based array
'
tmp = Split(txt, Delimiter)
Split1 = SegList(tmp, iBeg, iEnd)
'
End Function


"canoe414@xxxxxxxxxxxxxxxxxxxxxx" wrote:

W2K Pro SP3
VB6 SP3

Greetings all,

I've built a function to snip the n leftmost elements out of an array

Function SnipLeft(ByRef myarry() As String, indx As Integer) As
String()
' deletes the first indx number of elements from the front of an
array
Dim vv As String
Dim i As Integer
Dim jjjj As Integer
jjjj = UBound(myarry)
vv = Join(myarry, "-")
For i = indx To jjjj
myarry(i - indx) = myarry(i)
Next i
vv = Join(myarry, "=")
ReDim Preserve myarry(jjjj - indx + 1)
vv = Join(myarry, "%")
SnipLeft = myarry
End Function

executing the following:
---
Dim tmpStr As String
Dim rresult As String
Dim words() As String
tmpStr = "hookem;horns"
words = Split( tmpStr, ";")
words = SnipLeft( words, 1)
rresult = Join( words, "=")
---
at this point rresult contains
"horns=horns"
not
"horns"
as I'd expect/hope/want

tracing thru the function makes me think the problem lies with the
ReDim Preserve myarry(jjjj - indx + 1)
statement -
it's not trimming off the 2nd element from the array

Any ideas/hints/code snippets are always appreciated.

TIA,

Steve


.



Relevant Pages

  • RE: Follow up question. Get value of closed file.
    ... You havve to access each member of the array ... Dim ReturnData as Variant ... SourceSheet As String, _ ... Dim rsData As Object ...
    (microsoft.public.excel.programming)
  • Re: Need help with SeriesCollection Object Please !
    ... Why did you use 4 Elements in your Array and then the use of the Exit ... Function GetSourceSheet(sFmla As String, sWSname, sFile As String) As ... Dim i As Long ... ran my code on certain other charts sometimes it would return ...
    (microsoft.public.excel.programming)
  • Re: Searching for best matches in string query
    ... looped through my recordset as an array counting the matches. ... 'Receives an array to be searched and the string value containing all the ... Dim arrData() ' As String ... Dim iLeftcur, iRightCur, iPivot, iTemp As Long ...
    (microsoft.public.access.queries)
  • Re: get linenumber when string found in VBE codemodule
    ... Dim lStartRow As Long ... > if there was a direct way to get the line number where string was found> first. ... A Long specifying the column at which you want to start>> the ... the target string is a>> regular expression pattern. ...
    (microsoft.public.excel.programming)
  • Re: get linenumber when string found in VBE codemodule
    ... value in one of the arguments, namely startline. ... Dim vbMod As CodeModule ... >if there was a direct way to get the line number where string was found ... A Long specifying the column at which you want to start ...
    (microsoft.public.excel.programming)