Several Ideas on Perl List Context

Source: Internet
Author: User
Tags scalar

According to Beginning Perl book published by Tsinghua Pub., the list context appears when you were trying to Assi GN Some value to a list variable.

1 my @copy @source;

This was a very simple instance of shallow copy, which usually means you just copied the reference of the source array, non E new elements is created and no other memories'll be occupied.

When we assign an array with a hash table, the Key-value elements'll be converted to a plain list, which is called Plana Rization.

For example, the following Perl codes would print the result below:

1 my %hashtable = (2  tom=>,3  jerry=>,4   sam=>                5); 6 my @flattened %hashtable ; 7 Print @flattened;
Tom12jerry13sam17

*another thing I want to stress are, if we want to seperate these flattened elements by a blank space charactor, we NE Ed to modify the print statement to this: (Attention to the quatation marks)

1 Print " @flattened ";

Thus, the result in console would become:

Tom, Jerry, Sam 17.

The book has noted one important feature-the advantage of list context:

You can force to obtain it with a pair of brackets.

if we want to assign the first element to a scalar variable, we can simply surround it with Brackets, like this:

1 my @array = ('element0','element1'); 2 my ($scalar@array; 3 Print $scalar;

The result is displayed:

Element0

But there are more than that! We can add more scalars to the brackets (as long as the array has enough elements), and the scalars would be assigned by the Elements one by one. Code it as follow:

1 my @array= ('element0','element1');2 my($scalar 0,$scalar 1);3 Print "$scalar 0\t$scalar1";

The result is:

Element0    element1

Have you found the advantage of list context? If you haven ' t, see the Followint statement:

($leftScalar$rightScalar) = ($rightScalar$leftScalar);

Isn ' t it beautiful? Usually, especially in some other languages, we had to use a third variable to store one of the one of the both elements and that CER Tainly takes more than this in Perl.

  

Ok! It ' s What we get this afternoon!

Several Ideas on Perl List Context

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.