Leser: 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $file='tst.csv'; my @data; open(my $fh, '<', $file) or die("error open $file ($!)\n"); while(my $line=<$fh>) { chomp($line); my @elements=split(/\s+/,$line); push(@data,\@elements); } close($fh); print Dumper(\@data);
1
2
3
4
5
6
7
8
9
10
11
my (@header, @inhalt);
while(<>){
chomp;
if(1..1){
@header = split(/\t/,$_);
}
else{
@inhalt = split(/\t/,$_);
}
}
2011-01-05T11:08:37 kimmyAber meine Frage ist wie kann ich @header und @inhalt verbinden?
2011-01-05T10:42:43 kimmywie kann ich von der Datei solche Arrays erstellen?
@A = (1, 4, 3);
@B = ('a', 'b', '2c');
@C = (3, '4.5', 9);
@D = ('abc', 'xyz', 'df');
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $file='test.txt'; open(my $fh, '<', $file) or die("error open $file ($!)\n"); my $first=<$fh>; chomp($first); my @header=split(/\t/,$first); my @inhalt; while(my $line=<$fh>) { chomp($line); my @elements=split(/\t/,$line); push(@{$inhalt[$_]},shift(@elements)) for(0..$#header) } close($fh); print Dumper(\@inhalt);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
use strict; use warnings; use Data::Dumper; my %table; my @header = split /\s+/, scalar( <DATA> ); chomp $header[-1]; while ( my $line = <DATA> ) { chomp $line; my @fields = split /\s+/, $line; for my $col ( $[ .. $#header ) { push @{ $table{$header[$col]} }, $fields[$col]; } } print Dumper \%table; __DATA__ A B C D 1 a 3 abc 4 b 4.5 xyz 3 2c 9 df
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$VAR1 = {
'A' => [
'1',
'4',
'3'
],
'D' => [
'abc',
'xyz',
'df'
],
'C' => [
'3',
'4.5',
'9'
],
'B' => [
'a',
'b',
'2c'
]
};