[SOLUCIONADO] Split

Iniciado por Miseryk, 19 Julio 2013, 09:28 AM

0 Miembros y 1 Visitante están viendo este tema.

Miseryk

Hola, estaba tratando de hacer algo mientras me topé con un problema, tal vez parezca tonto o quizá nunca necesité algo así.

Lo que quiero lograr, es encontrar la posición con respecto a un index de un Split.

Code:

Form:
Código (vb) [Seleccionar]

Option Explicit

Private Sub Form_Load()
Call ConfigArray

Call Stuff

End
End Sub



Module:
Código (vb) [Seleccionar]

Option Explicit

Public MyByteArray() As Byte
Public Const StrByteArray As String = "255,254,253,0,252,0,0,0,251,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,250,0,0,0,0,0,0," & _
                                     "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,249,0," & _
                                     "0,0,3,4,248,3,0,247,246,5,6,2,245,244,5,6,243,242,7,241,8,240,9,10,239,9,11,12,8,238,11," & _
                                     "13,13,10,237,8,236,14,8,9,235,13,8,7,13,8,7,234,233,8,12,10,232,14,231,15,15,230,229,0,0,0," & _
                                     "0,0,0,0,228,227,226,16,5,2,17,18,5,2,17,18,5,2,17,18,225,224,4,18,223,2,17,18,222,19,18," & _
                                     "18,20,2,17,18,21,22,221,18,220,2,17,18,21,22,219,18,23,2,17,18,5,2,218,18,16,2,17,18,21,22," & _
                                     "217,18,19,2,17,18,21,22,216,218,23,2,17,18,21,22,215,18,20,2,17"
Public MaxArray As Integer

Public Sub ConfigArray()
Dim i As Integer

MaxArray = UBound(Split(StrByteArray, ","))
ReDim MyByteArray(0 To MaxArray) As Byte

For i = 0 To MaxArray
   MyByteArray(i) = Split(StrByteArray, ",")(i)
Next i
End Sub

Public Sub Stuff()
Dim i As Integer
Dim CurrentByte As Byte
Dim found As Long
Dim CurrentPos As Long

For i = 0 To MaxArray
   If i = 5 Then
       'Ejemplo, index 5 -> 255,254,253,0,252,0,
       CurrentByte = MyByteArray(i)
       CurrentPos = Aca quiero obtener la posicion del index 5
   End If
Next i
End Sub


CurrentPos = Aca quiero obtener la posicion del index 5, que sería 19

Alguna idea o algún comando mágico que me retorne la posición según el index?
Can you see it?
The worst is over
The monsters in my head are scared of love
Fallen people listen up! It's never too late to change our luck
So, don't let them steal your light
Don't let them break your stride
There is light on the other side
And you'll see all the raindrops falling behind
Make it out tonight
it's a revolution

CL!!!

raul338

No entiendo de que queres la posicion:
La posición en el string del valor que esta en el array (index 1, pos 1; index 2, pos 4; index 3, pos 8; index 4, pos 12; index 5, pos 13...)
Si es así deberias sumar el length del string de cada uno de los items del array que vas recorriendo + el contador (la cantidad de separadores que pasaste)

Otra cosa, ConfigArray se puede optimizar mucho! Trata de usar una sola vez Split y trabaja con el array devuelto, ese for con el split adentro tarda mucho porque tiene hacer tantas veces split una y otra vez cuando ya lo hizo antes para obtener el ubound!
Ademas de que estas duplicando el array que te devuelve el split

Miseryk

#2
Muy cierto, no me había dado cuenta. Muchas gracias, de lo la longitud lo pude hacer, pero de otra manera a la que quería.

Código (vb) [Seleccionar]

Option Explicit

Private Sub Form_Load()
Call ConfigArray

Call Stuff

End
End Sub



Código (vb) [Seleccionar]

Option Explicit

Public MyByteArray() As Byte

