用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - perl代码库

perl 构造记录

2012-10-13 作者: 神马举报

[perl]代码库

#-----------------------------
$record =
{
	NAME   => "Jason",
	EMPNO  => 132,
	TITLE  => "deputy peon",
	AGE    => 23,
	SALARY => 37_000,
	PALS   => [ "Norbert", "Rhys", "Phineas"],
};

printf "I am %s, and my pals are %s.\n",
$record-> {NAME},
join ( ", ", @ {$record->{PALS}} );
#-----------------------------
# store record
$byname { $record->{NAME} } = $record;

# later on, look up by name
if ( $rp = $byname {"Aron"} )
{
# false if missing
	printf "Aron is employee %d.\n", $rp-> {EMPNO};
}

# give jason a new pal
push @ {$byname{"Jason"}->{PALS}}, "Theodore";
printf "Jason now has %d pals\n", scalar @ {$byname{"Jason"}->{PALS}};
#-----------------------------
# Go through all records
while ( ( $name, $record ) = each %byname )
{
	printf "%s is employee number %d\n", $name, $record-> {EMPNO};
}
#-----------------------------
# store record
$employees[ $record-> {EMPNO} ] = $record;

# lookup by id
if ( $rp = $employee[132] )
{
	printf "employee number 132 is %s\n", $rp-> {NAME};
}
#-----------------------------
$byname {"Jason"}-> {SALARY} *= 1.035;
#-----------------------------
@peons   = grep { $_->{TITLE} =~ /peon/i } @employees;
@tsevens = grep { $_->{AGE}   == 27 }      @employees;
#-----------------------------
# Go through all records
foreach $rp ( sort { $a->{AGE} <=> $b->{AGE} } values %byname )
{
	printf "%s is age %d.\n", $rp-> {NAME}, $rp-> {AGE};
# or with a hash slice on the reference
	printf "%s is employee number %d.\n", @$rp {'NAME','EMPNO'};
}
#-----------------------------
# use @byage, an array of arrays of records
push @ { $byage[ $record->{AGE} ] }, $record;
#-----------------------------
for ( $age = 0; $age <= $#byage; $age++) {
	next unless $byage[$age];
	print "Age $age: ";
	foreach $rp ( @ {$byage[$age]} )
	{
		print $rp-> {NAME}, " ";
	}
	print "\n";
}
#-----------------------------
for ( $age = 0; $age <= $#byage; $age++) {
	next unless $byage[$age];
	printf "Age %d: %s\n", $age,
	join ( ", ", map {$_->{NAME}} @ {$byage[$age]} );

}
#-----------------------------


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...