Thread Wie Multi-Dimensionales Array erstellen (10 answers)
Opened by kimmy at 2011-01-05 10:54

Taulmarill
 2011-01-05 13:16
#144101 #144101
User since
2004-02-19
1750 Artikel
BenutzerIn

user image
Ich würde nicht für jede Spalte einen Array anlegen, sondern das in einem HoA (Hash of Arrays) ablegen.
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
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


Die Datenstruktur sieht dann so aus:
Code: (dl )
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'
]
};
$_=unpack"B*",~pack"H*",$_ and y&1|0& |#&&print"$_\n"for@.=qw BFA2F7C39139F45F78
0A28104594444504400 0A2F107D54447DE7800 0A2110453444450500 73CF1045138445F4800 0
F3EF2044E3D17DE 8A08A0451412411 F3CF207DF41C79E 820A20451412414 83E93C4513D17D2B

View full thread Wie Multi-Dimensionales Array erstellen