Thread Arrays of Hashes / Hashes of Hashes (5 answers)
Opened by hansi66 at 2010-11-18 16:59

topeg
 2010-11-18 17:37
#142853 #142853
User since
2006-07-10
2611 Artikel
BenutzerIn

user image
Ich nehme mal an die Dateien sähen so aus

values1.csv, values2.csw, values3.csw:
Code: (dl )
1
2
3
4
5
6
7
8
test1|test2|test3
test4|test5|test6
test7|test8|test9
testA|testB|testC
testD|testE|testF
testG|testH|testI
testJ|testK|testL
testM|testN|testO


keys.cfg:
Code: (dl )
Key1,Key2,Key3


Dann würde ich es so machen:
Code (perl): (dl )
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
35
36
37
#!/user/bin/perl
use strict;
use warnings;

# Modul zu lesen von CSV-Artigen Strukturen
use Text::CSV;

# Modul zur Ausgabe von Datenstrukturen
use Data::Dumper;

my $keys_file='keys.cfg';
my @val_files=qw(values1.csv values2.csv values3.csv);

my $keys_in=Text::CSV->new({sep_char=>','});
open(my $fh, "<", $keys_file) or die("Error open $keys_file ($!)\n");
my $keys = $keys_in->getline($fh);
close($fh);

my $vals_in=Text::CSV->new({sep_char=>'|'});
my @values;
for my $file (@val_files)
{
  my @lines;
  open(my $fh, "<", $file) or die("Error open $file ($!)\n");
  while(my $vals = $vals_in->getline($fh))
  {
    my %line_data;
    for my $pos (0..$#$vals)
    { $line_data{$keys->[$pos]}=$vals->[$pos]; }
    push(@lines,\%line_data);
  }
  close($fh);
  push(@values,\@lines);
}

# Testausgabe:
print Dumper(\@values);

View full thread Arrays of Hashes / Hashes of Hashes