Hi there,

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,
Benbo

In reply to Question regarding Tie::File or a better way to handle huge 2-D arrays by Benbo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.