Newsgroup: it.comp.lang.visual-basic
Titolo: Split

Salve,
sto utilizzando VB5 e vorrei mettere il contenuto di un file di testo in
un array in modo tale che ogni riga del file sia un elemento dell'array.
Ho scritto il seguente codice:
filenum% = FreeFile
nomefile = "d:\percorso_del_mio_file\file.txt"
Open nomefile For Input As #filenum%
s = split(input$(LOF(filenum), #filenum), vbCrLf)
Close #filenum%
ma sembra che la funzione split non esista nella versione 5 enterprise.
E' possibilie?!?!?!?!?
Se si, c'e' una funzione simile?!?!?!?
Grazie
Donatella :)
-- 
Posted from root@[195.120.213.3] 
via Mailgate.ORG Server - 
http://www.Mailgate.ORG

 ma sembra che la funzione split non esista nella versione 5 enterprise.
Infatti. Comunque puoi usare questa, che non ricordo dove ho trovato :-(
(Sicuramente lo saprà Pal :-))  )
Attenzione alle righe che vanno a capo...
______T_A_G_L_I_A_R_E__Q_U_I__________
Public Function SplitPublic Function Split(ByVal Expression As String, Optional Delimiter As String = " ", _
                      Optional Limit As Long = -1, _
                      Optional Compare As VbCompareMethod = vbBinaryCompare) As Variant
'===============================================================================
'   Split - This function mimics the VBA6.Split function which is not
'   available in VB5. This function helps VB5 & VB6 projects co-exist.
'
'   NOTE: Anyone making adjustments to this function should keep in
'   mind that it is intended to behave EXACTLY like the VBA6.Split
'   function (flaws and all, including operands).
'
'   WARNING: Like the VB6.Split function, do not use Split for
'   parsing a comma delimited string, as it does not conform to
'   the rules that govern the parsing of the comma delimited
'   file format.
'
'   Expression      String to parse.
'   Delimiter       Character to delimit fields.
'   Limit
'   Expression      Required. String expression containing substrings and
'                   delimiters. If expression is a zero-length string(""),
'                   Split returns an empty array, that is, an array with no
'                   elements and no data.
'   Delimiter       Optional. String character used to identify substring
'                   limits. If omitted, the space character (" ") is
'                   assumed to be the delimiter. If delimiter is a zero-length
'                   string, a single-element array containing the entire
'                   expression string is returned.
'   Limit           Optional. Number of substrings to be returned; A value
'                   of -1 (default) indicates that all substrings are returned.
'   Compare         Optional. Numeric value indicating the kind of comparison
'                   to use when evaluating substrings. See Settings section
'                   for values.
'
'   RETURNS         A Variant() array of substrings.
'===============================================================================
Const AvgSize   As Integer = 10 ' fudge factor: average field size
Dim ArrayIncr   As Long         ' array resizing increment
Dim j           As Long         ' work subscript
Dim k           As Long         ' work subscript
Dim StrPos()    As Long         ' string map: String field position
Dim StrLen()    As Long         ' string map: String field length
Dim ExprLen     As Long         ' length of the supplied string
Dim nElements   As Long         ' # of elements parsed
Dim RetArray    As Variant      ' not very efficient but the only way to RETURN a dynamic array in VB5
    Split = Empty                                   ' by default return a value of type Empty
    ExprLen = Len(Expression)                       ' save this for performance
    If ExprLen = 0 Then Exit Function               ' exit if nothing to parse
    If StrComp(Right$(Expression, Len(Delimiter)), _
       Delimiter, Compare) = 0 Then                 ' does the string have a trailing delimiter?
        Expression = Left$ _
            (Expression, ExprLen - Len(Delimiter))  ' strip trailing delimiter
        ExprLen = ExprLen - Len(Delimiter)          ' track new string length
    End If
    ArrayIncr = ExprLen / AvgSize                   ' make a guess of how many elements needed
    If ArrayIncr < 10 Then ArrayIncr = 20           ' size array at least 20 elements at a time
    Select Case Limit                               ' a limit has been placed on the # of elements
    Case Is > 0                                     ' a limit supplied
        ' all is cool
    Case 0, Is < -1                                 ' no elements allowed
        Exit Function                               ' we're done
    End Select
    If Len(Delimiter) = 0 Then                      ' delimiter cleared
        Split = Array(Expression)                   ' return a single element array
    End If
    ReDim StrPos(1 To ArrayIncr)                    ' initialize the array for looping
    ReDim StrLen(1 To ArrayIncr)                    ' initialize the array for looping
    k = 1                                           ' start parsing from position 1
    j = InStr(k, Expression, Delimiter)             ' locate the first delimiter
    If j = 0 Then                                   ' nothing found
        Split = Array(Expression)                   ' return a single element array
        Exit Function                               ' we're done
    Else                                            ' first element found
        While j > 0                                             ' loop until nothing left to parse
            nElements = nElements + 1                           ' track # of elements thus far
            StrPos(nElements) = k                               ' track the start of the read
            StrLen(nElements) = j - k                           ' track the # of characters to read
            k = j + Len(Delimiter)                              ' track next position
            j = InStr(k, Expression, Delimiter)                 ' get next position
            If nElements = UBound(StrPos) Then                  ' time to resize the array
                ReDim Preserve _
                    StrPos(1 To UBound(StrPos) + ArrayIncr)     ' resize the array & make room
                ReDim Preserve _
                    StrLen(1 To UBound(StrLen) + ArrayIncr)     ' resize the array & make room
            End If
        Wend
        ReDim RetArray(nElements)                               ' size the array to fit
        For j = 1 To nElements                                  ' loop thru each element
            RetArray(j) = _
                Mid$(Expression, StrPos(j), StrLen(j))          ' copy next substring into the array
        Next j
        Split = RetArray                                        ' return the array
    End If
