Thread .csv Zeilenweise in (Hash?)Array einlesen & auf einzelne Werte zugreifen (7 answers)
Opened by norvel at 2012-07-26 14:26

FIFO
 2012-07-26 16:38
#160214 #160214
User since
2005-06-01
469 Artikel
BenutzerIn

user image
Hi, topeg hat Dir ja die effizienteste Variante gezeigt, und auch, wie man Dateihandles besser einsetzt, strict/warnings verwendet, usw.

Zur Vervollständigung hier noch, wie es prinzipiell zu Fuß ginge:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
my @data_array;
my %data_hash

while (my $line = <$fh>) {
    chomp($line);
    # Variante 1: Anonyme Arrays
    my @values = split(/\s*,\s*/, $line);
    push @data_array, [@values];

    # Variante 2: Hash mit Spalte 0 als key
    my ($key, @hvalues) = split(/\s*,\s*/, $line);
    $data_hash{$key} = [@hvalues];
}


Zugriff auf Elemente:
Variante 1: $elem = $data_array[$row][$col]
Variante 2: $elem = $data_hash{$key}->[$col]
Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it? -- Brian Kernighan: "The Elements of Programming Style"

View full thread .csv Zeilenweise in (Hash?)Array einlesen & auf einzelne Werte zugreifen