Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在perl中使用列标题添加多行?_Perl - Fatal编程技术网

如何在perl中使用列标题添加多行?

如何在perl中使用列标题添加多行?,perl,Perl,我使用mysql查询从linux命令中转储了下表。 我的问题是如何使用列标题名称对列PEND和run的每个名称的值求和。在这里,我已经匹配了每个名称,然后进行了计算,但我需要根据标题名称i.e name进行计算。我该如何做 我的输入文件: NAME PRIO SUB PEND RUN LALLOT adice_long 5 980766 199 107 - adice_ncsim 7 390188 2 6 cal

我使用mysql查询从linux命令中转储了下表。 我的问题是如何使用列标题名称对列PEND和run的每个名称的值求和。在这里,我已经匹配了每个名称,然后进行了计算,但我需要根据标题名称i.e name进行计算。我该如何做

我的输入文件:

NAME            PRIO SUB     PEND RUN LALLOT  
adice_long      5    980766  199  107 - 

adice_ncsim     7    390188  2    6 

calibre         15   580     0    0   - 
我尝试了以下代码:

$DBH->do("USE $str1m;");

my $stmt = "select distinct * from queues ;";
my $sth = $DBH->prepare( $stmt );
$sth->execute() or print "Could not insert data";
while (my @columns = $sth->fetchrow_array() ) {
    if($columns[2] =~ /adice/i)
    {
        $pend_count{adice} +=$columns[4];
        $run_count{adice} +=$columns[5]; 
    }
    elsif($columns[2] =~ /calibre/i)
    {
        $pend_count{calibre} +=$columns[4];
        $run_count{calibre} +=$columns[5]; 
    }   
    elsif($columns[2] =~ /vcs/i)
    {
        $pend_count{vcs} +=$columns[4];
        $run_count{vcs} +=$columns[5]; 
    }     
    elsif($columns[2] =~ /spectre/i)
    {
        $pend_count{spectre} +=$columns[4];  
        $run_count{spectre} +=$columns[5]; 
    }       
    elsif($columns[2] =~ /Incisive/i)
    {
        $pend_count{incisive} +=$columns[4];
        $run_count{incisive} +=$columns[5]; 
    }     

    else
    {
        $pend_count{others} +=$columns[4];
        $run_count{others} +=$columns[5]; 

    }
}
foreach $feature ( keys %pend_count)
{
    $stmt = "INSERT INTO job_status(time,job_type,pending_qty,running_qty) VALUES(\"$current_time\",\"$feature\",\"$pend_count{$feature}\",\"$run_count{$feature}\")";
     $sth = $DBH->prepare( $stmt );
    $sth->execute() or print "Could not insert data";
}
$sth->finish;
$DBH->disconnect();
预期输出:


第4列的和应存储在一个变量中,第5列的和应存储在另一个不同的变量中。加法应与第1i.e列中的所有行匹配。NAME

我只是想知道这是否会帮助你:

my (@columns,$columns4,$columns5) = ();
while(<DATA>)
{
    #Each row we are splitting by tab delimit
    @columns = split "\t", $_;

    #Checking the column fourth not equal to null
    if($columns[3] ne '')
    {
        #Store the value which is in column 4th
        $columns4 += $columns[3];
    }
    #Checking the column fifth not equal to null
    if($columns[4] ne '')
    {
        #Store the value which is in column 5th
        $columns5 += $columns[4];
    }
}

print "col4: $columns4\tcol5: $columns5\n";

__DATA__
NAME    PRIO    SUB PEND    RUN LALLOT  
adice_long  5   980766  199 107 - 
adice_ncsim 7   390188  2   6   
calibre 15  580 0   0   -

谢谢

如果我正确理解了这个问题,您希望对每个name值的pend和run列求和。您可以使用,其中顶级键是名称,每个值是包含要添加的列的键的哈希:

use strict;
use warnings;

use Data::Dump;
use DBI;

# set up in-memory database that should be close enough for this example
my $dbh = DBI->connect("dbi:SQLite:dbname=:memory:", undef, undef, {
    RaiseError => 1,
    PrintError => 0,
});

# create fake schema
$dbh->do(q{
    create table queues (
        name varchar(255),
        prio int,
        sub int,
        pend int,
        run int,
        lallot int
    )
});

# load fake data
my $sth = $dbh->prepare(q{
    insert into queues (name, prio, sub, pend, run)
    values (?, ?, ?, ?, ?)
});

while (<DATA>) {
    $sth->execute(split);
}

# end of set-up code; on to the actual answer...
$sth = $dbh->prepare(q{select * from queues});
$sth->execute;

my %sums;

while (my $row = $sth->fetchrow_hashref) {
    for my $field (qw(pend run)) {
        $sums{$row->{name}}{$field} += $row->{$field};
    }
}

dd(\%sums);

__DATA__
adice_long      5    980766  199  107
adice_ncsim     7    390188  2    6
calibre         15   580     0    0

你能说清楚一点吗?您的预期输出是什么?简要介绍一下预期输出吗?我的预期输出应该是第4列的和,第5列的和应该存储在不同的位置variables@ssr1012I我猜,您需要类似$pend_count{adice}->{pend}+=$columns[4]和$pend_count{adice}->{RUN}+=$columns[5]查询失败时抛出的错误消息具有误导性。你说你不能插入,而实际上你的查询是select。我不认为这是OP所期望的。与第1列中所有行的匹配不发生加法。我的预期输出应该是第4列的和,第5列的和应该存储在不同的变量中。这就是我所做的。@serenesat:请解释不发生加法的位置。此代码有效吗..因为我没有从同一个变量中获得值的加法code@ssr1012col4:col5:未输出任何值。@ssr1012
{
  adice_long  => { pend => 199, run => 107 },
  adice_ncsim => { pend => 2, run => 6 },
  calibre     => { pend => 0, run => 0 },
}