End Function
______F_I_N_E___T_A_G_L_I_O________
Bye, G.
--
---------------------------------------------
    Le risposte alle tue domande le trovi
    sul Sito Comune di questo NewsGroup:
 
http://www.murialdo.it/it_lang_vb
       ... e nell'help in linea !
---------------------------------------------
Real E-address: 
gaet...@hotmail.com

 (Sicuramente lo saprą Pal :-))  )
il tuo uso degli avverbi si va facendo sempre piu' spregiudicato.
"sicuramente" ?
se punti su un cavallo dimmelo.
ne scelgo un altro.
:)

 Se si, c'e' una funzione simile?!?!?!?
VB 6 ha alcune nuove funzioni su stringa che VB5 non ha.
Cmq c'è un articolo su MSDN (ID: Q188007 )che riporta il codice
necessario per emulare dette funzioni in VB5.
Per comodità te lo riporto qui per intero, buon taglia&incolla ^_-
Byee
Zeross
========================================
      Option Explicit
      Public Function Join(source() As String, Optional _
            sDelim As String = " ") As String
      Dim sOut As String, iC As Integer
      On Error GoTo errh:
          For iC = LBound(source) To UBound(source) - 1
              sOut = sOut & source(iC) & sDelim
          Next
          sOut = sOut & source(iC)
          Join = sOut
          Exit Function
      errh:
          Err.Raise Err.Number
      End Function
      Public Function Split(ByVal sIn As String, Optional sDelim As _
            String, Optional nLimit As Long = -1, Optional bCompare As _
             VbCompareMethod = vbBinaryCompare) As Variant
          Dim sRead As String, sOut() As String, nC As Integer
          If sDelim = "" Then
              Split = sIn
          End If
          sRead = ReadUntil(sIn, sDelim, bCompare)
          Do
              ReDim Preserve sOut(nC)
              sOut(nC) = sRead
              nC = nC + 1
              If nLimit <> -1 And nC >= nLimit Then Exit Do
              sRead = ReadUntil(sIn, sDelim)
          Loop While sRead <> ""
          ReDim Preserve sOut(nC)
          sOut(nC) = sIn
          Split = sOut
      End Function
      Public Function ReadUntil(ByRef sIn As String, _
            sDelim As String, Optional bCompare As VbCompareMethod _
          = vbBinaryCompare) As String
          Dim nPos As String
          nPos = InStr(1, sIn, sDelim, bCompare)
          If nPos > 0 Then
              ReadUntil = Left(sIn, nPos - 1)
              sIn = Mid(sIn, nPos + Len(sDelim))
          End If
      End Function
      Public Function StrReverse(ByVal sIn As String) As String
          Dim nC As Integer, sOut As String
          For nC = Len(sIn) To 1 Step -1
          sOut = sOut & Mid(sIn, nC, 1)
          Next
          StrReverse = sOut
      End Function
      Public Function InStrRev(ByVal sIn As String, sFind As String, _
       Optional nStart As Long = 1, Optional bCompare As _
            VbCompareMethod = vbBinaryCompare) As Long
          Dim nPos As Long
          sIn = StrReverse(sIn)
          sFind = StrReverse(sFind)
          nPos = InStr(nStart, sIn, sFind, bCompare)
          If nPos = 0 Then
              InStrRev = 0
          Else
              InStrRev = Len(sIn) - nPos - Len(sFind) + 2
          End If
      End Function
      Public Function Replace(sIn As String, sFind As String, _
            sReplace As String, Optional nStart As Long = 1, _
            Optional nCount As Long = -1, Optional bCompare As _
            VbCompareMethod = vbBinaryCompare) As String
          Dim nC As Long, nPos As Integer, sOut As String
          sOut = sIn
          nPos = InStr(nStart, sOut, sFind, bCompare)
          If nPos = 0 Then GoTo EndFn:
          Do
              nC = nC + 1
              sOut = Left(sOut, nPos - 1) & sReplace & _
                 Mid(sOut, nPos + Len(sFind))
              If nCount <> -1 And nC >= nCount Then Exit Do
              nPos = InStr(nStart, sOut, sFind, bCompare)
          Loop While nPos > 0
      EndFn:
          Replace = sOut
      End Function

 Se si, c'e' una funzione simile?!?!?!?
No. Una funzione simile a "split" potresti facilmente costruirtela (sarebbe
anche un buon esercizio: consulta la guida alla voce "InStr") ma nel caso
particolare penso sia piu` conveniente leggere il file record per record ed
assegnare ogni record ad un elemento dell'array.
Se cerchi fra gli altri post che ho inviato adesso dovresti trovare un
esempio che fa al caso tuo. (Thread: "Leggere linea di un file").
--
Ciao.
Maurizio
--------
?SPQR(C)
 X
--------