I need to parse a file in list format, such as:
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
into a tabular format, like:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
I took a bit of perl last term so I wrote this:
#!/usr/bin/perl -w
use strict;
my $m_cols=5;
my $m_rows=3;
my $row_counter=0;
my $col_counter=0;
open(INFILE, "< dem.txt") or die "Can't open the infile";
open(OUTFILE,"> tempout.txt") or die "Can't open the outputfile";
#read in from file
while(<INFILE>)
{
if($col_counter < $m_cols)
{
print OUTFILE $_, " ", # suppress new line
$col_counter++;
}
else
{
$col_counter = 0;
$row_counter++;
print OUTFILE '';
print OUTFILE $_, " ", #suppress new line
}
}
if($m_rows != $row_counter)
{
print "Row counter and specified rows do not match. Corrupt output\n";
}
Am running it under active perl on windows.
Anyways, this generates this gimpy output:
1
02
13
24
35
41
2
03
14
25
35
44
3
02
11 2
Which isn't quite what I had in mind!
I've stepped using a debugger, and nothing jumped out at me (ie variables setting to zero early as I suspected was the case).
Any help would be much appreciated!
Cheers