CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
the % sign



Jun 5, 2015, 11:23 AM

Post #1 of 2 (1038 views)
the % sign Can't Post

I see from some places that they use % sign before an array, but without the array sign @. what does this mean?
For example, for an array @array1, what does this mean? %array1? Thanks.


Jun 5, 2015, 5:59 PM

Post #2 of 2 (1032 views)
Re: [rockcone01] the % sign [In reply to] Can't Post

The @ sigil denotes an array variable; the % denotes a hash variable.

An array variable contains an ordered list of zero or more elements. Elements may be undef, numbers, strings, or references. Values may be duplicated. The items are accessed by following the variable name with square brackets and index numbers beginning at zero:

my @array = ( 'a', 'b', 'c' );   # create 3-element array 
print $array[1]; # prints 'b'

A hash, also known as an "associative array," contains an unordered set of zero or more key and value pairs. Each key is a string, and no two keys in a hash can be the same. Values may be undef, numbers, strings, or references. Values of the hash are accessed by looking up the key, which follows the variable name in curly brackets:

my %hash = ( 'color' => 'red' );  # key => value 
print $hash{color}; # prints 'red'

Note that when you access a single value from either an array or a hash, you change the sigil to $ to denote that you're accessing a single scalar value.

You can find plenty more information about data types in 'perldoc perldata'.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives