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

Hi.. Is there a way to split by character in perl? As example I read a file and its contain a lots of data in one line of the input file. Then how could I captured the data based on data length (128) ?. As example record 1 will be 0-128, record 2 will be 129-256 as next and etc... Is there a way to do this?

Replies are listed 'Best First'.
Re: How to split by character ?
by Gangabass (Vicar) on Jul 22, 2008 at 03:21 UTC

      See also read and/or substr which will help you get the records to pass to unpack; also of interest might be the documentation for $/ about setting it to a reference to a number (e.g. in your example $/ = \128) in order to use the normal diamond operator to read records rather than lines.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

Re: How to split by character ?
by ikegami (Patriarch) on Jul 22, 2008 at 05:39 UTC
    From a file handle:
    local $/ = \128; while (<$fh>) { ... $_ contains 128 bytes ... }
    From a string:
    while (length($str)) { my $rec = substr($str, 0, 128, ''); ... $rec contains 128 bytes ... }

    Note that offsets should be 0-127, 128-255, etc.

    A reply falls below the community's threshold of quality. You may see it by logging in.