in reply to Re: Insert at regular increments
in thread Insert at regular increments

Thanks. I didn't know you could use substr as an lvalue, that's very handy. One comment: I think I'd need to say

substr($_, 321, 0) = "\n"

to avoid replacing some poor unsuspecting character with the newline.

And one question: how would I use this approach to add newlines after every record in the file (i.e. after every 320 characters)?

Replies are listed 'Best First'.
Re: Re: Re: Insert at regular increments
by broquaint (Abbot) on Jul 22, 2002 at 21:39 UTC
    how would I use this approach to add newlines after every record in the file (i.e. after every 320 characters)?
    Take advantage of the $/ variable
    { open(my $data, '<', "data_file") or die("ack - $!"); open(my $out, '>', "out_file") or die("ack - $!"); local $/ = \320; while(<$data>) { chomp; print $out $_ . "\n"; } }

    HTH

    _________
    broquaint

      Using $/ is a nice esoteric idea requiring the person to really know perl in order to understand the code... but it won't work unless you already know that the file doesn't have the record separators. (In which case you wouldn't need chomp().) If I read the question correctly, the records themselves are 320 characters, not 319 characters plus a newline. If the file already has newlines, the first newline will become the first character in the second record and everything will get even more squirrely after that.

      If you are going to use it, besides pre-determining the existence of newlines, I suggest use English; and $INPUT_RECORD_SEPARATOR as well as comments explaining the magic for the uninitiated.

      -sauoq
      "My two cents aren't worth a dime."
      
        Thanks, sauoq. Agreed, using $/ may be a little more obscure, but it is the type of record-based solution I was looking for. I wouldn't run into the newline problem since I first check to see whether the file needs the mod in the first place (although you suggested a much more concise way of performing that check in another reply, so thanks for that too!).

        -P

        Using $/ is a nice esoteric idea requiring the person to really know perl in order to understand the code...
        I'd saying changing the value of $/ to alter how a file is read in is pretty idiomatic perl[1], and failing that knowledge there was a link to the perlvar manpage.
        but it won't work unless you already know that the file doesn't have the record separators.
        If the records are delimited by 320 character chunks then the code should work as it'll remove any existing newlines then append one. If it gets much more complex than that then some seek()ing and closer examination of the data will be needed.
        HTH

        _________
        broquaint

        [1] what's more, setting $/ to an integer reference was implemented with record-oriented files in mind

        update: reworded the second paragraph to be more accurate/less wrong (thanks for the heads-up Aristotle)

      That's what I was looking for! I knew there was nice native support for dealing with fixed-length records. Much reading to do... Thanks broquaint!