Leser: 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
use strict; use warnings; use Data::Dumper; my %values; for my $line (@lines) { chomp $line; my ($epoch, $d, $m, $y, $value1, ...) = split ' ', $line; $values{$epoch}->{count}++; $values{$epoch}->{value1_sum} += $value1; ... } for my $epoch (keys %values) { my $value1_sum = delete $values{$epoch}->{value1_sum}; $values{$epoch}->{value1} = $value1_sum / $values{$epoch}->{count}; ... } print Dumper \%values;
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
@lines=<INFO>; for($i=0;$i<=$#lines;++$i) { # Block Aircraft Registration #$aircraftregistration=substr($lines[$i],7,5); # Block Departure Airport # $departureairport=$zeile1[4]; # $departureairport=substr($lines[$i],23,4); # Block Destination Airport # $destinationairport=substr($lines[$i],28,4); # Block Datum $day1=substr($lines[$i],33,2); $month1=substr($lines[$i],35,3); $year1=substr($lines[$i],38,4); if ($month1 eq "SEP"){ $month="09" } # Block Zeit der Messung $hr=substr($lines[$i],42,2); $min=substr($lines[$i],44,2); $sec=substr($lines[$i],46,2); # Block Zeit Empfang der Messung $hr1=substr($lines[$i],49,2); $min1=substr($lines[$i],51,2); $sec1=substr($lines[$i],53,2); # Block Koordinaten # $lat=substr($lines[$i],57,7); #$long=substr($lines[$i],64,7); # Block Höhe in Zehntel # $fl=substr($lines[$i],71,4); # Block Temperatur # $sign=substr($lines[$i],75,1); # $temp=substr($lines[$i],76,3); #if ($sign eq "P"){ $sig=" " #} #if ($sign eq "M"){ $sig="-" # } # Block Wind $dir=substr($lines[$i],79,3); # $vel=substr($lines[$i],82,3); # Bestimme UNIX Zeitstempel $TIME = timelocal($sec, $min, $hr, $day1, $month-1, $year1-1900); for my $line (@lines) { chomp $line; my ($TIME, $dir) = split ' ', $line; $values{$TIME}->{count}++; $values{$TIME}->{value1_sum} += $value1; } for my $TIME (keys %values) { my $value1_sum = delete $values{$TIME}->{value1_sum}; $values{$TIME}->{value1} = $value1_sum / $values{$TIME}->{count}; } print $TIME," ",$dir,"\n";
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 55 56 57
#!/usr/bin/perl use 5.010; use strict; use warnings; # Data reducer collecting values in a list my $collect = { init => [], step => sub { [ @{$_[0]}, $_[1] ] }, fini => sub { '[' . join(', ', @{$_[0]}) . ']' } }; # Data reducer averaging numerical values my $average = { init => [ 0, 0 ], step => sub { [ $_[0]->[0] + 1, $_[0]->[1] + $_[1] ] }, fini => sub { $_[0]->[1] / $_[0]->[0] } }; # Mapping from field indices to data reducers my @reducers = ( $collect, $collect, $average, $average, $average, $average ); # Read data my %data = (); while (<DATA>) { my ($stamp, $d, $m, $y, @fields) = split /\s+/; push @{$data{$stamp}}, \@fields; } # Reduce and print data foreach my $stamp (sort keys %data) { my @accumulators = map { $_->{init} } @reducers; foreach my $fields (@{$data{$stamp}}) { @accumulators = map { $reducers[$_]->{step}->($accumulators[$_], $fields->[$_]) } $[ .. $#reducers; } @accumulators = map { $reducers[$_]->{fini}->($accumulators[$_]) } $[ .. $#reducers; say join("\t", $stamp, @accumulators); } __DATA__ 1094000940 01 09 2004 N49390 E009202 12360 -3 320 017 1094000940 01 09 2004 N49410 E009203 11650 -2 305 016 1094000940 01 09 2004 N49431 E009204 11120 -2.5 339 016 1094001000 01 09 2004 N49451 E009205 10990 -2.2 325 018 1094001000 01 09 2004 N49470 E009199 10460 -1.5 291 018 1094001000 01 09 2004 N49489 E009184 9550 -5.2 250 010
2010-04-12T13:53:20 pqinit, step und fini sind ganz einfache hash-keys...