Perl 查找整个文件中仅有的空值列

Perl 查找整个文件中仅有的空值列,perl,hash,Perl,Hash,使用下面的csv数据 name,place,animal a,, b,, a,, ,b, 名称字段在3行中可用,但在1行中不可用 位置字段在1行中可用,但在3行中不可用 动物字段在所有行中为空->获取这些列名称1 我只想在所有行中都为空时获取列名 我正试图为同样的问题编写一个perl脚本,但不确定如何解决这个问题 step 1: Check all the columns in first row, if any column is not empty ,dont search it in n

使用下面的csv数据

name,place,animal
a,,
b,,
a,,
,b,
名称字段在3行中可用,但在1行中不可用
位置字段在1行中可用,但在3行中不可用
动物字段在所有行中为空->获取这些列名称1

我只想在所有行中都为空时获取列名

我正试图为同样的问题编写一个perl脚本,但不确定如何解决这个问题

step 1: Check all the columns in first row, if any column is not empty ,dont search it in next row
step2: keep repeating step1 in a loop  and finally we will get the output.and this brings down the complexity as we are not bothered about columns that have value even once.
我将实现代码并将其发布在这里

但是如果你有什么新的想法,请告诉我


感谢没有引号和转义的CSV文件,到目前为止只保留空列的散列。逐行读取文件,从哈希中删除任何非空列:

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

chomp( my @column_names = split /,/, <> );
my %empty;
@empty{ @column_names } = ();

while (<>) {
    chomp;
    my @columns = split /,/;
    for my $i (0 .. $#columns) {
        delete $empty{ $column_names[$i] } if length $columns[$i];
    }
}

say for keys %empty;

对于没有引号和转义的CSV文件,到目前为止只保留空列的散列。逐行读取文件,从哈希中删除任何非空列:

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

chomp( my @column_names = split /,/, <> );
my %empty;
@empty{ @column_names } = ();

while (<>) {
    chomp;
    my @columns = split /,/;
    for my $i (0 .. $#columns) {
        delete $empty{ $column_names[$i] } if length $columns[$i];
    }
}

say for keys %empty;

在处理行时,更新一个辅助数组,该数组跟踪每个字段的真值

如果新行中的任何字段为非空,则数组的相应元素将翻转为true;否则它将保持错误。最后,数组伪元素的索引标识空列的索引

use warnings;
use strict;
use feature 'say';    
use Text::CSV;

my $file = 'cols.csv';
my $csv = Text::CSV->new( { binary => 1 } ) 
    or die "Cannot use CSV: " . Text::CSV->error_diag (); 

open my $fh, '<', $file or die "Can't open $file: $!";

my @col_names = @{ $csv->getline($fh) };

my @mask;
while (my $line = $csv->getline($fh)) {
    @mask = map { $mask[$_] || $line->[$_] ne '' } (0..$#$line);
}

for (0..$#mask) {
    say "Column \"$col_names[$_]\" is empty" if not $mask[$_];
}
使用警告;
严格使用;
使用特征“说”;
使用Text::CSV;
my$file='cols.csv';
我的$csv=Text::csv->new({binary=>1})
或死亡“无法使用CSV:”。Text::CSV->error_diag();

在处理行时打开my$fh,“,更新跟踪每个字段真值的辅助数组

如果新行中的任何字段为非空,则数组的相应元素将翻转为true;否则它将保持错误。最后,数组伪元素的索引标识空列的索引

use warnings;
use strict;
use feature 'say';    
use Text::CSV;

my $file = 'cols.csv';
my $csv = Text::CSV->new( { binary => 1 } ) 
    or die "Cannot use CSV: " . Text::CSV->error_diag (); 

open my $fh, '<', $file or die "Can't open $file: $!";

my @col_names = @{ $csv->getline($fh) };

my @mask;
while (my $line = $csv->getline($fh)) {
    @mask = map { $mask[$_] || $line->[$_] ne '' } (0..$#$line);
}

for (0..$#mask) {
    say "Column \"$col_names[$_]\" is empty" if not $mask[$_];
}
使用警告;
严格使用;
使用特征“说”;
使用Text::CSV;
my$file='cols.csv';
我的$csv=Text::csv->new({binary=>1})
或死亡“无法使用CSV:”。Text::CSV->error_diag();

打开我的$fh,'如果文件有1亿行,第一个代码是否有效,或者我们是否有办法从最初包含整个文件数据的数组中删除列。@LearningCpp:TITS(尝试查看)。这两个程序在内存中只保留空列名称的散列。如果文件中有1亿行,第一个代码会起作用吗?或者我们是否有办法从最初包含整个文件数据的数组中删除列。@LearningCpp:TITS(请尝试查看)。这两个程序在内存中只保留到目前为止空列名称的散列。