Public Const StrByteArray As String = "255,254,253,0,252,0,0,0,251,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,250,0,0,0,0,0,0," & _
                                     "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,249,0," & _
                                     "0,0,3,4,248,3,0,247,246,5,6,2,245,244,5,6,243,242,7,241,8,240,9,10,239,9,11,12,8,238,11," & _
                                     "13,13,10,237,8,236,14,8,9,235,13,8,7,13,8,7,234,233,8,12,10,232,14,231,15,15,230,229,0,0,0," & _
                                     "0,0,0,0,228,227,226,16,5,2,17,18,5,2,17,18,5,2,17,18,225,224,4,18,223,2,17,18,222,19,18," & _
                                     "18,20,2,17,18,21,22,221,18,220,2,17,18,21,22,219,18,23,2,17,18,5,2,218,18,16,2,17,18,21,22," & _
                                     "217,18,19,2,17,18,21,22,216,218,23,2,17,18,21,22,215,18,20,2,17"
Public MaxArray As Integer

Public colObject As New Collection

Public Sub ConfigArray()
Dim i As Integer
Dim pos As Double
Dim newpos As Double
Dim SplitOneTime As Variant

SplitOneTime = Split(StrByteArray, ",")

MaxArray = UBound(SplitOneTime)

ReDim MyByteArray(0 To MaxArray) As Byte

pos = 1

For i = 0 To MaxArray
   MyByteArray(i) = SplitOneTime(i)
   If i = 0 Then
       colObject.Add Array((i + 1), CStr(1))
   Else
       newpos = InStr(pos, StrByteArray, ",") + 1
       colObject.Add Array((i + 1), CStr(newpos))
       pos = newpos
   End If
Next i
End Sub

Public Sub Stuff()
Dim i As Integer
Dim CurrentByte As Byte
Dim found As Double
Dim CurrentPos As Double
Dim NextPos As Double
Dim FirstPattern As String
Dim SecondPattern As String

For i = 0 To MaxArray
   'Ejemplo, index 5 -> 255,254,253,0,252,0,0,0,251,0,0,0,1,1,0,0,0
   CurrentByte = MyByteArray(i)
   If i = 103 Then
       Stop
       CurrentPos = colObject(i + 1)(1)
       NextPos = colObject(i + 2)(1)
       found = InStr(NextPos, StrByteArray, CurrentByte)
       
       FirstPattern = Mid(StrByteArray, CurrentPos, found - CurrentPos) '= "13,8,7,"
       SecondPattern = Mid(StrByteArray, found, found - CurrentPos) '= "13,8,7,"
       
       MsgBox StrComp(FirstPattern, SecondPattern) = 0
   End If
Next i
End Sub


Ahí obtuve el CurrentPos y el NextPos que quería para encontrar coincidencias con mid y found.

PD: pero no es lo que quería, no quería usar
Código (vb) [Seleccionar]
Public colObject As New Collection para obtener las posiciones en mid para el index del array o del split.
Can you see it?
The worst is over
The monsters in my head are scared of love
Fallen people listen up! It's never too late to change our luck
So, don't let them steal your light
Don't let them break your stride
There is light on the other side
And you'll see all the raindrops falling behind
Make it out tonight
it's a revolution

CL!!!

DarkMatrix

Y asi:

Código (vb) [Seleccionar]

Option Explicit

Public MyBytePos()        As Long
Public MyByteArray()      As Byte

Public Const StrByteArray As String = "255,254,253,0,252,0,0,0,251,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,250,0,0,0,0,0,0," & _
                                     "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,249,0," & _
                                     "0,0,3,4,248,3,0,247,246,5,6,2,245,244,5,6,243,242,7,241,8,240,9,10,239,9,11,12,8,238,11," & _
                                     "13,13,10,237,8,236,14,8,9,235,13,8,7,13,8,7,234,233,8,12,10,232,14,231,15,15,230,229,0,0,0," & _
                                     "0,0,0,0,228,227,226,16,5,2,17,18,5,2,17,18,5,2,17,18,225,224,4,18,223,2,17,18,222,19,18," & _
                                     "18,20,2,17,18,21,22,221,18,220,2,17,18,21,22,219,18,23,2,17,18,5,2,218,18,16,2,17,18,21,22," & _
                                     "217,18,19,2,17,18,21,22,216,218,23,2,17,18,21,22,215,18,20,2,17"
                                     
Public MaxArray           As Integer

