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.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: upper or lower triangular matrix to full by thanos1983
in thread upper or lower triangular matrix to full by savita

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.