http://www.perlmonks.org?node_id=1029136


in reply to Re^5: Adding a TAB after a certain ammount of characters
in thread Adding a TAB after a certain ammount of characters

Replace the second line
perl -Mstrict -Mwarnings -E
which is for the shell with equivalent Perl lines:
use strict; use warnings; use feature 'say';
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^7: Adding a TAB after a certain ammount of characters
by biancoari (Initiate) on Apr 17, 2013 at 16:04 UTC

    This is working GREAT!!!


    #!/usr/local/bin/perl use strict; use warnings; use feature 'say'; my $text = q{2013020400000000006810083610022013068100200220130688000200494}; my @offs = ( 17, 6, 31, 3, 3 ); my @cums = ( 0 ); push @cums, $_ + $cums[ -1 ] for @offs; substr $text, $_, 0, q{-} for reverse @cums[ 1 .. $#cums ]; say $text;

    Now what is need to do is to modify all the lines of the file, I know that I need to call the file, like this


     open(FILE, "file1.txt") or die "Could not open file: $!";

    Am I heading on the right direction?

      Almost. Use lexical file handles and three argument open:
      open my $FILE, '<', 'file1.txt' or die "Cannot open file1.txt: $!";

      To iterate over the lines of the file, use the diamond operator in a while loop:

      while (my $text = <$FILE>) { chomp $text; my @cums = (0); # ... }
      You can define the @offs outside the loop because they never change.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        Thanks everyone, I was able to complete it, here is the the final product

        #!/usr/local/bin/perl open my $FILE, '<', 'rapipago.txt' or die "Cannot open file1.txt: $!"; use strict; use warnings; use feature 'say'; while (my $text = <$FILE>) { chomp $text; my @cums = (0); my @offs = ( 17, 6, 31, 3, 3 ); my @cums = ( 0 ); push @cums, $_ + $cums[ -1 ] for @offs; substr $text, $_, 0, q{-} for reverse @cums[ 1 .. $#cums ]; say $text; }

        Now I have another question, is there a way that instead of printing with say, the results prints on another file, or the program modifies my first file with the result?