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

时间:2016-12-26 06:11:32

标签: perl

我使用mysql查询从linux命令转储了下表。 我的问题是如何使用列标题NAME将列PE​​ND AND RUN的每个名称的值相加。我已经匹配了每个名字然后计算但我需要根据标题名称(即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列的总和应存储在另一个不同的变量中。添加应该与第1列中所有行的匹配(即NAME)

2 个答案:

答案 0 :(得分:1)

如果我正确理解了这个问题,您需要为pend的每个值加总runname列。您可以使用hash of hashes,其中顶级键是名称,每个值都是包含要添加的列的键的哈希:

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

输出:

{
  adice_long  => { pend => 199, run => 107 },
  adice_ncsim => { pend => 2, run => 6 },
  calibre     => { pend => 0, run => 0 },
}

答案 1 :(得分:0)

我想,并知道这是否会对你有所帮助:

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   -

由于