2012-08-01 38 views
0

Perl中的@variable$variable之間有什麼區別?

我已經閱讀代碼,其符號爲$,符號前爲變量名稱@

例如:

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

是什麼含$而不是@變量之間的差異?

+1

'$' - 標量,'@' - 陣列。 '$ info'是一個包含':'的標量字符串,@personal - 數組賦予分配函數的返回值。 'split'需要兩個參數(分隔符,valueToSplit) – user907860 2012-08-01 07:22:46

+1

http://stackoverflow.com/questions/2731542/what-is-the-difference-between-this-that-and-those-in-perl – daxim 2012-08-01 11:23:09

+1

有** [perlintro](http://perldoc.perl.org/perlintro.html#Perl-variable-types)**中的一個非常好和簡潔的描述! – 2012-08-01 11:25:39

回答

2

你所有的知識約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,使陣列

3

開始$的變量是標量,一個值。

$name = "david"; 

的變量開始@是數組:

@names = ("dracula", "frankenstein", "dave"); 

如果從陣列指的是單個值,可以使用$

print "$names[1]"; // will print frankenstein 
0

$是標量(以你的情況是一個字符串變量)。 @用於數組。

split函數將按照提及的分隔符(:)拆分傳遞給它的變量,並將字符串放入數組中。

+1

迂迴地,'split'函數將標量轉換爲* list *。然後賦值將該列表存儲在一個數組中。 – 2012-08-01 08:56:22

+0

這是錯誤的。什麼是$ array [$ n]和$ hash {$ key}? – 2012-08-01 14:45:33

+0

$ n是一個標量(這裏是一個數組的索引),$ array [$ n]是另一個標量變量。 – Vijay 2012-08-02 06:17:40

5

它不是真的關於變量,但更多關於上下文如何使用該變量。如果您在變量名前面加上$,那麼它在標量上下文中使用,如果您有@這意味着您在列表上下文中使用該變量。

  • my @arr;定義變量arr作爲數組
  • 當你要訪問一個單個元件(這是一個標量上下文),你必須使用$arr[0]

你可以找到更多有關Perl的上下文這裏:http://www.perlmonks.org/?node_id=738558

+0

很好的答案。您也可以將'$'看作'This'(*這個數字是42 * <=>'$ number = 42'),'@'可以看作'These'(*這些數值是(1..42)* <=>' @values =(1..42)'和*最後一個值* <=>'$ values [-1]') – amon 2012-08-01 09:44:25

+1

'@ arr'是**數組**,而不是**列表**。參見行爲:'print $ s = @all = qw(我的列表在這裏);'和'print $ s = qw(我的列表在這裏);' – gaussblurinc 2012-08-01 10:03:07

+0

我希望聽到關於單片的列表和標量的情況。 在這裏做'@one [$ one]''@表示列表上下文嗎?在所有情況下都不是這樣 – gaussblurinc 2012-08-02 09:35:12

2

perldoc perlfaq7

這些是什麼$ @%& *標點符號,以及如何知道何時使用它們?

他們是類型說明符,詳見 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. 
1
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. 
相關問題