Thread Daten "Hashen"? (14 answers)
Opened by gmafx at 2010-11-25 22:39

topeg
 2010-11-26 20:30
#143103 #143103
User since
2006-07-10
2611 Artikel
BenutzerIn

user image
Das hier funktioniert bei mir:
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
#!/usr/bin/perl
use strict;
use warnings;

my %elements = ();
my $input = $ARGV[0];

my $cnt=0;
open (my $fh, '<', $input) or die("ERROR open $input ($!)\n");
while(my $line=<$fh>)
{
  chomp($line);
  my ($id,@words) = split /\s+/, $line;

  #füge dem hasheintrag "$elements{$id}",
  # welches ein Array ist (siehe Autovivikation),
  # einen weiteren Eintrag hinzu,
  # der das Array "@words" ist.
  push(@{$elements{$id}},\@words);

  $cnt++;
  if ($cnt % 1000000 == 0) {print "read another 1000000 lines!\n";}
}
close($fh);

while( my ($k, $v) = each %elements )
{
  print "key: $k\n";
  for my $i ( 0 .. $#{$v} ) {
    print "  cols $i: ". join( ', ', @{ $v->[$i] } ) ."\n";
  }
}


Daten:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
id1 test1a test1b test1c
id1 test1d test1e test1f
id1 test1g test1h test1i
id2 test2a test2b test2c
id2 test2d test2e test2f
id2 test2g test2h test2i
id3 test3a test3b test3c
id3 test3d test3e test3f
id3 test3g test3h test3i
id4 test4a test4b test4c
id4 test4d test4e test4f
id4 test4g test4h test4i

Ausgabe:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
key: id1
cols 0: test1a, test1b, test1c
cols 1: test1d, test1e, test1f
cols 2: test1g, test1h, test1i
key: id4
cols 0: test4a, test4b, test4c
cols 1: test4d, test4e, test4f
cols 2: test4g, test4h, test4i
key: id2
cols 0: test2a, test2b, test2c
cols 1: test2d, test2e, test2f
cols 2: test2g, test2h, test2i
key: id3
cols 0: test3a, test3b, test3c
cols 1: test3d, test3e, test3f
cols 2: test3g, test3h, test3i


Es kommt intern eine Struktur der Art heraus:
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
%elements=(
  'id1' => [
             [ 'test1a', 'test1b', 'test1c' ],
             [ 'test1d', 'test1e', 'test1f' ],
             [ 'test1g', 'test1h', 'test1i' ],
           ],
  'id2' => [
             [ 'test2a', 'test2b', 'test2c' ],
             [ 'test2d', 'test2e', 'test2f' ],
             [ 'test2g', 'test2h', 'test2i' ],
           ],
  'id3' => [
             [ 'test3a', 'test3b', 'test3c' ],
             [ 'test3d', 'test3e', 'test3f' ],
             [ 'test3g', 'test3h', 'test3i' ],
           ],
  'id4' => [
             [ 'test4a', 'test4b', 'test4c' ],
             [ 'test4d', 'test4e', 'test4f' ],
             [ 'test4g', 'test4h', 'test4i' ],
           ],
);

View full thread Daten "Hashen"?