Public Sub ConfigArray()

    Dim I              As Integer
    Dim Pos            As Double
    Dim NewPos         As Double
    Dim SplitOneTime() As String

    SplitOneTime = Split(StrByteArray, ",")

    MaxArray = UBound(SplitOneTime)
   
    ReDim MyBytePos(0 To MaxArray)
    ReDim MyByteArray(0 To MaxArray)

    Pos = 1

    For I = 0 To MaxArray

        MyByteArray(I) = CByte(SplitOneTime(I))
       
        If I = 0 Then

            MyBytePos(I) = 1

        Else
       
            MyBytePos(I) = (MyBytePos(I - 1) + Len(SplitOneTime(I - 1))) + 1
       
        End If

    Next I
   
    'MsgBox MyBytePos(5) ' = 19
    'MsgBox MyBytePos(40) ' = 93

End Sub

Public Sub Stuff()

    Dim I             As Integer
    Dim CurrentByte   As Byte
    Dim found         As Double
    Dim CurrentPos    As Double
    Dim NextPos       As Double
    Dim FirstPattern  As String
    Dim SecondPattern As String

    For I = 0 To MaxArray

        'Ejemplo, index 5 -> 255,254,253,0,252,0,0,0,251,0,0,0,1,1,0,0,0

        CurrentByte = MyByteArray(I)

        If I = 103 Then

            Stop
           
            CurrentPos = MyBytePos(I)

            NextPos = MyBytePos(I + 1)

            found = InStr(NextPos, StrByteArray, CurrentByte)

            FirstPattern = Mid(StrByteArray, CurrentPos, found - CurrentPos) '= "13,8,7,"
            SecondPattern = Mid(StrByteArray, found, found - CurrentPos) '= "13,8,7,"

            MsgBox StrComp(FirstPattern, SecondPattern) = 0

        End If

    Next I

End Sub

Todo aquello que no se puede hacer, es lo que no intentamos hacer.
Projecto Ani-Dimension Digital Duel Masters (Juego de cartas masivo multijugador online hecho en Visual Basic 6.0)

Desing by DarkMatrix

Miseryk

#4
También, pero andaba buscando algún método o algo del split
Código (vb) [Seleccionar]
SplitOneTime = Split(StrByteArray, ",") con el cual pueda obtener esa pocisión sin tener que declarar otra varible ya sea array o collección.
Can you see it?
The worst is over
The monsters in my head are scared of love
Fallen people listen up! It's never too late to change our luck
So, don't let them steal your light
Don't let them break your stride
There is light on the other side
And you'll see all the raindrops falling behind
Make it out tonight
it's a revolution

CL!!!

Miseryk

Aunque no sea la respuesta que quiero, me ayuda a resolver mi problema, es por éso que lo pongo como SOLUCIONADO y no como RESUELTO, si quieren aportar nuevas cosas, bienvenidos sean :D

Gracias a todos.
Can you see it?
The worst is over
The monsters in my head are scared of love
Fallen people listen up! It's never too late to change our luck
So, don't let them steal your light
Don't let them break your stride
There is light on the other side
And you'll see all the raindrops falling behind
Make it out tonight
it's a revolution

CL!!!

BlackZeroX

#6
Esto es mas fácil que un split... y te olvidas de convertir de string a byte o integer

Código (vb) [Seleccionar]


dim MyBytePos() as Byte ' Si los numeros en el Array son mayors usa Integer en lugar de byte y si son mayores usa long y si no pss Double....

MyBytePos = Array(255,254,253,0,252,0,0,0,251,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,250,0,0,0,0,0,0, _
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,249,0, _
0,0,3,4,248,3,0,247,246,5,6,2,245,244,5,6,243,242,7,241,8,240,9,10,239,9,11,12,8,238,11, _
13,13,10,237,8,236,14,8,9,235,13,8,7,13,8,7,234,233,8,12,10,232,14,231,15,15,230,229,0,0,0, _
0,0,0,0,228,227,226,16,5,2,17,18,5,2,17,18,5,2,17,18,225,224,4,18,223,2,17,18,222,19,18, _
18,20,2,17,18,21,22,221,18,220,2,17,18,21,22,219,18,23,2,17,18,5,2,218,18,16,2,17,18,21,22, _
217,18,19,2,17,18,21,22,216,218,23,2,17,18,21,22,215,18,20,2,17)

debug.print chr(34) & Join(days(), ",") & chr(34) ' Esta linea la puedes quitar sin problemas

debug.print "Posicion Numero 0:"; MyBytePos(0)
debug.print "Posicion Numero 5:"; MyBytePos(5)


Dulces Lunas!¡.
The Dark Shadow is my passion.