Leser: 23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
sub MATRIX { #Einlesen und Speichern der Datenfiles open (FILETOREAD, "<$_") || die "Can't open $_: $!"; $i = 0; while (<FILETOREAD>) { # read each line of file, one at a time ++$i; chomp; next if /^#/; # Kommentarzeilen überspringen s/\s+//g; # Leerzeichen entfernen $xyz = $_; push(@values,[split(/\,/,$xyz)]); } $values_ref = \@values; [b]return $values_ref; return $i; return $spalten_values = @ergebniss = (split(/\,/,$xyz));[/b] }
1 2 3 4 5 6 7 8 9 10 11 12
#!/usr/bin/perl use strict; use warnings; use DeinModul; my $var = funktion(); my @test = test(); print "$var\n"; print $_,"\n" for @test;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
package DeinModul; use strict; use warnings; use base 'Exporter'; our @EXPORT = qw(funktion test); sub funktion { return 'test'; } sub test { return (1,2,3); } 1;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
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; }
my ( $count, $array_r ) = matrix( '/path/to/file' );
Quotereturn $i, \@values;
1 2 3 4 5 6
# alle elemente der array referenz for ( @$array_r ) { print $_; } # erstes element print $array_r->[0];