如何使用Perl添加每一行并在每列的末尾打印? [英] How to add every rows and print at the end of the every column using Perl?

查看:81
本文介绍了如何使用Perl添加每一行并在每列的末尾打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

perl a.pl> result.txt



我生成的输出:



perl a.pl > result.txt

my generated output:

name:asf192lin1	total slots used:0	total slots free:8	

name:asf192lin10	total slots used:7	total slots free:9	

name:asf192lin11	total slots used:6	total slots free:10	

name:asf192lin12	total slots used:8	total slots free:8	

name:asf192lin3	total slots used:0	total slots free:16	

name:asf192lin4	total slots used:8	total slots free:8	

name:asf192lin5	total slots used:8	total slots free:8	

name:asf192lin6	total slots used:8	total slots free:8	



如何使用perl将evry行相加并打印列末尾的总数



我尝试过:




How can i sum the evry rows and print the total at the end of the column using perl

What I have tried:

<pre>#!/usr/local/bin/perl
%namesum;
open(File,"tmp.txt")||die "error";
@array1=<File>;
close File;
open(File1,"<","gen_machines_data.txt")||die "error";
@file=<File1>;
foreach $line (@array1)
{
chomp $line;
}
foreach $line (@file)
{
$line=~s/[\{\}]//g; 
@parts=split(/\s+/,$line); 
@input=($slots_used,$slots_max)=split('/',$parts[2]);  
$name=substr($parts[0],0,11);
$namesum{$name}[0]+=$input[0]; 
$namesum{$name}[1]+=$input[1];
} 
$sum=0;
$total=0;
for $key (sort keys %namesum){
$slots_free = $namesum{$key}[1] - $namesum{$key}[0];
print "\nname:".$key."\t";
print "total slots used:".$namesum{$key}[0]."\t";
print "total slots free:".$slots_free."\t";
print "\n";
#$total+=$slots_free;
#print $total;
#$sum+=$input[0];
}
#print $sum;

推荐答案

line(@ array1)
{
chomp
line (@array1) { chomp


line;
}
foreach
line; } foreach


line(@file)
{
line (@file) {


这篇关于如何使用Perl添加每一行并在每列的末尾打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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