Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by bh_perl (Monk)
on Aug 15, 2008 at 07:43 UTC ( [id://704484]=perlquestion: print w/replies, xml ) Need Help??

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

Hi.. Could somebody help me how to skip first n character of input data and read others data for every 100 character ?
As example:- This is my input data:- 1234567891000000000000000000000000000000000000 Then.. i want to skip first 9 character and read every 100 characetr f +or others data which are:- 10000000000000000000000000000000000000000000..
Could soembody help me ?
  • Comment on How to skip first n character from data and read every m character..
  • Download Code

Replies are listed 'Best First'.
Re: How to skip first n character from data and read every m character..
by RMGir (Prior) on Aug 15, 2008 at 10:01 UTC
    I'll elaborate a bit more on the previous answers.

    Take a look at the documentation (using perldoc -f) for the read and seek functions, and for the substr function.

    You can use seek to "skip" past parts of a file (let's say 9 bytes), and "read" to read x bytes from your current position in a file.

    You could just read the first 9 bytes and throw them away, but seek is better in the general case, so it's not a bad idea to learn about it now.

    If your data is in memory, "substr" will let you access any part of it you want, for example the 100 bytes starting at byte 9.


    Mike
Re: How to skip first n character from data..
by Anonymous Monk on Aug 15, 2008 at 07:52 UTC
Re: How to skip first n character from data and read every m character..
by thezip (Vicar) on Aug 15, 2008 at 16:08 UTC

    Here's yet another way to do it:

    #!/perl/bin/perl use strict; use warnings; use Data::Dumper; my $str = '123456789100020003000400050006000'; my $pattern = 'A9(A4)*'; my @values = unpack($pattern, $str); print Dumper(\@values);

    Just change the values in the $pattern to suit your needs.


    Your wish is my commandline.
Re: How to skip first n character from data and read every m character..
by apl (Monsignor) on Aug 15, 2008 at 08:05 UTC
Re: How to skip first n character from data and read every m character..
by eosbuddy (Scribe) on Aug 15, 2008 at 10:01 UTC
    Not sure whether you mean that your data comes in through a file or STDIN (in anycase, if you don't consider converting to a file from STDIN a problem here's a snippet). Also, unfortunately, I don't know how not to read the first few characters, but chopping it off, I consider is easy. You may need to think of formatted input as an alternative. Here's the snippet:
    use strict; use warnings; my ($line, $line2); while ($line = <>) { chomp($line); $line2 = substr($line,9,80); print "$line2\n"; }
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

    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: perlquestion [id://704484]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-23 12:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found