Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How to skip first n character from data and read every m character..

by Perlbotics (Archbishop)
on Aug 16, 2008 at 15:45 UTC ( [id://704698]=note: print w/replies, xml ) Need Help??


in reply to How to skip first n character from data and read every m character..

I assume, that you want to strip off a fixed size header first and then process a list of records having another fixed size. You can set the input record separator ($/) to a scalar reference holding the record size (see perlvar). Might be slower than (sys)read, though.

#!/usr/bin/perl use strict; use Data::Dumper; my ($n, $m) = (9, 3); # var-names as requested my @result; $/ = \$n; # record size := 9 characters $result[0] = <DATA>; # read one record (size: 9) $/ = \$m; # record size := 3 charcters while (<DATA>) { # do something with the next block of 3 chars last if length != $m; # stop at first non m-sized record push @result, $_; } print Dumper \@result; __END__ 123456789111222333444555666777888999xxxyyyzzzZ
This prints:
$VAR1 = [ '123456789', '111', ... 'zzz' ];

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-23 22:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found