Reputation: 853
What are the differences between @variable
and $variable
in Perl?
I have read code with the symbol $
and the symbol @
before a variable name.
For example:
$info = "Caine:Michael:Actor:14, Leafy Drive";
@personal = split(/:/, $info);
What are the difference between a variable containing $
as opposed to @
?
Upvotes: 6
Views: 12039
Reputation: 67211
$
is for scalar variables(in your case a string variable.)
@
is for arrays.
split
function will split the variable passed to it acoording to the delimiter mentioned(:
) and put the strings in the array.
Upvotes: 2
Reputation: 5109
It isn't really about the variable, but more about the context how the variable is used. If you put a $
in front of the variable name, then it is used in scalar context, if you have a @
that means you use the variable in list context.
my @arr;
defines variable arr
as array$arr[0]
You can find more about Perl contexts here: http://www.perlmonks.org/?node_id=738558
Upvotes: 7
Reputation: 119
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.
Upvotes: 1
Reputation: 3682
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:
i have a cat. $myCatName = 'Snowball';
it jump on bed where sit @allFriends = qw(Fred John David);
And you can count them $count = @allFriends;
but can't count them at all cause list of names not countable: $nameNotCount = (Fred John David);
So, after all:
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)
So, list is not the same, as an array.
Interesting feature is slices where your mind will play a trick with you:
this code is a magic:
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;
so, after all things:
Perl have an interesting feature about context. your $
and @
are sigils, that help Perl know, what you want, not what you really mean.
$
like s
, so scalar
@
like a
, so array
Upvotes: 7
Reputation: 37136
From perldoc perlfaq7
What are all these $@%&* punctuation signs, and how do I know when to use them?
They are type specifiers, as detailed in
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.
Upvotes: 3
Reputation: 5090
Variables that start $ are scalars, a single value.
$name = "david";
Variables that start @ are arrays:
@names = ("dracula", "frankenstein", "dave");
If you refer to a single value from the array, you use the $
print "$names[1]"; // will print frankenstein
Upvotes: 4