Hello savita,
Although I know very little about matrixes, I was bored and I wanted to wake up my mind so I wrote this. Something like that could do the job (I guess).
Sample of code:
#!/user/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; sub matrix_write_file { my ($filename, $matrix) = @_; open(my $wfh, '>', $filename) or die "Could not open file '$filename' $!"; # print the whole thing with refs for my $aref ( @$matrix ) { say $wfh "@$aref"; } close $wfh or die "Could not close file '$filename' $!"; return; } sub matrix_read_file { my $matrix_name; my ($filename) = @_; open (my $rfh, $filename) or die "Could not open $filename: $!"; while (my $line = <$rfh>) { chomp($line); next if $line =~ /^\s*$/; # skip blank lines if ($line =~ /^([A-Za-z]\w*)/) { $matrix_name = $1; } else { my (@row) = split (/\s+/, $line); push (@{$matrix_name}, \@row); # insert the row-array into # the outer matrix array } } close $rfh or die "Could not close file '$filename' $!"; return $matrix_name; } my @matrix = ( [1, 2, 3], [4, 5, 6], [7, 8, 9] ); my $filename = 'matrix.txt'; matrix_write_file($filename, \@matrix); print Dumper matrix_read_file($filename); __END__ $ perl test.pl $VAR1 = [ [ '1', '2', '3' ], [ '4', '5', '6' ], [ '7', '8', '9' ] ]; $ cat matrix.txt 1 2 3 4 5 6 7 8 9
Update: Maybe a while loop in writing consumes less memory:
# print the whole thing with refs faster than for # but you are destroying the AoA (matrix) while ( defined ( my $aref = shift @$matrix ) ) { say $wfh "@$aref"; }
Update2: I manage to update the code using IO::All maybe this could help:
#!/user/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; use IO::All -utf8; # use Benchmark qw(:all) ; # WindowsOS use Benchmark::Forking qw( timethese cmpthese ); # UnixOS sub matrix_write_file { my ($filename, $matrix) = @_; open(my $wfh, '>', $filename) or die "Could not open file '$filename' $!"; # print the whole thing with refs for my $aref ( @$matrix ) { say $wfh "@$aref"; } close $wfh or die "Could not close file '$filename' $!"; return; } sub matrix_read_file { my $matrix_name; my ($filename) = @_; open (my $rfh, $filename) or die "Could not open $filename: $!"; while (my $line = <$rfh>) { chomp($line); my (@row) = split (/\s+/, $line); push (@{$matrix_name}, \@row); } close $rfh or die "Could not close file '$filename' $!"; return $matrix_name; } sub writeMatrix2File{ my ( $filename , $matrix ) = @_; my $iol = io($filename)->lock; while (defined(my $line = shift @$matrix)) { # io($filename)->appendln(join ' ', map "$_", @$line); $iol->println(join ' ', map "$_", @$line) } $iol->unlock; } sub readMatrixFromFile{ my $matrix; my ( $filename ) = @_; my @lines = io($filename)->chomp->slurp; while (defined (my $line = shift @lines)){ my (@row) = split (/\s+/, $line); push (@{$matrix}, \@row); } return $matrix; } sub manual { my $matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; my $filename = 'matrix.txt'; matrix_write_file($filename, $matrix); matrix_read_file($filename); } sub module { my $matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; my $filename = 'matrix.txt'; writeMatrix2File($filename, $matrix); readMatrixFromFile($filename); } manual(); module(); my $results = timethese(1000000, { Manual => \&manual, Module => \&module, }, 'none'); cmpthese( $results ); __END__ :~/PerlMonks$ perl matrix.pl Rate Module Manual Module 1895/s -- -84% Manual 12054/s 536% --
Update3: This is the final code that does ( I tried reading in the file into an array of arrays):
#!/user/bin/perl use strict; use warnings; use Data::Dumper; use IO::All -utf8; sub writeMatrix2File{ my ( $filename , $matrix ) = @_; my $iol = io($filename)->lock; while (defined(my $line = shift @$matrix)) { # io($filename)->appendln(join ' ', map "$_", @$line); $iol->println(join ' ', map "$_", @$line) } $iol->unlock; } sub readMatrixFromFile{ my $matrix; my ( $filename ) = @_; my @lines = io($filename)->chomp->slurp; while (defined (my $line = shift @lines)){ my (@row) = split (/\s+/, $line); push (@{$matrix}, \@row); } return $matrix; } my $matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; my $filename = 'matrix.txt'; writeMatrix2File($filename, $matrix); print Dumper readMatrixFromFile($filename); __END__ ~/PerlMonks$ perl matrix.pl $VAR1 = [ [ '1', '2', '3' ], [ '4', '5', '6' ], [ '7', '8', '9' ]
Hope this helps, BR.
In reply to Re: upper or lower triangular matrix to full
by thanos1983
in thread upper or lower triangular matrix to full
by savita
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |