加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl 求和

发布时间:2020-12-16 00:31:53 所属栏目:大数据 来源:网络整理
导读:CU上一道题,利用perl求和 请问如何求和? a?2? a?3? a?4? b?4? b?5? c?6? d?4? d?1? 相同的字母求和。 直接上代码哦, #!/bin/env?perl? ? use?strict?"subs";? use?List::Util?qw/sum/;? ? my?% hash ?=?();? ? open?my?$file,' ','a.txt'?or?die?"$!n";?

CU上一道题,利用perl求和

请问如何求和?

 
 
  1. a?2?
  2. a?3?
  3. a?4?
  4. b?4?
  5. b?5?
  6. c?6?
  7. d?4?
  8. d?1?

相同的字母求和。

直接上代码哦,

 
 
  1. #!/bin/env?perl?
  2. ?
  3. use?strict?"subs";?
  4. use?List::Util?qw/sum/;?
  5. ?
  6. my?%hash?=?();?
  7. ?
  8. open?my?$file,'<','a.txt'?or?die?"$!n";?
  9. ?
  10. while?(<$file>)?{?
  11. ???????chomp;?
  12. ???????my?@array?=?split;?
  13. ???????$hash{$array[0]}?=?[]?unless?exists?$hash{$array[0]};?
  14. ???????push?@{$hash{$array[0]}},$array[1];?
  15. }?
  16. ?
  17. close?$file;?
  18. ?
  19. for?my?$str?(sort?keys?%hash)?{?
  20. ????my?@tmp_array?=?@{$hash{$str}};?
  21. ????printf?"%-3s",$str;?
  22. ????printf?sum?@tmp_array;?
  23. ????printf?"n";?
  24. }?

output:

 
 
  1. [root@rhel172_16_3_248?henry]#?perl?a.pl?
  2. a??9?
  3. b??9?
  4. c??6?
  5. d??5?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读