Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Create matrix and save to a file

by Anonymous Monk
on Apr 19, 2007 at 06:16 UTC ( [id://610903]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to create a matrix with different numerical values and save it to a .txt file. also I want to read this matrix values from the file and perform operations on it and save it again, any idea? Thanks...

Replies are listed 'Best First'.
Re: Create matrix and save to a file
by varian (Chaplain) on Apr 19, 2007 at 08:00 UTC
    A convenient method I use to save an arbitrary structure of data to a textfile is to use our famous Data::Dumper core module to capture the variable's content in a string.
    When I need it again I just use an eval to convert the string back into my matrix or other datastructure.
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @data = (0,1,2,3 ); print "BEFORE: ",join(',',@data),"\n"; my $saved_data = Dumper(\@data); # ...here you would write saved data to textfile my $VAR1; eval $saved_data; my @data2 = @{$VAR1}; print "AFTER: ",join(',',@data2),"\n";
    Output would be:
    BEFORE: 0,1,2,3 AFTER: 0,1,2,3
Re: Create matrix and save to a file
by stonecolddevin (Parson) on Apr 19, 2007 at 06:26 UTC

    Not sure if these are what you're looking for, but a few of the Bio:: namespace modules looked good. Link: Matrix.

    meh.
Re: Create matrix and save to a file
by agianni (Hermit) on Apr 19, 2007 at 11:39 UTC

    If your specific mention of a text file was arbitrary, you might consider Storable, which easily and efficiently stores a data structure for reuse at a later time. The downside is that the file isn't human readable.

    perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'
Re: Create matrix and save to a file
by fmerges (Chaplain) on Apr 19, 2007 at 07:54 UTC

    Hi,

    Hi, you can do that with any serialization module. For example you can use YAML::Syck.

    BTW there's also PDL

    Regards,

    fmerges at irc.freenode.net
Re: Create matrix and save to a file
by logie17 (Friar) on Apr 19, 2007 at 15:42 UTC
    First I would check out some tutorials on Input and Output. Once you understand basic filehandles and how to read/create files then manipulating the data is fairly straightforward.
    s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;
Re: Create matrix and save to a file
by Moron (Curate) on Apr 19, 2007 at 16:31 UTC
    It's trivial enough to just code this. See perlref, for, open, split, join, print, close. The operator for reading from a file is to enclose the filehandle in '<' '>'. A matrix in Perl is just an array of array references, e.g.
    # matrix of random numbers my @matrix; for my $y ( 0..9 ) { my @row = (); for my $x ( 0..9 ) { push @row, 100*rand(); } push @matrix, \@row; }
    __________________________________________________________________________________

    ^M Free your mind!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://610903]
Approved by Sidhekin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-23 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found