2013-02-25 46 views
0

Perl中最简单的代码是什么,它可以通过CSV文件并创建数学计算?
更具体:
我有一个包含一个CSV文件:从Perl中的CSV文件获取信息

Input  Execution  Output Total 
608   124800   1856 127264 
512   124960   1920 127392 
864   124992   1888 127744 

,我想计算每列的标准偏差。 (用于输入\ exection \输出\总)
让我们假设这个文件是下:myDocuments\me\info.csv

我已经成功地写入该循环csv文件,并计算柱输入的标准偏差的函数。但如果我希望它是一个通用函数,我可以从代码中的几个地方调用,该函数接收一个字符串,如函数应该对其进行计算的“Input”或“exection”字符串,我该怎么做?

+0

是您的输入CSV文件?它看起来像你在列中。你在做什么数学?这是你的输出吗?你的输入是什么样的?你想表演什么数学?你知道多少Perl?我写作Perl脚本生活。我很乐意以我的标准费率115美元/小时完成这项工作。 – 2013-02-25 15:37:34

+0

只是做一些研究如何吸取文本文件并解析出分隔字段。 – amphibient 2013-02-25 15:37:40

回答

0

大多数人只是使用split来修补解析器,但快速和强大的wheel已经存在用于解析和生成CSV(和制表符分隔的数据)。

但是你问到什么和你看起来是不同的东西。你发布的内容不是CSV,而Text :: CSV_XS不会这样做。但是,真的很简单。

可以使用

printf($fh "%-11d %-15d %-7d %d\n", @fields); 

所以你的问题不是关于任意列运行分析使用

my @fields = split(' ', $line); 

线路,您可以重新创建该文件(如有必要)。这意味着你需要有可以按名称访问的数据列。那叫喊数组的哈希..

my $headers = <$fh>; 
my @headers = split ' ', $header; 

my %data; 
while (<$fh>) { 
    my @row = split; 
    for (0..$#header) { 
     push @{ $data{ $headers[$_] } }, $row[$_]; 
    } 
} 

some_func(@{ $data{Execution} }); 
+0

我已经成功编写了循环csv文件并计算列输入的标准偏差的函数。但如果我希望它是一个通用函数,我可以从severl中调用,这些代码会引入一个像“Input”或“exection”这样的函数应该在其中进行计算的字符串 - 我该怎么做? – user1584314 2013-02-25 16:08:55

+0

更新了我的答案。 – ikegami 2013-02-25 23:18:07

1

这会做你需要什么

use strict; 
use warnings; 

use List::Util 'sum'; 

my @data; 
while (<DATA>) { 
    push @data, [ split ]; 
} 

my $headers = shift @data; 
my $n = @data; 

for my $i (0 .. $#{$data[0]}) { 

    my $mean = sum(map $_->[$i], @data)/$n; 
    my $stddev = sqrt(sum(map { ($_->[$i] - $mean) ** 2 } @data)/$n); 

    printf "%-9s: Mean %.3f, Standard Deviation %.3f\n", 
     $headers->[$i], $mean, $stddev; 
} 


__DATA__ 
Input  Execution  Output Total 
608   124800   1856 127264 
512   124960   1920 127392 
864   124992   1888 127744 

输出

Input : Mean 661.333, Standard Deviation 148.569 
Execution: Mean 124917.333, Standard Deviation 83.989 
Output : Mean 1888.000, Standard Deviation 26.128 
Total : Mean 127466.667, Standard Deviation 202.947 
+0

哇,这看起来如此复杂...你对我的代码有什么看法? – user1584314 2013-02-25 16:16:58

+0

您的代码不会处理您在问题中显示的数据,并且只会计算第一列的统计数据。我的代码在单个语句中执行了几个步骤,因此它可能比您想要的更紧凑。 – Borodin 2013-02-25 16:21:34

0
the function is: 
sub parse_standard 
{ 

    my $filename = "myDocuments\\me\\info.csv"; 
     #first, calculate the Avg and the number of rows 
    open(INPUT, $filename) or die "Cannot open $filename"; 

    # Read the header line. 
    my $line = <INPUT>; 
    my $sum = 0 ; 
    my $counter = 0; 
    #Read the lines one by one. 
    while($line = <INPUT>) 
    { 
     chomp($line); 
     my ($Input,$Execution,$Output,$Total,$SelfTest,$Log_Location,$Log_Name) = split(',', $line); 
     $sum = $sum + $Input; 
     $counter = $counter +1; 

    } 

    $avg = $sum/$counter ; 
#second , calculate the standard deviation 
    open(INPUT, $filename) or die "Cannot open $filename"; 
    my $line = <INPUT>; 
    my $sum = 0 ; 
    #Read the lines one by one. 
    while($line = <INPUT>) 
    { 
     chomp($line); 
     my ($Input,$Execution,$Output,$Total,$SelfTest,$Log_Location,$Log_Name) = split(',', $line); 
     $diff = ($Input-$avg);  
     $square = $diff * $diff ;  
     $sum = $sum + $square; 

    } 

    $tosqrt = $sum/$counter; 
    $answer = sqrt($tosqrt); 
    print "standard deviation is $answer\n"; 
    close(INPUT); 
} 

parse_standard();