Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Parsing data from a data feed

by Apterigo (Scribe)
on Jul 19, 2001 at 00:01 UTC ( #97866=perlquestion: print w/replies, xml ) Need Help??

Apterigo has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I have a question I have been kicking around for a few days, and I'm baffled. Here is my problem, I will try to explain the problem as indepth as I possible can. First off, I am reading data from a constant data stream, and therefore I need to parse the data as it codes through, byte by byte. The data comes in, in a form such as this:
14|XXX\XXX|1.2541|1.2546|0|2001-07-17 10:25:22.17 0x00 16|XXX\XXX|1.2 +547|1.2552|0|2001-07-17 10:26:23.23 0x00
and on and on. The 0x00 is there to mean that each "string" is seperated by hex 00. What I am trying to do is read all the data from each "string" into one variable and stop reading when 0x00 is reached. Now, for some reason, I am having a heck of a time getting it to work. Here is my code (please note that this is only a portion, obviously):
do { $socket->recv($response, 1); $data = $data . $response; } until ($response == hex(0x00)); print "$data\n"; close($socket) || die "Error terminating socket connection...\n";


Now, basically what happens is when reading if the code hits either 0 | , . - , it interprets this as hex 0x00 and terminates. Any insights?


Thanks, Apterigo

Edit ar0n 2001-07-18

Replies are listed 'Best First'.
Re: Parsing data from a data feed
by bikeNomad (Priest) on Jul 19, 2001 at 00:09 UTC
    You don't want to compare the characters with hex(0x00), because this is just a 0. Comparing arbitrary strings with 0 won't work, since non-numeric ones will evaluate to 0. You want to say something like  $response == "\x00"

    Beyond that little problem, you're not checking the status in the recv call.

    You might try instead letting the getline call work and get your data a record at a time, by setting the input record separator $/ to NUL:

    local $/ = "\x00"; $response = $socket->getline; if ($socket->eof) { ... }

    update: you may have been thinking about chr(0) which returns "\x00".

Re: Parsing data from a data feed
by Apterigo (Scribe) on Jul 19, 2001 at 00:33 UTC
    Well, this is what i had to change it to, now it works perfect:
    do { $socket->recv($response, 1); $data = $data . response; } until ($response eq "\0x00");
    As far as the rest is concerned, I cannot read using getline I think, and I cannot read til EOF because there is no EOF, as I said the datafeed goes on forever. You may be right though, I'll check into that.
    Thanks Tons!
    Apterigo
      I don't know why getline wouldn't return a record at a time if you set $/ to "\x00". The advantage of doing it this way is that you should burn less CPU time, because the low-level (C) code is actually handling the incoming characters and you get full records. Generally, the less times you can manage to go through a loop, the better. So your get-a-record routine would be just:

      my $response; { local $/ = chr(0); # or "\x00" $response = $socket->getline }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://97866]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-08-18 20:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?