Gerard Robin wrote:
> Hello,

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;

Use a Hash of Arrays:

#!/usr/bin/perl
use warnings;
use strict;


sub matrix_read_file;  # Declare first

matrix_read_file \my %data;

print "@$_\n" foreach values %data;


sub matrix_read_file {
    my $hash_ref = shift;
    my $matrix_name;

    while ( my $line = <DATA> ) {
        next if $line =~ /^\s*$/;
        if ( $line =~ /^([A-Za-z]\w*)/ ) {
            $matrix_name = $1;
        }
        else {
            my @row = split ' ', $line;
            push @{ $hash_ref->{ $matrix_name } }, [EMAIL PROTECTED];
        }
    }
}

__DATA__
MAT1
1  2 4
10 30 0
MAT2
5  6
1 10




John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to