I am currently trying to write a script which alters the contents of a 2D array. I first created a 2D array filled with '*':
******************** ******************** ******************** ********************
Then attempted to change parts of the 2D array based on y-value, x value and replacement string. So for instance, a change of (1, 2, "test) in the above array would yield
******************** **test************** ******************** ********************
This all worked fine, except that the arrays I was dealing with in the real world were 1.3-4 million columns x 100 rows. As you can imagine I pretty soon ran out of memory. So my next idea was to create an output file containing the '*' array and then use Tie::File and string manipulation to alter the rows, to save holding it all in memory. I then hit two problems with Tie::File (version 0.96). Using the following code from the CPAN documentation:
tie my @array, 'Tie::File', $outputFile or die $!; my $line = $array[2]; print $line . "\n"; untie @array;
Throws a "Use of uninitialized value in concatenation" error when printing the line, even though the outputFile contained 100 rows. The second problem I had was that using:
tie my @array, 'Tie::File', $outputFile or die $!; $array[2] = 'This is a test'; untie @array;
inserted 2 blank rows, then 'This is a test', then the original 100 rows. I was expecting it to simply replace the 3rd line. My complete code is here:
#!/usr/bin/perl use strict; use warnings; use Tie::File; my $genome_size = 1000; #1300000; my $outputFile = "/Users/Benbo/Desktop/temp/template.txt"; unlink $outputFile if (-e $outputFile); print "Start...\n"; add_fragment("Test", 2, 10, 200); create_template(); print "Done\n"; sub add_fragment{ my ($ref_id, $identity, $coord, $length) = @_; tie my @array, 'Tie::File', $outputFile or die $!; # $array[10] = 'This is a test'; my $line = $array[2]; print $line . "\n"; untie @array; } sub create_template{ for (1..9){ write_to_file($_ x 50 . "\n", $outputFile); } } sub write_to_file{ my $input = shift; my $outputFile = shift; open my $output, ">>", "$outputFile" or die "Could not open $outp +utFile: $!"; print $output $input; }
So my first question is can anyone tell me what I'm doing wrong with Tie::File as it is not working as I thought it would from the CPAN docs. My second question is, given that I will be inserting around 300k fragments into the array, is there a quicker and more effcient way of doing this?
Many thanks,| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |