As Javafan said there is no reason to read more than one number from the file at a time. To do this you might use (for example) the module File::Stream which allows you to read your array file one number at a time with
use File::Stream; my $stream = File::Stream->new($filehandle); $/ = qr/\s+/; ... $nextnum= <$stream>;

UPDATE: Here is an example implementation that seems to work (but with harcoded n and m)

#!/usr/bin/perl use warnings; use strict; use File::Stream; my $m=3; my $n=4; my $filehandle; open ($filehandle, '<', "testfile") || die "can't open the testfile"; my $stream = File::Stream->new($filehandle); $/ = qr/\s+/; my @a; my @ia; my @ja; my $nnz=1; my $x=0; my $y=0; my $value; while ($value=<$stream>) { $value=~s/\s//g; #because chomp doesn't work with File::Stream $x++; if ($y==0 or $x>$n) { $x=1; $y++; push @ia,$nnz; } if ($value) { push @a, $value; push @ja, $x; $nnz++; } } push @ia,$nnz; if ($y!=$m and $x!=$m) { print "Wrong number of values in array data\n"; } print 'A= [ ', join(' ',@a),"]\n"; print 'IA= [ ', join(' ',@ia),"]\n"; print 'JA= [ ', join(' ',@ja),"]\n";

UPDATE2: Check out kennethk's version below. He reads one line at a time. If you can guarantee that n is lower than lets say 10^8 for any matrix you have to process, his version is probably a lot faster. But if you can't rule out matrices with bigger n, something like above has to be done to read even lines in smaller chunks.


In reply to Re: Memory Efficient Sparse Matrix Handling by jethro
in thread Memory Efficient Sparse Matrix Handling by neversaint

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.