Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by johngg (Abbot)
on Apr 16, 2013 at 22:20 UTC ( #1029023=note: print w/ replies, xml ) Need Help??


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

Attacking the insertions in reverse order from the right-hand side means you don't have to increment your position by one for each successive insertion. Swings and roundabouts really but sometimes a useful technique.

$ 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;' 20130204000000000-068100-8361002201306810020022013068800-020-049-4

Here's the code without the PS2 prompt for easier copy'n'paste, as requested by /msg in respect of another post.

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;'

I hope this is of interest.

Cheers,

JohnGG


Comment on Re^2: Adding a TAB after a certain ammount of characters
Select or Download Code
Re^3: Adding a TAB after a certain ammount of characters
by biancoari (Initiate) on Apr 17, 2013 at 03:11 UTC

    Hi, I've tried to use it exactly as you put it and I got:

    biancoari@biancoari ~/Desktop $ perl perl1.pl
    String found where operator expected at perl1.pl line 8, near "say $text;'"
    (Might be a runaway multi-line '' string starting on line 1)
    (Missing semicolon on previous line?)
    syntax error at perl1.pl line 8, near "say $text;'"
    Execution of perl1.pl aborted due to compilation errors.

    Besides I have another question what I am trying to modify is a txt file with several lines (like the one on the example, they are all the same) any clue on that?

      Please show us your perl1.pl script inside <code> and </code> tags so we can see where you are going wrong. As for working on a multi-line file, calculate the offsets, open your file then do something like

      while ( <$inputFH> ) { foreach my $offset ( reverse @cums[ 1 .. $#cums ] ) } substr $_, $offset, 0, q{-}; } print; }

      I hope this moves you forward.

      Update: Code modified to replace for statement modifier with an explicit loop, thanks choroba.

      Cheers,

      JohnGG

        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?

      syntax error at perl1.pl line 8, near "say $text;'"

      Note the single-quote just after the ; (semicolon) in "say $text;'". This closes the quoted code that began with an opening single-quote just after the -E on the first line of the command line invocation in johngg's reply. Get rid of that closing single-quote if you are running the code from a file and things will probably work better.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2015-07-03 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (54 votes), past polls