Thread Array-Lines zählen (12 answers)
Opened by cohama at 2014-05-06 15:09

hlubenow
 2014-05-06 17:52
#175347 #175347
User since
2009-02-22
875 Artikel
BenutzerIn
[default_avatar]
Ich benutze bei sowas gern "split()" (und weniger RegEx). Mußt mal gucken, was bei Dir genau der Separator ist (vermutlich "\t"). Wenn das bei Dir einheitlich ist, braucht man die Funktion "prepareLine()" nicht:
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/perl

use warnings;
use strict;

sub prepareLine {
    my $a = shift;
    $a =~ s/ +/ /g;
    return $a;
}

my @data = <DATA>;
my $i; my $u; my @a;
my %id_count;
my %six_count;
my $sep = " ";
foreach $i (@data) {
    chomp($i);
    $i = &prepareLine($i);
    @a = split($sep, $i);
    $id_count{$a[0]}++;
    $six_count{$a[0]} += $a[5];
}

for $i (sort(keys(%id_count))) {
    print "$i\t$id_count{$i}\t$six_count{$i}\n";
}

__DATA__
3681     OUT 2014-05-06 12:57:05 22     30     3338359406   3338753402   unkown           8414        
3681     OUT 2014-05-06 12:39:44 37     43     3338359406   3338753402   unkown           8414        
3681     OUT 2014-05-06 11:56:15 191    210    3338359406   3338753402   unkown           3991110     
3681     OUT 2014-05-06 12:06:15 0      10     3338359406   3338753402   unkown           033372270   
5494     OUT 2014-05-06 06:25:48 149    172    3375550834   X            international    0038763931307
5494     OUT 2014-05-06 07:43:49 419    443    3375585134   X            international    0038763649266
5494     OUT 2014-05-06 11:41:07 127    141    3375585134   X            international    002207733514
5494     OUT 2014-05-06 08:54:48 252    259    3375550834   X            international    00447589972757
5494     OUT 2014-05-06 01:28:17 0      11     3375585133   X            international    0023221350752
5494     OUT 2014-05-06 11:41:15 121    133    3375550834   X            international    0022062892015
5494     OUT 2014-05-06 01:28:25 0      10     3375585133   X            international    005331997555
5494     OUT 2014-05-06 01:28:39 0      12     3375585133   X            international    002392204144
5494     OUT 2014-05-06 08:15:03 0      14     3375585133   X            international    0038763456569
5494     OUT 2014-05-06 08:15:02 0      14     3375585134   X            international    0038763649266
5494     OUT 2014-05-06 11:41:09 122    131    3375585133   X            international    0022062892015
5494     OUT 2014-05-06 08:14:36 0      14     3375585134   X            international    0038763649266
5494     OUT 2014-05-06 01:29:00 0      10     3375585133   X            international    0037125050148
7787     OUT 2014-05-06 04:06:37 125    132    3376290890   X            international    00447589972819
7787     OUT 2014-05-06 04:06:51 124    131    3376290890   X            international    00447589972819
7787     OUT 2014-05-06 04:06:58 127    134    3376290890   X            international    00447589972819
7787     OUT 2014-05-06 07:24:22 121    128    3376290890   X            international    00447589972819
7787     OUT 2014-05-06 08:44:07 126    133    3376290890   X            international    00447589972819
7787     OUT 2014-05-06 04:07:26 119    126    3376290890   X            international    00447589972819
7787     OUT 2014-05-06 04:07:40 122    129    3376290890   X            international    00447589972819
7787     OUT 2014-05-06 10:48:55 127    146    3376290890   X            international    002207396071

Bei großen Dateien empfehle ich zudem Tie::File.

View full thread Array-Lines zählen