|
|
| Author |
Message |
< Erlang ~ Stupid newbie questions: sockets and text processing |
| mwm |
Posted: Tue Apr 14, 2009 12:54 am |
|
|
|
Joined: 14 Apr 2009
Posts: 2
|
I have a project for a bot which connects to a shared virtual space. I have working bots in perl, but I am interested in converting at least one of them to erlang.
I have found many nice features which speed development (pattern matching is super-keen) and make substantial improvements to my old code. However, I have also run into typical new guy problems.
At this point, I have 2 major ones on which I would like some guidance/pointers.
It appears that when I receive a line from the socket, it is terminated with a \r\n. (?) As I tokenize and break out each piece, I am having trouble removing the \r. lib:nonl works as advertised, but I seem to be continuing to have the \r appended to the end of mystring.
I used re:replace to remove 'any' newline before parsing. Once I did this, the guards on my line parsing function:
parse_line(Sock, ["#"++J,"+"++F, "&"++U| Nonsense]) ->
stopped working and everything fell through to the default case.
If I merely use lib:nonl, there is still a \r (or other "newline" character) appended to the end of Nonsense.
Without regard to that, I am basically implementing a weirdo RPC with this bot.. lines of the form
#job_num# +function_to_call_ &user_id& `arg1`arg2`arg3[...]
While I created this format, for legacy reasons, I don't want to change it. the second problem has to do with tokenizing the args list.
[FirstArg|Rest] = string:tokens(string:strip(Nonsense), "`"),
This simply doesn't work. I have used re:split with limited success, but the "trim" option doesn't appear to work. Is ` a special character? I tried escaping it "\'" with no better result.
I realize these are simplistic questions, but any help provided would be appreciated.
M |
|
|
| Back to top |
|
| rvirding |
Posted: Sun Apr 19, 2009 8:48 pm |
|
|
|
User
Joined: 30 Aug 2006
Posts: 452
Location: Stockholm, Sweden
|
Could you send an example of an input line before/after trying to remove \r\n with different means. Also show the call to re:replace.
Also your match line
parse_line(Sock, ["#"++J,"+"++F, "&"++U| Nonsense]) ->
does not match one string but actually a list of strings: the first starting with #, the second with +, etc. Is this what you really want it to do? It does not seem like that from your description. |
|
|
| Back to top |
|
| mwm |
Posted: Sun Apr 19, 2009 9:03 pm |
|
|
|
Joined: 14 Apr 2009
Posts: 2
|
rvirding wrote: Could you send an example of an input line before/after trying to remove \r\n with different means. Also show the call to re:replace.
Also your match line
parse_line(Sock, ["#"++J,"+"++F, "&"++U| Nonsense]) ->
does not match one string but actually a list of strings: the first starting with #, the second with +, etc. Is this what you really want it to do? It does not seem like that from your description.
Thanks for your reply. I've actually gotten all of the bits working in the time since this post. It turns out that my lines were CR/LF/CR terminated.. (examining the list of integers which comprised my string made this easy.) I have no idea why. Once I got beyond that everything else sorted itself out. |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|