Y
Yup
Hello,
I'm just starting to learn Perl. Up until now I'd been struggling to
learn how to import a tab-delimited data table (from a text file) into
Perl as a two dimensional nested array. I wanted to be able to
manipulate that data by accessing it using x-y coordinates, and then
output it again.
I may have been looking in the wrong place, but I had a hard time
finding help on Google groups and other places online. However, I've
managed to figure it out, and thought I'd post my code.
For two reasons, I suppose - to help other novices, and perhaps get
some comments from more advanced users. On the latter part, I'm
interested in knowing what I could do to make my code run faster and
make it more Perl-ish.
Thanks for your help ahead of time.
#!/usr/local/bin/perl -w
#define filename and open it
$file = 'a1.txt';
open(INFO, $file) || die "Can't open file $file\n" ;
# read file into temporary array called "lines"
while(<INFO>)
{
#chop off the carrage return
chop $_;
push @lines, $_;
}
# Close the file
close(INFO);
#reset index for generating arrays named "line_{$i}"
$i = 0;
#read each array entry into new array, split with tab
foreach (@lines)
{
push @{'line_'.${i}}, split("\t", $lines[$i]);
$i++
}
#generate an array to hold the other arrays
for ($i=0; $i<scalar(@lines); $i++)
{
push @A, *{'line_'.${i}};
}
#rename the top corner to be START
$A[0][0] = "START";
#open file to send data to
open(OUTFILE, ">a1_edited.txt");
for ($i=0; $i<scalar(@lines);$i++)
{
for ($j=0;$j<scalar(@line_0);$j++)
{
print OUTFILE "$A[$i][$j]";
if ($j<(scalar(@line_0)-1)) { print OUTFILE "\t";}
}
print OUTFILE "\n";
}
close(OUTFILE);
I'm just starting to learn Perl. Up until now I'd been struggling to
learn how to import a tab-delimited data table (from a text file) into
Perl as a two dimensional nested array. I wanted to be able to
manipulate that data by accessing it using x-y coordinates, and then
output it again.
I may have been looking in the wrong place, but I had a hard time
finding help on Google groups and other places online. However, I've
managed to figure it out, and thought I'd post my code.
For two reasons, I suppose - to help other novices, and perhaps get
some comments from more advanced users. On the latter part, I'm
interested in knowing what I could do to make my code run faster and
make it more Perl-ish.
Thanks for your help ahead of time.
#!/usr/local/bin/perl -w
#define filename and open it
$file = 'a1.txt';
open(INFO, $file) || die "Can't open file $file\n" ;
# read file into temporary array called "lines"
while(<INFO>)
{
#chop off the carrage return
chop $_;
push @lines, $_;
}
# Close the file
close(INFO);
#reset index for generating arrays named "line_{$i}"
$i = 0;
#read each array entry into new array, split with tab
foreach (@lines)
{
push @{'line_'.${i}}, split("\t", $lines[$i]);
$i++
}
#generate an array to hold the other arrays
for ($i=0; $i<scalar(@lines); $i++)
{
push @A, *{'line_'.${i}};
}
#rename the top corner to be START
$A[0][0] = "START";
#open file to send data to
open(OUTFILE, ">a1_edited.txt");
for ($i=0; $i<scalar(@lines);$i++)
{
for ($j=0;$j<scalar(@line_0);$j++)
{
print OUTFILE "$A[$i][$j]";
if ($j<(scalar(@line_0)-1)) { print OUTFILE "\t";}
}
print OUTFILE "\n";
}
close(OUTFILE);