Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^5: Adding a TAB after a certain ammount of characters

by biancoari (Initiate)
on Apr 17, 2013 at 12:09 UTC ( #1029131=note: print w/replies, xml ) Need Help??


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

Now the file looks like this perl1.pl script looks like this (after some modifications I did from reading the replies

#!/usr/local/bin/perl perl -Mstrict -Mwarnings -E 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;

And the reply I get from running the script is:

biancoari@biancoari ~/Desktop $ perl perl1.pl
syntax error at perl1.pl line 2, near "E
my "
Execution of perl1.pl aborted due to compilation errors.

I will add the

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

and modify the text, any idea if I am heading the right direction?

Replies are listed 'Best First'.
Re^6: Adding a TAB after a certain ammount of characters
by hdb (Monsignor) on Apr 17, 2013 at 12:11 UTC

    You need to remove the second line alltogether.

    perl -Mstrict -Mwarnings -E
Re^6: Adding a TAB after a certain ammount of characters
by choroba (Bishop) on Apr 17, 2013 at 12:33 UTC
    Replace the second line
    perl -Mstrict -Mwarnings -E
    which is for the shell with equivalent Perl lines:
    use strict; use warnings; use feature 'say';
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      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.
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2019-03-20 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How do you Carpe diem?





    Results (105 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!