Thread File sortieren (13 answers)
Opened by Henri at 2015-08-05 13:40

Gast Henri
 2015-08-05 13:40
#181727 #181727
Hallo Leute,
ich benötige noch einmal ihre Hilfe, ich habe hier ein Code der leider nicht gut lauft, ich möchte meine DATA.csv spalten, so dass am Ende bekomme ich nur die spalte mit "false" oben.

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
#!/usr/bin/env perl

use strict;
use warnings;
use feature qw(say);

my $ts = <DATA>;

my @flags = (true=> (<DATA> =~ /(false|true)/g));
my @header = (<DATA> =~ /([a-z_]+)/g);

use YAML::XS;
print Dump \@header, \@flags;

my @selected = grep $flags[ $_ ] eq 'false', 0 .. $#flags;

my %output_fh;
for my $i (@selected) {
    my $fname = "file$i";
    open my $fh, '>', $fname
        or die "Cannot open '$fname' for writing: $!";
    say $fh $header[$i];
    $output_fh{$i} = $fh;
}

while (my $line = <DATA>) {
    next unless $line =~ /\S/;
    # next if $line =~ /^false/;

    my @cols = split qr{ , }x, $line;
    for my $i (@selected) {
        say { $output_fh{$i} } join(q{ : }, $cols[0], $cols[$i]);
    }
}



Code: (dl )
1
2
3
4
5
6
7
8
9
------------------------------------DATA.csv-----------------------------------------------

2015.07.23 16:47:23
false, false, true, false, true, false,
defau, ame_a, ame_m, ame_uc, ame_f, ams_l, en_affi,
true DPA01, fault, fault, fault, fault, fault, 1,
false DPA02, , , , , fault, 1,
false DPA03, fault, fault, olt, fault, fault, 1,
false DPA01, fault, at, fault, 2, fault, 1,


ich möchte gern am Ende, so eine Ausgabe bekommen:


Code: (dl )
1
2
3
4
5
6
---------------------------------ame_a-----------------------------------
ame_a,
fault,
,
fault,
fault,

Code: (dl )
1
2
3
4
5
6
------------------------------ame_m-------------------------------------
ame_m,
fault,
,
fault,
at,


Code: (dl )
1
2
3
4
5
6
----------------------------------ame_f-----------------------------------
ame_f,
fault,
,
fault,
2,


Code: (dl )
1
2
3
4
5
6
--------------------------------en_affi----------------------------------
en_affi,
1,
1,
1,
1,

Last edited: 2015-08-05 13:41:50 +0200 (CEST)

View full thread File sortieren