Perl Example Refinement Learning notes the second day of the variable, array, hash

Source: Internet
Author: User
Tags hash variables scalar


A description of the variable



A variable is a data item whose value can change as the program runs.



A variable can be anywhere in the program, and you do not need to declare it as a type of feature, as in a high-level language.



The value of a variable can be a string, a number, or a combination of them.



Second, the type of Perl variable



A. Scalar type



$ variable = value;



B. Array type



@ array = (' value 1 ', ' Value 2 ', ' Value 3 ');



C. Associative array (often called hash "hash")



%hash= (' scalar 1 ' => ' value 1 ',



' Scalar 2 ' => ' value 2 ',);



Third, the scope of the variable



The scope of the variable is global by default. For local variables, you need to use the relevant local variables function our,my,local set



Iv. how the variable is assigned



The assignment operator is the equals sign (=) and often assigns the value on the right to the variable on the left. Any assigned value represents a named storage space called the Left value (lvalue)



The statement ends with a semicolon (;).



Example


#! / usr / bin / perl
$ day = 12; #variable
@months = ('Mar', 'Apr', 'may'); #Array
% states = (#hash
    'CA' => 'California',
    'ME' => 'Maine',
    'MT' => 'Montana',
    'NM' => 'New Mexico',
          );
print "$ dayn"; #n newline
print "@monthsn";
print "$ months [0], $ months [1], $ months [2] n]; #Print the values in the array
Print "$ states {'CA'}, $ states {'NM'} n"; #Print the values in the hash
Print $ x + 3, "n"; #Unassigned is empty
Print "* * * * $ name *** n"; # is empty for assignment 


The results of the operation are as follows








Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.