免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3323 | 回复: 3
打印 上一主题 下一主题

求助--perl6实现分类统计频数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-09-26 18:59 |只看该作者 |倒序浏览
本帖最后由 w123456_cu 于 2015-09-27 20:18 编辑

文件有三列分别为:文章名、文章作者、该论文被引用的次数。如下:

  1. # ----------------data.txt----------------------------
  2. #
  3. # paper_name  author_name  cited_num
  4. # name1        author1     2
  5. # name2        author2     1
  6. # name3        author1     3
  7. # name4        author4     4
  8. # name5        author1     2
  9. # name6        author3     7
  10. # name7        author4     5
  11. # name8        author1     1
  12. # name9        author4     2
  13. # name10       author3     5
  14. # ...
  15. # ----------------------------------------------------
复制代码
希望统计: 同一作者、相同被引用次数出现的频率(例如,author1被引用次数为2的文章有2篇)。希望输出的矩阵如下:

  1. #-------------------output------------------------------
  2. # cited_num   author1  author2   author3  author4   ...
  3. # 1               1       1          0       0
  4. # 2               2       0          0       1
  5. # 3               1       0          0       0
  6. # 4               0       0          0       1
  7. # 5               0       0          1       1
  8. # 6               0       0          0       0
  9. # 7               0       0          1       0
  10. #----------------------------------------------------
复制代码
perl入门,还请多指点。
  1. use v6;

  2. my $file = open 'data.txt';
  3. my %h;

  4. for $file.lines {
  5.     my ($author, $num) = .words[1,2];
  6.     %h{$author}{$num}++;
  7. }

  8. say %h.fmt;
复制代码

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
2 [报告]
发表于 2015-09-28 01:31 |只看该作者
biru:
  1. #!perl6
  2. my ( $da, @ta ) = $=finish.lines;
  3. my ( @this, %ha, %sh );

  4. for @ta {
  5.     my ( $, $sp, $lit ) = .words;
  6.     %ha{$lit}{$sp}++;
  7.     %sh{$sp} ||= 1;
  8. }

  9. say join "\t", $da.words[*-1], @this = %sh.keys.sort;

  10. for %ha.sort: +*.key {
  11.     say join "\t", $^p.key, @this.map: { $p.value{$_} || 0 };
  12. }


  13. =finish
  14. paper        author      cited
  15. name1        author1     2
  16. name2        author2     1
  17. name3        author1     3
  18. name4        author4     4
  19. name5        author1     2
  20. name6        author3     7
  21. name7        author4     5
  22. name8        author1     1
  23. name9        author4     2
  24. name10       author3     5
  25. name11       author3     11
复制代码

论坛徽章:
0
3 [报告]
发表于 2015-09-30 10:16 |只看该作者
本帖最后由 w123456_cu 于 2015-09-30 16:20 编辑
  1. +*.key
  2. $^p.key
  3. $p.value{$_}
复制代码
The ^ twigil declares a formal positional parameter to blocks or subroutines.
If you have self-declared a parameter using $^a once, you may refer to it using only $a thereafter.

论坛徽章:
0
4 [报告]
发表于 2015-09-30 10:18 |只看该作者
从perl 6起步, 有前途
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP