“@"之间的区别和“$"在 Perl 中 [英] Difference between "@" and "$" in Perl

查看:61
本文介绍了“@"之间的区别和“$"在 Perl 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Perl 中的 @variable$variable 有什么区别?

What are the differences between @variable and $variable in Perl?

我已经阅读了在变量名称前带有符号 $ 和符号 @ 的代码.

I have read code with the symbol $ and the symbol @ before a variable name.

例如:

$info = "Caine:Michael:Actor:14, Leafy Drive";
@personal = split(/:/, $info);

包含 $ 的变量与包含 @ 的变量有什么区别?

What are the difference between a variable containing $ as opposed to @?

推荐答案

当你不感受上下文时,你所有关于 Perl 的知识都会被淹没这种语言.

All your knowledge about Perl will be crashed with mountains, when you don't feel context of this language.

与许多人一样,您在演讲中使用单个值(标量)和一组中的许多东西.

As many people, you use in your speech single value (scalars) and many things in a set.

所以,它们之间的区别:

So, the difference between all of them:

我有一只猫.$myCatName = '雪球';

它跳到床上坐着@allFriends = qw(Fred John David);

你可以计算它们$count = @allFriends;

但根本无法计算它们,因为名称列表不可数:$nameNotCount = (Fred John David);

but can't count them at all cause list of names not countable: $nameNotCount = (Fred John David);

所以,毕竟:

print $myCatName = 'Snowball';           # scalar
print @allFriends = qw(Fred John David); # array! (countable)
print $count = @allFriends;              # count of elements (cause array)
print $nameNotCount = qw(Fred John David); # last element of list (uncountable)

因此,listarray 不同.

有趣的功能是切片,你的大脑会和你玩一个把戏:

Interesting feature is slices where your mind will play a trick with you:

这段代码很神奇:

my @allFriends = qw(Fred John David);
$anotherFriendComeToParty =qq(Chris);
$allFriends[@allFriends] = $anotherFriendComeToParty; # normal, add to the end of my friends
say  @allFriends;
@allFriends[@allFriends] = $anotherFriendComeToParty; # WHAT?! WAIT?! WHAT HAPPEN? 
say  @allFriends;

所以,毕竟:

Perl 有一个关于上下文的有趣功能.你的 $@ 是符号,帮助 Perl 知道你想要什么,而不是你真正的意思.

Perl have an interesting feature about context. your $ and @ are sigils, that help Perl know, what you want, not what you really mean.

$ 类似于 s,所以标量
@ 类似于 a,所以数组

$ like s, so scalar
@ like a, so array

这篇关于“@"之间的区别和“$"在 Perl 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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