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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |