-
Aug 28th, 2014, 12:41 AM
#1
Thread Starter
Lively Member
parsing again with two splits
ok i was shown how to parse a string with 1 identifying split of vbcrlf
but what if it does like this
Select Case Asc(Mid(buffer, 2, 1))
Case 54
Dim uid() As String, uidstr As String
uidstr = Split(buffer2, "uid=")(1)
uid = Split(uidstr, "nickname=")
For i = 0 To UBound(uid) - 1
debug.print uid(i)
Next i
End Select
the incoming data contains multiple uid's so i need to make it go through it all the way ive been shown before
was just one split this needs 2 and i cant seem to make it go through all data .....grant it it gets the first uid so i guess i need to make it idk if its array like above or how help appreciated...
-
Aug 28th, 2014, 01:06 AM
#2
Re: parsing again with two splits
Use code tags when posting code to retain formatting
Is that supposed to be buffer2 on the 4th line or is that a mistake?
Since you did not tell us what data you are parsing or what you are trying to get from it there is not much to say beyond that you probably need a loop that is not there rather than just referencing the 2nd element in the array returned by the first split but then again no idea what is be parsed or what you want from it.
-
Aug 28th, 2014, 07:44 AM
#3
Re: parsing again with two splits
Can you post the incoming data? That would help us understand better. And, kinda looking for what you are trying to find....is it all UIDs and all NickNames? Just wondering.
-
Aug 28th, 2014, 01:06 PM
#4
Thread Starter
Lively Member
Re: parsing again with two splits
this is my code
Code:
Select Case Asc(Mid(buffer, 2, 1))
Case 54
Dim uid() As String, username() As String, user As String, uidstr As String
na = 0
'user = Split(buffer2, "nickname=")(1)
'username = Split(user, "admin=")(0)''''''''this is the nickname
uidstr = Split(buffer, "uid=")(1)
uid = Split(uidstr, "nickname=")(0)'''''this is the uid
For i = 0 To UBound(uid) - 1
uid(na) = uid(i) ' uid
Debug.Print uid(na)
Debug.Print username(na)
Next i
End Select
this is the data incoming is somthing to this it has more data in the beggining but is all not wanted so i wanna parse all incoming uid's and nicknames
.6...J..Ú5.... (Official Page).T...°group_id=55861
uid=90287005
nickname=sexytina105
admin=1
color=000000000
mic=1
pub=N
away=0
ct=2.group_id=55861
uid=49737864
nickname=PottyMouf
admin=0
color=000000000
mic=1
pub=N
away=0
ct=2.group_id=55861
uid=93606609
nickname=Tnt_on
admin=0
color=000128000
mic=1
pub=N
away=0
ct=2.group_id=55861
uid=84972933
nickname=bite_me_666
admin=0
color=000128000
mic=1
pub=N
away=0
ct=2.group_id=55861
uid=84850151
nickname=juicygurl1971
admin=0
color=153000255
mic=1
pub=N
away=0
vg=2
ct=2.group_id=55861
[/CODE]
-
Aug 28th, 2014, 02:16 PM
#5
Re: parsing again with two splits
Given that I would probably use a single split() and the vbcrlf to get the lines broken out into an array.
I would then loop through that array and check for the existence of uid= and nickname= then extract the data from that line after the tag in question
Something like this for example
Code:
Dim ArrayOfLines() As String
ArrayOfLines = Split(DataStream, vbCrLf)
Dim X As Integer
For X = 0 To UBound(ArrayOfLines)
If Left$(ArrayOfLines(X), 4) = "uid=" Then
Debug.Print "User ID=" & Mid$(ArrayOfLines(X), 5)
ElseIf Left$(ArrayOfLines(X), 9) = "nickname=" Then
Debug.Print "Nickname=" & Mid$(ArrayOfLines(X), 10)
End If
Next
Given the data above as input the output in the debug window would be
User ID=90287005
Nickname=sexytina105
User ID=49737864
Nickname=PottyMouf
User ID=93606609
Nickname=Tnt_on
User ID=84972933
Nickname=bite_me_666
User ID=84850151
Nickname=juicygurl1971
-
Aug 28th, 2014, 07:19 PM
#6
Thread Starter
Lively Member
Re: parsing again with two splits
ok that works for what i need but i need to know how to buffer this properly with a terminator of "eof=Y."
this is the code with mod i use that adds to the treeview properly
Code:
Private Sub Winsock2_DataArrival(ByVal bytesTotal As Long)
On Error Resume Next
Dim data As String
Static buffer2 As String
Winsock2.GetData data, vbString
buffer2 = buffer2 & data
Select Case Asc(Mid(buffer2, 2, 1))
Case 54
Dim ArrayOfLines() As String
ArrayOfLines = Split(buffer2, Chr(&HA))
Dim X As Integer
For X = 0 To UBound(ArrayOfLines)
If Left$(ArrayOfLines(X), 4) = "uid=" Then
Set Item = Form3.TreeView1.Nodes.Add(, , , Mid$(ArrayOfLines(X + 1), 10), 1)
Item.Key = LongToHex(Mid$(ArrayOfLines(X), 5))
Debug.Print "UID=" & Mid$(ArrayOfLines(X), 5)
ElseIf Left$(ArrayOfLines(X), 9) = "nickname=" Then
Debug.Print "Nickname=" & Mid$(ArrayOfLines(X), 10)
End If
Next
End Select
-
Aug 28th, 2014, 07:22 PM
#7
Thread Starter
Lively Member
Re: parsing again with two splits
yes with what ive done i can now remove the elseif i just havent done it yet ...but to buffer with a terminator of "eof=Y."
how would i implement this or how would you should i say...
-
Aug 28th, 2014, 07:27 PM
#8
Thread Starter
Lively Member
Re: parsing again with two splits
the eof=Y. is the last 5 bytes sent at the end of the data needing buffered
-
Aug 28th, 2014, 07:30 PM
#9
Re: parsing again with two splits
Basically you add any data that comes in to your buffer
Once you have added data to the buffer you could use INSTR() to test for the existence of those characters and the location of them if they exist.
If it is there then you can pull data out of the buffer up to that location and process it
You will then reset the buffer in such a way that you remove the data that has been processed but leave anything that may be there after the eof=y so it can be processed later if needed.
You will need INSTR(), and possibly Left$() and Mid$() to work with the data, you can find examples for these in your online help or a quick google search if needed.
General concept
Code:
Buffer=Buffer & NewData
Location=Instr(Buffer,"eof=y")
If Location >0 Then
DataToProcess=Left$(buffer,Location)
Buffer=Mid$(Buffer,location+5)
'process the data in the DataToProcess var
End If
You may need to adjust the second parameter in those two lines above +/-1 to get the correct result for the given location. I haven't tested it and sometimes get confused on the offset when doing this.
Last edited by DataMiser; Aug 28th, 2014 at 07:36 PM.
-
Aug 28th, 2014, 08:05 PM
#10
Thread Starter
Lively Member
Re: parsing again with two splits
so i am thinking somthing like this may work i think i have it reading the buffer2 untill it recieves the "eof=Y" then process the buffer is this close due to the fact the terminator is not needed in my parse i excluded the location + 5 part of the code.....am i close to what you would have done i havent tried it yet but i am going to now...
Code:
Private Sub Winsock2_DataArrival(ByVal bytesTotal As Long)
On Error Resume Next
Dim data As String
Static buffer2 As String, location As String
Winsock2.GetData data, vbString
buffer2 = buffer2 & data
location = InStr(buffer2, "eof=Y")
If location > 0 Then
buffer2 = Left$(buffer2, location)
'buffer2 = Mid$(buffer2, location + 5)
'process the data in the DataToProcess var
Select Case Asc(Mid(buffer2, 2, 1))
Case 54
Dim ArrayOfLines() As String
ArrayOfLines = Split(buffer2, Chr(&HA))
Dim X As Integer
For X = 0 To UBound(ArrayOfLines)
If Left$(ArrayOfLines(X), 4) = "uid=" Then
Set Item = Form3.TreeView1.Nodes.Add(, , , Mid$(ArrayOfLines(X + 1), 10), 1)
Item.Key = LongToHex(Mid$(ArrayOfLines(X), 5))
End If
Next
End Select
End If
-
Aug 28th, 2014, 08:42 PM
#11
Thread Starter
Lively Member
Re: parsing again with two splits
ok that wont work at all and how can i clear a statis buffer and when i say clear all....
-
Aug 28th, 2014, 09:13 PM
#12
Re: parsing again with two splits
You changed the var that would hold the data to be processed to use the buffer instead and commented out the part that would remove the processed data from the buffer so it would process the same data everytime data comes in.
Code:
buffer2 = Left$(buffer2, location)
'buffer2 = Mid$(buffer2, location + 5)
the first line should pull the data up to the eof tag and place it in a different variable. This variable does not need to be static and it is what would be processed by the parse.
the second line is there to remove the processed data from the buffer but leave anything that follows the eof tag so it can be processed later.
If there is no chance of getting data for another transaction after the eof part then you don't need that part of the logic [i.e. Those two lines] , instead you would just process the data in the buffer and then clear the buffer using buffer2=""
As for clearing the Static buffer that is a local var so it has to be done within the procedure where it exists. If you need to access it elsewhere within the same form then it needs to be defined at a different level such as in the form declarations section, or if you need to access it from another form or from a module then it should be declared as public and in a module.
-
Aug 28th, 2014, 09:18 PM
#13
Re: parsing again with two splits
Another note is that you really should not be using a select case here
Code:
Select Case Asc(Mid(buffer2, 2, 1))
Case 54
There is only one case being processed so an If statement would make more sense
Code:
If Asc(Mid(buffer2, 2, 1))=54 then
or better still drop the ASC() function as it is not needed and add a $ on the mid statement to force it to use a string rather than a variant. This would be a little faster than either of the other two methods and easier to read
Code:
If Mid$(buffer2, 2, 1)="6" then
-
Aug 29th, 2014, 12:03 AM
#14
Thread Starter
Lively Member
Gotcha
ok bro now i get it .... the first is everything left of eof=Y tag and contrary to what i thought before now i see what you did the + 5 was after my parse to set the buffer to still store the remaining data i must have read it wrong or was scatter minded cuz now that you said that its obvious buffer2 = mid$(location, +5) is the remaining data saved in buffer after i use what i want of the left of eof i gotcha i do have a stupid ? tho i got it to work without static as you say i dont need it here .. but for my future ref i tried to declare it in the form declarations but i couldnt i mean im sure if i tried a few diff ways but i tried private static buffer2 as string, static buffer2 as string and all were invalid how do you declare it in form or module i fixed this issue but i wanna know so i dont need to ask later...
-
Aug 29th, 2014, 02:11 AM
#15
Re: parsing again with two splits
Static is only used in a procedure and only for local vars
When you dim at the form level you would generally use Private or simply Dim which is private. In some cases you may use Public.
The reason for Static in a sub or function is that it will retain the value when called again where as non Static vars are throw away when the sub/function completes.
Form level vars are alive and retain their value so long as the form is loaded. Module level vars hold their value so long as the program is running.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|