I m trying to aline a text file so that TABs are alined from line to line. For example, the following input file (those are TABs not SPACEs)
foo	bar		baz	booz		qaaz			abc
foo		bar	baz		booz	qaaz	abc	123
foo		bar		baz		booz	qaaz		abc
SHOULD output to
foo		bar		baz		booz		qaaz		abc
foo		bar		baz		booz		qaaz		abc	123
foo		bar		baz		booz		qaaz		abc
I read the file twice. The first time to get max position of each TAB stop for each line that I put into $max array. If a line has a Nth TAB stop bigger than any previous line then $max[$max_tab] is augmented
$max[$max_tab] = $+[0] if $max[$max_tab] < $+[0] ;$max_tab++; at line 16
then I go back to the begginning of that file using seek F0,0,0 and insert extra TABs for the lines that are missing and then I add portion line text after that TAB stop at line 30
print substr("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t", 0, ($max[$max_tab++] +- $+[0]) / 8), substr($_, $-[0], $+[0]);

However the output looks like portion of the previous text of each line gets repeated,that is,previous words gets pushed onto future words (ex: babaz, qabooz, abcqaaz) and TABs don t aline
foobar		babaz	booz		qabooz		qaaz			abcqaaz			abcabc
foobar	baz	baz		booz	qabooz	qaaz	abc	123qaaz	abc	123abc	123													123
foobar		bazbaz		booz	qaabooz	qaaz		abcqaaz		abcabc

I have printed where the TABs begin/end using $-[0] and $+[0] (LAST_MATCH_START/@- and LAST_MATCH_END/@+) at line 15
print $-[0], ' ', $+[0], ' ';
and get the right indexes
3 4 7 9 12 13 17 19 23 26 
3 5 8 9 12 14 18 19 23 24 27 28 
3 5 8 10 13 15 19 20 24 26 

I have read the substr perl function (lines 7 and 33)
https://www.geeksforgeeks.org/perl-substr-function/
https://perldoc.perl.org/functions/substr
https://www.tutorialspoint.com/perl/perl_substr.htm
https://squareperl.com/en/function/substr
and give the right arguements i m trying to figure out what s wrong
thank s for your help
here is the code
$valid_line==0; $nbr_line=$ARGV[1]; # format the first digit @_[0], length @_[1] wide with leading 0s sub format { return substr("00000000", 0, (@_[1] - length(@_[0]))) . @_[0]; } open(F0, $ARGV[0]); while(<F0>) { if (/[a-zA-Z0-9]/) { $valid_line++; $max_tab = 0; while (/\t+/g) { # print $-[0], ' ', $+[0], ' '; $max[$max_tab] = $+[0] if $max[$max_tab] < $+[0] ;$max_tab +++; } # print "\n"; $nbr_tab = $max_tab if $nbr_tab < $max_tab; } } $max_line=$nbr_line+$valid_line; seek F0,0,0; while(<F0>) { s/\r//;chop; $max_tab = 0; while (/[^\t]+/g) { print substr("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t", 0, ($max[$ma +x_tab++] - $+[0]) / 8), substr($_, $-[0], $+[0]); } print "\n"; } close F0;

In reply to misalined TABs using substr,LAST_MATCH_START/END,regex by perl_boy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.