问题的同时创造出CSV Perl中的数组 [英] Issues while creating CSV out of an array in Perl

查看:170
本文介绍了问题的同时创造出CSV Perl中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题的简介:

我有需要的数组转换为 CSV 。我面临与所需的输出,而不是转化itsef问题。

I have an array which needs to be converted to CSV. I am facing problems with the desired output, and not the conversion itsef.

我的code:

use strict;
use warnings;
use Text::CSV;
use Text::CSV_XS;
use Data::Dumper; 

my (@first_set, @final_set); 
my $file = do {
    open my $fh, '<', '<file_name>.txt' or die "Unable to open file for input: $!";
    local $/;
    <$fh>;
};

@first_set = ( $file =~ m/<regex>/g)  ;
while ( @first_set ) {
    @final_set = splice @first_set, 0, 26;
    open(my $output, '>', 'mpc7_file.csv') or die "Couldn't open file file.csv, $!";
    my $csv = Text::CSV -> new ( { sep_char => ",", eol => "\n", binary => 1, quote_char => undef } );
    foreach my $element ( @final_set ) { 
        $csv -> print ( \*$output, \@final_set );
    }   
    close($output) or die "Couldn't close file properly";
}

@final_set 内容:

Contents of @final_set:

10:38:49 788 56 51 56 61 56 59 56 51 56 80 56 83 56 50 45 42 45 50 45 50 45 43 45 54
10:38:51 788 56 51 56 61 56 59 56 51 56 80 56 83 56 50 45 42 45 50 45 50 45 43 45 54

CSV 文件的内容:

Contents of the CSV file:

10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54

所需的输出:

我的 CSV 文件应包含 @final_set (用逗号隔开的每个元素)。

My CSV file should contain the @final_set (each element separated by comma).

10:38:49,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54
10:38:51,788,56,51,56,61,56,59,56,51,56,80,56,83,56,50,45,42,45,50,45,50,45,43,45,54

问:

我在哪里去了?我真的不能够理解我的错误。请帮忙。

Where am I going wrong? I'm not really able to understand my mistake. Please help.

编辑:

即使我移动 CSV 文件创建块之外,而循环,输出仍然是相同的。

Even if I move the CSV file creation block outside of while loop, the output is still the same.

推荐答案

好吧,我看到你在做什么。问题是, @final_set 改变周围的,而每一次循环,以及你是否在全球范围宣布它这样或局部你应该,你仍然需要在每次改变时间打印的内容

Okay I see what you're doing. The problem is that @final_set changes each time around the while loop, and whether you declare it globally like that or locally as you should you still need to print its contents each time it changes

但也没有必要使用文字:: CSV 这样的简单数据。正如你所看到的,每一个变量声明的在其点第一次使用的,哪个是最好的做法在大多数语言

There's also no need to use Text::CSV with such simple data. As you can see, every variable is declared at its point of first use, which is best practice in most languages

use strict;
use warnings;

my $in_file  = '<file_name>.txt';
my $out_file = 'mpc7_file.csv';

my $data = do {
    open my $fh, '<', $in_file or die qq{Unable to open "$in_file" for input: $!};
    local $/;
    <$fh>;
};

my @first_set = $data =~ /<regex>/g;

open my $out_fh, '>', $out_file or die qq{Unable to open "$out_file" for output: $!};

while ( @first_set ) {
    my @final_set = splice @first_set, 0, 26;
    print $out_fh join(',', @final_set), "\n";
}

close $out_fh or die qq{Unable to close "$out_file": $!};

这篇关于问题的同时创造出CSV Perl中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