Perl中的@variable
和$variable
之间有什么区别?
我已经阅读代码,其符号为$
,符号前为变量名称@
。
例如:
$info = "Caine:Michael:Actor:14, Leafy Drive";
@personal = split(/:/, $info);
是什么含$
而不是@
变量之间的差异?
Perl中的@variable
和$variable
之间有什么区别?
我已经阅读代码,其符号为$
,符号前为变量名称@
。
例如:
$info = "Caine:Michael:Actor:14, Leafy Drive";
@personal = split(/:/, $info);
是什么含$
而不是@
变量之间的差异?
你所有的知识约Perl的将被山坠毁,当你不觉得这种语言的背景。
许多人,你在语音中使用单值(标量)和许多事物。
于是,所有的人之间的区别:
我有一只猫。 $myCatName = 'Snowball';
它跳到床上坐哪里@allFriends = qw(Fred John David);
而且你可以指望他们$count = @allFriends;
,但在名称的所有原因列表不能指望他们不是可数:$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)
所以,列表是不一样的,作为阵列。
有趣的特点是切片在您的头脑会捉弄你:
这段代码是魔法:
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知道,你想,不是你真正的意思。
$
像s
,所以标
@
像a
,使阵列
开始$的变量是标量,一个值。
$name = "david";
的变量开始@是数组:
@names = ("dracula", "frankenstein", "dave");
如果从阵列指的是单个值,可以使用$
print "$names[1]"; // will print frankenstein
$
是标量(以你的情况是一个字符串变量)。 @
用于数组。
split
函数将按照提及的分隔符(:
)拆分传递给它的变量,并将字符串放入数组中。
迂回地,'split'函数将标量转换为* list *。然后赋值将该列表存储在一个数组中。 – 2012-08-01 08:56:22
这是错误的。什么是$ array [$ n]和$ hash {$ key}? – 2012-08-01 14:45:33
$ n是一个标量(这里是一个数组的索引),$ array [$ n]是另一个标量变量。 – Vijay 2012-08-02 06:17:40
它不是真的关于变量,但更多关于上下文如何使用该变量。如果您在变量名前面加上$
,那么它在标量上下文中使用,如果您有@
这意味着您在列表上下文中使用该变量。
my @arr;
定义变量arr
作为数组$arr[0]
你可以找到更多有关Perl的上下文这里:http://www.perlmonks.org/?node_id=738558
很好的答案。您也可以将'$'看作'This'(*这个数字是42 * <=>'$ number = 42'),'@'可以看作'These'(*这些数值是(1..42)* <=>' @values =(1..42)'和*最后一个值* <=>'$ values [-1]') – amon 2012-08-01 09:44:25
'@ arr'是**数组**,而不是**列表**。参见行为:'print $ s = @all = qw(我的列表在这里);'和'print $ s = qw(我的列表在这里);' – gaussblurinc 2012-08-01 10:03:07
我希望听到关于单片的列表和标量的情况。 在这里做'@one [$ one]''@表示列表上下文吗?在所有情况下都不是这样 – gaussblurinc 2012-08-02 09:35:12
这些是什么$ @%& *标点符号,以及如何知道何时使用它们?
他们是类型说明符,详见
perldata
:$ for scalar values (number, string or reference) @ for arrays % for hashes (associative arrays) & for subroutines (aka functions, procedures, methods) * for all types of that symbol name. In version 4 you used them like pointers, but in modern perls you can just use references.
Variable name starts with $ symbol called scalar variable.
Variable name starts with @ symbol called array.
$var -> can hold single value.
@var -> can hold bunch of values ie., it contains list of scalar values.
'$' - 标量,'@' - 阵列。 '$ info'是一个包含':'的标量字符串,@personal - 数组赋予分配函数的返回值。 'split'需要两个参数(分隔符,valueToSplit) – user907860 2012-08-01 07:22:46
http://stackoverflow.com/questions/2731542/what-is-the-difference-between-this-that-and-those-in-perl – daxim 2012-08-01 11:23:09
有** [perlintro](http://perldoc.perl.org/perlintro.html#Perl-variable-types)**中的一个非常好和简洁的描述! – 2012-08-01 11:25:39