Re: splitting data stream

2005-08-18 Thread Jeff 'japhy' Pinyan

On Aug 18, Octavian Rasnita said:


I want to use regular expressions (or other techniques) to split a data
stream which has the following format:

...

The  is a 4 digit number and tells the size of the next
body it should get.


You can roll your own with substr() and what-not, or you can use the 
unpack() function to do it for you:


  my @chunks = unpack "(A4/A)*", $buffer;

The A4/A means "read four characters and get that many characters"; the 
(...)* around that means "do this repeatedly.


--
Jeff "japhy" Pinyan %  How can we ever be the sold short or
RPI Acacia Brother #734 %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %-- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: splitting data stream

2005-08-18 Thread Moon, John
From: Octavian Rasnita [mailto:[EMAIL PROTECTED] 
Sent: Thursday, August 18, 2005 8:04 AM
To: beginners perl
Subject: splitting data stream

Hi,

I want to use regular expressions (or other techniques) to split a data
stream which has the following format:

...

The  is a 4 digit number and tells the size of the next
body it should get.

Thank you very much.

Teddy


It's not clear from your note exactly what the data stream looks like... 
Is it delimited between fields? Are the fields in a "record" and is the
record delimited? Can you "dump" some of the data and include it in a post?

But given the above...

perl -e'
$a=q{0010testtest1X0005testX0001A0003BBX0006CX};
while (length($a)) {
$size=substr($a,0,4);
$body[$i]=substr($a,4,$size);
$a=substr($a,4+$size);
$i++
}
print "$_\n" foreach (@body);
'

DEV,SUN2>
testtest1X
testX
A
BBX
CX
DEV,SUN2>
jwm

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




splitting data stream

2005-08-18 Thread Octavian Rasnita
Hi,

I want to use regular expressions (or other techniques) to split a data
stream which has the following format:

...

The  is a 4 digit number and tells the size of the next
body it should get.

Thank you very much.

Teddy


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]