I am reading from a file and again writing to another file. So when writing due to some big words alignment of the subsequent column gets distorted. I want to align all columns regardless of their length. I got one related question here. But the problem is that they are using Perl module Perl6::Form
, and I want to do without any modules.
#This the input file
Hey! How Are You
I AM FINEEE Thankyouuu
1 22 333 4444
After alignment it should look like this:
Hey! How Are You
I AM FINEEE Thankyouuu
1 22 333 4444
Code tried (UPDATED):
#!usr/bin/perl
use warnings;
use strict;
use feature 'say';
$file1 = "file1.log";
$temp = "temp.log";
open(OUT, "<$file1") or die "Could not open file $file1: $!";
open(temp,"+>>$temp") or die "Could not open file $temp: $!";
while (my $line = <OUT>) {
my @fs = split " ", $line;
my @rows = @fs ;
@col_lens = map { length } @rows if $.==1;
for my $col_idx (0..$#rows) {
my $col_len = length $rows[$col_idx];
if ($col_lens[$col_idx] < $col_len) {
$col_lens[$col_idx] = $col_len; #line 144
}
}
say temp1 (join "|",@fs);
}
close temp1;
close OUT;
Update: The code is working and giving me the length of biggest word, but getting a few warnings also. But now how will I add spaces to each column according to max size of word in it?
say temp1 (join "|",@fs);
this only add "|" in between them.
Use of uninitialized value in numeric lt (<) at log.pl line 144,
Use of uninitialized value in numeric lt (<) at log.pl line 144,
Use of uninitialized value in numeric lt (<) at log.pl line 144,
Use of uninitialized value in numeric lt (<) at log.pl line 144,
Use of uninitialized value in numeric lt (<) at log.pl line 144,
Use of uninitialized value in numeric lt (<) at log.pl line 144,