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
my $cnt = 0;
my @Values;
my $dir = './FXE';
foreach my $fp (glob("$dir/*")) {
#printf "%s\n", $fp;
open my $fh, "<", $fp or die "can't open";
while (<$fh>)
{
# get rid of newline character
chomp;
# read the fields in the current record into an array
@fields = split(/,/, $_);
if($fields[0] >=1101227)
{
$Values[$cnt]= $fields[0];
$cnt++;
$Values[$cnt]= $Values[$cnt] +$fields[1];
$cnt++;
$Values[$cnt]= $Values[$cnt] +$fields[2];
$cnt++;
$Values[$cnt]= $Values[$cnt] +$fields[3];
$cnt++;
$Values[$cnt]= $fields[4];
$cnt++;
$Values[$cnt]= $fields[5];
$cnt++;
$Values[$cnt]= $fields[6];
$cnt++;
$Values[$cnt]= $fields[7];
$cnt++;
$Values[$cnt]= "\n";
$cnt++;
}
}
close $fh or die "can't close";
$cnt=0;
}
print join(",", @Values), "\n";
1 2 3 4 5 6 7 8 9 10 11 12 13 14
use strict; # ! use warnings; # ! my %zeile = (); @zeile{qw(Name Alter Ort)} = split /,/, qq(Heinz,33,Buxtehude); print Dumper \%zeile; so sieht das aus $VAR1 = { 'Alter' => '33', 'Name' => 'Heinz', 'Ort' => 'Buxtehude' };
QuoteUse of uninitialized value in addition (+) at master_fxe.pl line 23, <$fh> line XYZ
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
use strict;
use warnings;
my @fields;
my $cnt = 0;
my @Values;
my $dir = './FXE';
foreach my $fp (glob("$dir/*"))
{
#printf "%s\n", $fp;
open my $fh, "<:encoding(utf8)", $fp or die "can't open $!";
while (<$fh>)
{
chomp;
@fields = split(',', $_);
if($fields[0] >=1101227)
{
$Values[$cnt++]= $fields[0];
$Values[$cnt++]= $Values[$cnt] + $fields[1];
$Values[$cnt++]= $Values[$cnt] + $fields[2];
$Values[$cnt++]= $Values[$cnt] + $fields[3];
$Values[$cnt++]= $fields[4];
$Values[$cnt++]= $fields[5];
$Values[$cnt++]= $fields[6];
$Values[$cnt++]= $fields[7];
$Values[$cnt++]= "\n";
}
}
close $fh or die "can't close $!";
$cnt=0;
}
print join(",", @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
use strict;
use warnings;
use lib '/var/root/perl5/lib/perl5';
use Text::CSV;
my @rows;
my $fh;
my $csv = Text::CSV->new ( { binary => 1 } ) # should set binary attribute.
or die "Cannot use CSV: ".Text::CSV->error_diag ();
my $cnt = 0;
my @Values;
my @fields;
my @columns;
my $status;
my $dir = './TEST';
foreach my $fp (glob("$dir/*")) {
open my $fh, "<:encoding(utf8)", $fp or die "can't open $!";
while ( my $row = $csv->getline( $fh ) )
{
$status = $csv->parse(@$row);
@fields=$csv->fields;
@columns = $csv->fields();
print($columns[0]); # funktioniert
print($columns[1]); # funktioniert nicht „Use of uninitialized value“
}
$csv->eof or $csv->error_diag();
close $fh or die "can't close $!";
}
#open $fh, ">:encoding(utf8)", "new.csv" or die "new.csv: $!";
#$csv->print ($fh, $_) for @Values;
#close $fh or die "new.csv: $!";
1 2 3 4 5 6 7 8 9 10 11
... while ( my $row = $csv->getline( $fh ) ) { # einzelne Felder ausgeben print $row->[0], "\n"; print $row->[1], "\n"; # alle Felder ausgeben print join( " ", @$row ), "\n"; } ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
while ( my $row = $csv->getline( $fh ) )
{
if($row->[0] >=1101227)
{
$Values[$cnt++]= $row->[0];
$Values[ $cnt] = $Values[$cnt] + $row->[1];
$cnt++;
$Values[ $cnt ] = $Values[$cnt] + $row->[2];
$cnt++;
$Values[ $cnt ] = $Values[$cnt] + $row->[3];
$cnt++;
$Values[$cnt++]= $row->[4];
$Values[$cnt++]= $row->[5];
$Values[$cnt++]= $row->[6];
$Values[$cnt++]= $row->[7];
$Values[$cnt++]= "\n";
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
while ( my $row = $csv->getline( $fh ) ) { if($row->[0] >=1101227) { $Values[$cnt++] = $row->[0]; $Values[$cnt++] += $row->[1]; $Values[$cnt++] += $row->[2]; $Values[$cnt++] += $row->[3]; $Values[$cnt++] = $row->[4]; $Values[$cnt++] = $row->[5]; $Values[$cnt++] = $row->[6]; $Values[$cnt++] = $row->[7]; # $Values[$cnt++] = "\n"; } # reset $cnt per csv-row? #$cnt = 0; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
my $dir = './FXE';
foreach my $fp (glob("$dir/*")) {
open my $fh, "<:encoding(utf8)", $fp or die "can't open $!";
while ( my $row = $csv->getline( $fh ) )
{
if($row->[0] >=1101227)
{
$Values[$cnt++] = $row->[0];
$Values[$cnt++] += $row->[1];
$Values[$cnt++] += $row->[2];
$Values[$cnt++] += $row->[3];
$Values[$cnt++] = $row->[4];
$Values[$cnt++] = $row->[5];
$Values[$cnt++] = $row->[6];
$Values[$cnt++] = $row->[7];
}
}
$csv->eof or $csv->error_diag();
close $fh or die "can't close $!";
[b] $cnt=0;[/b]
}
Quote13536
13536
13536
13536
13536
13536
13536
13536
13536
13536
13624
13624
...
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
use strict;
use warnings;
use lib '/var/root/perl5/lib/perl5';
use Text::CSV;
my @rows;
my $fh;
my $csv = Text::CSV->new ( { binary => 1 } ) # should set binary attribute.
or die "Cannot use CSV: ".Text::CSV->error_diag ();
my $cnt = 0;
my @Values;
my $dir = './FXE';
foreach my $fp (glob("$dir/*")) {
open my $fh, "<:encoding(utf8)", $fp or die "can't open $!";
while ( my $row = $csv->getline( $fh ) )
{
if($row->[0] >=1101227)
{
$Values[$cnt++] = $row->[0];
$Values[$cnt++] += $row->[1];
$Values[$cnt++] += $row->[2];
$Values[$cnt++] += $row->[3];
$Values[$cnt++] = $row->[4];
$Values[$cnt++] = $row->[5];
$Values[$cnt++] = $row->[6];
$Values[$cnt++] = $row->[7];
}
}
print($cnt);
print("\n");
$csv->eof or $csv->error_diag();
close $fh or die "can't close $!";
$cnt=0;
}
1
2
3
$Values[ $cnt++ ] = $Values[$cnt] + $fields[1];
$Values[ $cnt++ ] = $Values[$cnt] + $fields[2];
$Values[ $cnt++ ] = $Values[$cnt] + $fields[3];
1
2
3
$Values[ $cnt++ ] = $Values[$cnt]//0 + $fields[1];
$Values[ $cnt++ ] = $Values[$cnt]//0 + $fields[2];
$Values[ $cnt++ ] = $Values[$cnt]//0 + $fields[3];
1
2
3
4
while ( my $row = $csv->getline( $fh ) ) {
$row->[2] =~ m/pattern/ or next; # 3rd field should match
push @rows, $row;
}
$Values[$cnt++] = ...;
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
#!/usr/bin/perl use warnings; use strict; my @fields; my $line; my @lines; my @values; my $i; my @resultline; my $dir = './FXE'; foreach my $fp (glob("$dir/*")) { open my $fh, "<:encoding(utf8)", $fp or die "can't open $!"; while ($line = <$fh>) { chomp $line;; @fields = split(/\,/ , $line); @values = (); if($fields[0] >= 1101227) { push(@values, $fields[0]); foreach $i (1 .. 3) { push(@values, $values[$i - 1] + $fields[$i]); } foreach $i (4 .. 7) { push(@values, $fields[$i]); } push(@values, "\n"); my $resultline = join(',', @values); print $resultline; push(@lines, $resultline); } } close $fh or die "can't close $!"; } # foreach $i (@lines) { # print $i; # }
Guest VionigVielen Dank für deine Hilfe.
Ich hab noch aus dem $i-1 das "-1" entfernt, dann stimmt die Ausgabe auf die Datei selbst bezogen.
So ganz blicke ich noch nicht durch deinen Code, das einlesen funktioniert, nur die Ausgabe ist nun nicht eine Zusammenfassung der Werte aller Dateien ab dem Datum, sondern jedesmal eine Ausgabe pro Datei. Kann es sein das "push()" daher nicht die richtige Lösung ist?
Quote...
1170828,-1,0,1,0.44,0.45,0.55,791579,115.71
1170829,1,0,0,-0.61,-0.07,0.65,824215,115.64
1170830,1,0,0,-0.35,-0.79,0.81,1107319,114.85
1170831,1,0,0,0.50,0.18,0.56,641804,115.03
1170901,1,0,0,-0.38,-0.44,0.56,557809,114.59
1170905,1,0,0,0.27,0.55,0.72,428555,115.14
1170906,1,0,0,-0.12,-0.01,0.37,944258,115.13
1170907,1,0,0,0.00,0.99,1.07,1222680,116.12
1170908,1,0,0,-0.19,0.05,0.32,247485,116.17
1170911,1,0,0,-0.21,-0.71,0.75,503383,115.46
1170912,1,0,0,0.29,0.12,0.34,141956,115.58
1170913,1,0,0,-0.70,-0.83,0.90,498113,114.75
1170914,1,0,0,0.31,0.24,0.48,260200,114.99
Quote1170828,-1,0,0,0.44,0.45,0.55,791579,115.71
1170829,-1,0,0,-0.61,-0.07,0.65,824215,115.64
1170830,-1,0,0,-0.35,-0.79,0.81,1107319,114.85
1170831,-1,0,1,0.50,0.18,0.56,641804,115.03
1170901,1,0,0,-0.38,-0.44,0.56,557809,114.59
1170905,1,1,0,0.27,0.55,0.72,428555,115.14
1170906,1,1,0,-0.12,-0.01,0.37,944258,115.13
1170907,1,1,0,0.00,0.99,1.07,1222680,116.12
1170908,1,1,0,-0.19,0.05,0.32,247485,116.17
1170911,1,1,-1,-0.21,-0.71,0.75,503383,115.46
1170912,-1,0,1,0.29,0.12,0.34,141956,115.58
1170913,1,0,0,-0.70,-0.83,0.90,498113,114.75
1170914,1,0,0,0.31,0.24,0.48,260200,114.99
Quote,1170828,-2,0,1,0.44,0.45,0.55,791579,115.71,
,1170829,0,0,0,-0.61,-0.07,0.65,824215,115.64,
,1170830,0,0,0,-0.35,-0.79,0.81,1107319,114.85,
,1170831,0,0,1,0.50,0.18,0.56,641804,115.03,
,1170901,2,0,0,-0.38,-0.44,0.56,557809,114.59,
,1170905,2,1,0,0.27,0.55,0.72,428555,115.14,
,1170906,2,1,0,-0.12,-0.01,0.37,944258,115.13,
,1170907,2,1,0,0.00,0.99,1.07,1222680,116.12,
,1170908,2,1,0,-0.19,0.05,0.32,247485,116.17,
,1170911,2,1,-1,-0.21,-0.71,0.75,503383,115.46,
,1170912,0,0,1,0.29,0.12,0.34,141956,115.58,
,1170913,2,0,0,-0.70,-0.83,0.90,498113,114.75,
,1170914,2,0,0,0.31,0.24,0.48,260200,114.99,
Quote,1170913,-3,5,2,-0.70,-0.83,0.90,498113,114.75,
,1170914,1,5,0,0.31,0.24,0.48,260200,114.99,
,1170830,0,3,-1,-0.35,-0.79,0.81,1107319,114.85,
,1170831,-2,3,0,0.50,0.18,0.56,641804,115.03,
,1170901,-2,3,-1,-0.38,-0.44,0.56,557809,114.59,
,1170905,-4,2,-1,0.27,0.55,0.72,428555,115.14,
,1170906,-6,1,0,-0.12,-0.01,0.37,944258,115.13,
,1170907,-6,4,0,0.00,0.99,1.07,1222680,116.12,
,1170908,-6,4,3,-0.19,0.05,0.32,247485,116.17,
,1170911,0,4,1,-0.21,-0.71,0.75,503383,115.46,
,1170912,2,5,-1,0.29,0.12,0.34,141956,115.58,
,1170913,0,-1,-1,-0.70,-0.83,0.90,498113,114.75,
,1170914,-2,3,0,0.31,0.24,0.48,260200,114.99,
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
#!/usr/bin/perl use warnings; use strict; my $fieldsep = ","; sub addLine { my $rline = shift; my $line = shift; my @r = split($fieldsep, $rline); my @l = split($fieldsep, $line); $r[0] += $l[1]; $r[1] += $l[2]; $r[2] += $l[3]; return join($fieldsep, @r); } my %results = (); my $dir = './FXE'; foreach my $fp (glob("$dir/*")) { open my $fh, "<:encoding(utf8)", $fp or die "can't open $!"; while (my $line = <$fh>) { chomp $line; my @fields = split($fieldsep, $line); my $date = $fields[0]; if($date < 1101227) { next; } if (exists($results{$date})) { $results{$date} = addLine($results{$date}, $line); } else { my @f = @fields; shift(@f); $results{$date} = join($fieldsep, @f); } } close $fh or die "can't close $!"; } foreach my $i (sort(keys(%results))) { print "$i,$results{$i}\n"; }