41

Example:

my $some_variable;
my @some_variable;
my %some_variable;

I know, @ seems to be for an array and $ for a primitive. Is it totally right?

What is % for?

5
  • 2
    You could lookup sigil on google. Commented Apr 5, 2011 at 14:58
  • 1
    This can't possibly NOT be a duplicate Commented Apr 6, 2011 at 6:46
  • some people obviously disagree, please help delete this question Commented Apr 6, 2011 at 7:15
  • @dvk: did you mean cannot delete? or just close it plz Commented Apr 6, 2011 at 7:19
  • % is for hashes. Refer Here Commented May 19, 2016 at 7:22

4 Answers 4

47

One of the nice things about Perl is that it comes with a built-in manual. Type in the following command:

perldoc perlintro

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 scalar variable. It can hold a single value which can be a string, numeric, etc.

  • @foo is an array. Arrays can hold multiple values. You can access these values using an index. For example $foo[0] is the first element of the array and $foo[1] is the second element of the array, etc. (Arrays usually start with zero).

  • %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.

Sign up to request clarification or add additional context in comments.

1 Comment

What about a @ before a $? In the example below - $baseball_team_ids is passed to a subroutine then referenced in a new variable creation. my $total_ids = scalar(@$baseball_teamIDs);
24

$ is for scalars, @ is for arrays, and % is for hashes. See the Variable Types section of the documentation for more information.

1 Comment

Well, $ for for single item, @ is for zero or more items, and % is for zero or more pairs. When used with an identifier (variable name) and no indexing, that translates to what you say. However, all of these are different variables: $scalar, $array[0], $hash{"key"}. And these are different variables: @array, @hash{qw(key1 key2)}, as are these: %array[0], %hash{qw(key1 key2)} (although the hash window slicing wasn't. thing when this answer was produced).
14

$ is scalar, @ is array, and % is hash.

4 Comments

yeah, but why $_ and @_ are the same for containing pass-in parameters to a funcction? or i'm wrong?
@nicola, Array and hash elements use "$". $_[0] is the first element of @_. Both are unrelasted to $_.
@nicola => @_ is an array containing all of the arguments passed to a function. $_ is a scalar containing the current item being worked with in situations like for (@array) {...}. $_ has nothing to do with @_. Where you are likely getting confused is that to access an individual element of the argument list, you would write $_[0], which accesses the first element. Here the sigil changes to $ to denote that you are accessing a scalar, however the trailing [0] tells perl that it is accessing a scalar element of the array in _ or in other words, @_.
Well, $ for for single item, @ is for zero or more items, and % is for zero or more pairs. When used with an identifier (variable name) and no indexing, that translates to what you say. However, all of these are different variables: $scalar, $array[0], $hash{"key"}. And these are different variables: @array, @hash{qw(key1 key2)}, as are these: %array[0], %hash{qw(key1 key2)} (although the hash window slicing wasn't. thing when this answer was produced).
5

$var denotes a single-valued scalar variable
@var denotes an array
%var denotes an associative array or hash (they are both the same)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.