Perl-09-Hash variable

Source: Internet
Author: User

I. Hash Variables

A hash variable is also called an associated array. Like an ordinary array, you can use a subscript to access the data elements. The difference is that the subscript of a hash variable can not only be a non-negative integer, it can also be other types of simple variables or variable values, that is, the subscript index of the hash variable can be a simple variable or variable value of any type, called a keyword; the hash variable starts with the character "%". Each keyword corresponds to a value;

There are two ways to create a hash variable: using the array creation method and using the association operator "=>;

% Hash = (key1, value1, key2, value2, key3, value3, key4, value4,..., keyn, valuen );

Or

% Hash = (key1 => value1, key2 => value2, key3 => value3, key4 => value4,..., keyn => valuen );

When using the first method, the element structure in the array must be a key-value pair, and the number of array elements must be an even number. The second method also follows this rule, however, the "=>" symbol can be used to make the program easier to understand;

The key of a hash variable is a direct number. Generally, the most commonly used is the string direct number used as the key of the hash variable, or the number can be used as the key of the hash variable;

The unit value of the hash variable is used to read scalar variables starting with "$". In Perl, different symbols are used around the unit index to distinguish the unit value of the referenced hash variable from the unit value of the array variable; the subscript of the hash variable can be any simple value or scalar value. Therefore, the hash variable is marked by a pair of braces "{}" around the unit index, the common array is marked with a pair of square brackets;

Ii. operation functions of hash Variables

1. @ keylist = keys (% hash): returns all the keywords in the hash variable % hash, in the form of a list;

2. @ ValueList = values (% hash): return all values of the hash variable % hash, in the form of a list;

3. ($ key, $ value) = each (% hash) function: This function is used to return a key-value pair in the hash variable % hash, Which is returned as a list;

This function is often used to traverse elements in hash traversal in a loop. This method is the easiest way; for example:

While ($ key, $ value) = each (% hash ))

{

# Various operations ......

}

4. $ retdelete = Delete ($ hash {key}): Used to delete the specified Element in the hash variable % hash and use it as the return value;

Note: you must use the delete () function to delete the elements in the hash variable. This is the only practice. Do not try to use push (), pop (), shift (), unshift () and other functions to operate on the hash variable, because the position of the elements in the hash variable is immediate;

5. exists () function: used to determine whether an element exists in a hash variable. If an element exists, a non-zero value (true) is returned; otherwise, a zero value (false) is returned. For example:

If (exists ($ hash {key }))

{

Printf ("key exists/N ");

}

Else

{

Printf ("key doesn't exists/N ");

}

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.