Gerard Robin wrote:
Hello,
this script does what is expected :
#!/usr/bin/perl
use warnings;
# use strict;
&matrix_read_file;
print "@$_\n" foreach @MAT1;
print "@$_\n" foreach @$matrix_name;
sub matrix_read_file
{
while (my $line = <DATA>) {
chomp $line;
next if $line =~ /^\s*$/;
if ($line =~ /^([A-Za-z]\w*)/) {
$matrix_name = $1;
} else {
my @row = split /\s+/, $line;
push @{$matrix_name}, [EMAIL PROTECTED];
}
}
}
__DATA__
MAT1
1 2 4
10 30 0
MAT2
5 6
1 10
but how can I saved the values of $matrix_name in two variables $matrix1
and $matrix2 to be reuse in the lines:
print "@$_\n" foreach @MAT1;
print "@$_\n" foreach @$matrix_name;
tia
It is better to store your data in a single data structure,
specifically, a hash of arrays of arrays.
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;
$Data::Dumper::Maxdepth = 0;
my %Matrices = ();
&matrix_read_file;
# print Dumper \%Matrices; # uncomment to see the data structure
# Display the matrices' content
for my $matrix_name ( sort keys %Matrices ){
for my $array_ref ( @{$Matrices{$matrix_name}} ){
print "@$array_ref\n";
}
}
sub matrix_read_file
{
my $matrix_name = '';
while (my $line = <DATA>) {
chomp $line;
next if $line =~ /^\s*$/;
if ($line =~ /^([A-Za-z]\w*)/) {
$matrix_name = $1;
} else {
my @row = split /\s+/, $line;
push @{$Matrices{$matrix_name}}, [EMAIL PROTECTED];
}
}
}
__DATA__
MAT1
1 2 4
10 30 0
MAT2
5 6
1 10
--
Just my 0.00000002 million dollars worth,
--- Shawn
"For the things we have to learn before we can do them,
we learn by doing them."
Aristotle
"The man who sets out to carry a cat by its tail learns something that
will always be useful and which will never grow dim or doubtful."
Mark Twain
"Believe in the Divine, but paddle away from the rocks."
Hindu Proverb
* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>