parse tr socket $buf

D

dogdog

I've created some simple socket code to send data to a server and
then read back whats sent back. Something like this...I dont have
it in front of me so forgive on the errors.. i'm doing this from
memory.


read($socket,$buf,100);

What I need to do is somehow tr that $buf line. It gives back
soemthing like this

xxx 123456

I just want the 123456 portion to print to my webpage. How can
I do this in perl and embed it in my script after the read($socket.....

tia
dogdog
 
T

Tad McClellan

read($socket,$buf,100);


Wouldn't you like to know if you actually got what you asked for?


die "couldn't read 100 bytes $!" unless read($socket,$buf,100) == 100;

What I need to do is somehow tr that $buf line. It gives back
soemthing like this

xxx 123456

I just want the 123456 portion to print to my webpage.


Then you DON'T want to "tr" it, because tr doesn't do what you want done,
it *tr*ansliterates characters.

You have neglected to tell us what it is about the 123456 portion
that indicates that it is the part you want.

All of the code below gives you the "123456" portion, but probably
won't work with your Real Data...


( all untested )

Delete everything up to the first space character?

s/[^ ]+ //;
or
s/.+? //;

Delete leading "x"s and spaces?

s/^[ x]+//;

Delete everything up to the first "1" character?

s/[^1]+//;

Delete everthing up to the first digit character?

s/\D+//;

Delete the first 4 characters?

s/....//;
or
substr $_, 0, 4 = '';

Delete everything except the last 6 characters?

substr $_, 0, length()-6;

The most simple way would be to just print what you want printed:

print '123456';



Your specification sucks, so my implementation sucks to match.

corollary: spend a bit of effort in devising the problem specification.
 
T

Tassilo v. Parseval

Also sprach Tad McClellan:
Wouldn't you like to know if you actually got what you asked for?


die "couldn't read 100 bytes $!" unless read($socket,$buf,100) == 100;

This isn't quite right. read() may return fewer bytes than specified (in
that it behaves just like the read(2) system call). That is not an error,
it just means that fewer bytes were available. A return value of zero
means end of file.

One would have to check for definedness. But then read() is one of those
functions where I rarely, if ever, check for errrors. The only one I
ever got was EIO and I am not even sure that can happen with a socket.

Tassilo
 
D

dogdog

Also sprach Tad McClellan:


This isn't quite right. read() may return fewer bytes than specified (in
that it behaves just like the read(2) system call). That is not an error,
it just means that fewer bytes were available. A return value of zero
means end of file.

One would have to check for definedness. But then read() is one of those
functions where I rarely, if ever, check for errrors. The only one I
ever got was EIO and I am not even sure that can happen with a socket.

Tassilo

I can understand the concerns however I'm using ethereal and I'm
seeing the correct amount of bytes returning. I can also currently
post it using
print "Content-type: text/html";
So I guess the answer to this is that I know I'm getting the
right data back, I just am not sure how I can use tr in the
script before posting the data to the webpage. I would like
to remove the XXX before the 123456 and just post the 123456
to the webpage.

thanks for the input
dogdog
 
B

Bart Lateur

I can understand the concerns however I'm using ethereal and I'm
seeing the correct amount of bytes returning. I can also currently
post it using
print "Content-type: text/html";
So I guess the answer to this is that I know I'm getting the
right data back, I just am not sure how I can use tr in the
script before posting the data to the webpage. I would like
to remove the XXX before the 123456 and just post the 123456
to the webpage.

You don't want to use tr, believe me. Use a regex. Describe exactly what
kind of data you want to extract, and make a pattern out of it. For now,
it would seem to me that you want a substring of consecutive digits:

my($digits) = $buf =~ /(\d+)/;

$digits now holds that number.

For example:

my $buf = "XXX 123456 lalalala";
my($digits) = $buf =~ /(\d+)/;
print $digits;
 
D

dogdog

You don't want to use tr, believe me. Use a regex. Describe exactly what
kind of data you want to extract, and make a pattern out of it. For now,
it would seem to me that you want a substring of consecutive digits:

my($digits) = $buf =~ /(\d+)/;

$digits now holds that number.

For example:

my $buf = "XXX 123456 lalalala";
my($digits) = $buf =~ /(\d+)/;
print $digits;
Bart,

Thank you greatly .. this should work and is exactly what I was
looking for and to do.

thanks again
dogdog
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top