use strict; use warnings; my $i=0; sub matrix { my ( $filepath ) = @_; # new array my @values; # open file for reading open my $fh, '<', $filepath or die "$filepath: open failed: $!\n"; my $i = 0; while ( my $line = <$fh> ) { $i++; # skip commented lines next if $line =~ m{^#}; # remove line breaks; obsolete because of following s/\s+//g # but good idea to do this (what happens if the s///g is gone?) ;o) chomp $line; # remove any white spaces (including line breaks) $line =~ s/\s+//g; push @values, [ split m{,}, $line ]; } close $fh or die "$filepath: close failed: $!\n"; # return number of read lines and reference to this array return $i, \@values; }