免费注册 查看新帖 |

Chinaunix

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

$num = ${$averages{$entry[0]}}[1] 这个代表什么意思啊? [复制链接]

论坛徽章:
1
操作系统版块每日发帖之星
日期:2015-09-03 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-11-01 10:49 |只看该作者 |倒序浏览
求教各位大神,在这段代码中,这二句代表什么意思啊?

$temp = ${$averages{$entry[0]}}[0];
$num = ${$averages{$entry[0]}}[1];

假设: entry[0]= 姓名   entry[1] = 学号  entry[2] = 成绩

我对$temp的理解是:如果姓名已经在hash中存在,就将该姓名赋值给$temp
但是$num是什么情况啊?



#!/usr/bin/perl
# This program reads a text file passed as argument
# and computes a mark average for each student
# The format of each line of the input file is:
# name surname,subject_number,mark
# The program uses a hash of arrays with 2 elements each
# The formula used for the recurring average is:
# new_average = (old_average * N + current_value) / (N + 1)


open (INFILE, $ARGV[0]) or die "An input file is required as argument\n";

%averages = ();
while(<INFILE>)
{
  chomp($_);
  @entry = split(/,/, $_);
  #print "1: $entry[0]; 2: $entry[1]; 3: $entry[2]\n"; # test correct reading

  if (exists $averages{$entry[0]})
  {
    # this student is already in the hash:
    $temp = ${$averages{$entry[0]}}[0];
    $num = ${$averages{$entry[0]}}[1];
    $temp = ($temp * $num + $entry[2]) / ($num + 1);
    $num++;
    ${$averages{$entry[0]}}[0] = $temp;
    ${$averages{$entry[0]}}[1] = $num;
  }
  else
  {
    # this is a new student:
    ${$averages{$entry[0]}}[0] = $entry[2];
    ${$averages{$entry[0]}}[1] = 1;
  }
}

论坛徽章:
26
2015亚冠之胡齐斯坦钢铁
日期:2015-06-25 21:40:202015亚冠之柏斯波利斯
日期:2015-08-31 17:03:192015亚冠之柏斯波利斯
日期:2015-11-07 13:10:00程序设计版块每日发帖之星
日期:2015-11-10 06:20:00每日论坛发贴之星
日期:2015-11-10 06:20:00程序设计版块每日发帖之星
日期:2015-11-26 06:20:00程序设计版块每日发帖之星
日期:2015-12-02 06:20:00黄金圣斗士
日期:2015-12-07 17:57:4615-16赛季CBA联赛之天津
日期:2015-12-23 18:34:14程序设计版块每日发帖之星
日期:2016-01-02 06:20:00程序设计版块每日发帖之星
日期:2016-01-06 06:20:00每日论坛发贴之星
日期:2016-01-06 06:20:00
2 [报告]
发表于 2015-11-01 12:55 |只看该作者
回复 1# palladiosh

# new_average = (old_average * N + current_value) / (N + 1)

可见,$num就是N啊

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
3 [报告]
发表于 2015-11-01 13:05 |只看该作者
本帖最后由 MMMIX 于 2015-11-01 13:10 编辑

回复 1# palladiosh


    array reference 的 dereference。

$temp = ${$averages{$entry[0]}}[0];

写成

$temp = $averages{$entry[0]}->[0]; 可能会更清晰些。

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
4 [报告]
发表于 2015-11-01 13:10 |只看该作者
回复 1# palladiosh

How about this way ...

$ perl avg.pl
Name:John, Average:84
Name:Mary, Average:85

$ cat avg.pl
use strict;
use warnings;

my %hScore = ();
while(<DATA>){
  chomp;
  my($sName, $sNo, $sScore) = split /,/;
  
  if (exists $hScore{$sName}){
    # this student is already in the hash:
    my $rhName = $hScore{$sName};
    $rhName->{'total'} += $sScore;
    $rhName->{'subject'}++;
    $rhName->{'average'} = $rhName->{'total'} / $rhName->{'subject'};
  }
  else{
    # this is a new student:
    $hScore{$sName}->{'total'} = $sScore;
    $hScore{$sName}->{'subject'} = 1;
    $hScore{$sName}->{'average'} = $sScore;
  }
}
foreach my $sName (keys %hScore){
  print "Name:", $sName, ", Average:", $hScore{$sName}->{'average'}, "\n";

}
__DATA__
Mary,math,82
Mary,eng,88
John,math,84

   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP