Here is a simple program that I created to print a file by breaking it after a fixed number of columns. I feel it can be optimized but not sure how. Any feedback is great.
#This is a simple program that takes a file and the limits the columns length to
#the number passed in as second argument
if ($#ARGV < 1) {
printf("Usage: perl %s <name of the input file> <column length>\n", $0);
exit(-1);
}
$file=$ARGV[0];
open(INFO, $file) or die("Could not open file.");
$LINE_LENGTH = $ARGV[1];
$count = 0;
foreach $line (<INFO>) {
#print $line;
if (length($line) <= $LINE_LENGTH) {
printf("%s\n", $line);
} else {
$remaining_chars = length ($line);
$remaining_line = $line;
while ($remaining_chars >= $LINE_LENGTH) {
$cur_str = substr($remaining_line, 0, $LINE_LENGTH - 1 );
printf("%s\n", $cur_str );
$remaining_line = substr($remaining_line, $LINE_LENGTH - 1);
$remaining_chars = $remaining_chars - length($cur_str);
}
printf("%s",$remaining_line );
}
if ($++counter == 2){
last;
}
}
printf("\n");
close(INFO);