Example:
my $some_variable;
my @some_variable;
my %some_variable;
I know, @
seems to be for array, $
for primitive, is it totally right?
What is %
for?
Example:
my $some_variable;
my @some_variable;
my %some_variable;
I know, @
seems to be for array, $
for primitive, is it totally right?
What is %
for?
$
is for scalars,@
is for arrays, and%
is for hashes. See the Variable Types section of the the docs for more information.$var denotes a single-valued scalar variable
@var denotes an array
%var denotes an associative array or hash (they are both the same)
$
is scalar,@
is array, and%
is hash.One of the nice things about Perl is that it comes with a built in manual. Type in the following command:
and take a look at the section Perl variable types. You can also see this on line with the perldoc.perl.org section on Perl variables.
A quick overview:
%foo is a hash, this is like an array because it can hold more than one value, but hashes are keyed arrays. For example, I have a password hash called %password. This is keyed by the user name and the values are the user's password. For example:
$password{Fred} = "swordfish"; $password{Betty} = "secret";
$user = "Fred"; print "The Password for user $user is $password{$user}\n"; #Prints out Swordfish $user = "Betty"; print "The Password for user $user is $password{$user}\n"; #Prints out secret
Note that when you refer to a single value in a hash or array, you use the dollar sign. It's a little confusing for beginners.
I would recommend that you get the Llama Book. The Llama Book is Learning Perl and is an excellent introduction to the language.