Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Perl array in Perl

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use the Perl array in Perl. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The use of Perl Array in Perl

The Perl array is a very useful thing in perl. We can define the Perl array in the following ways:

@ a = ("hello", "great", "cipher")

@ b = (1, 2, 3, 4, 5, 6, 7)

The definition of @ b can also be written as follows:

@ b = (1.. 7); # this approach is too important for Perl arrays that require an initial value of 1 to 10000.

@ b = ("a".. "z")

It's OK, but I can't think of any use for the Chinese. You can define an empty Perl array in the following ways:

@ c = ()

Access the Perl array

Access the Perl array in the following way (the subscript is exactly the same as the c language, starting with 0, users of basic should pay attention)

$b [0] = 10000

Notice that the bootstrap becomes $instead of @. This change indicates that a separate data is being accessed at this time. Not a set of data. You can also use multiple subscripts, but at this point you need to use the @ leader. For example:

@ b [5rec 7je 9] = (45pr 56pr 67)

In this case, print$b [8]; will get an error using an undefined variable when using perl-w, and nothing else will happen.

Perl arrays copy each other in the following ways:

@ dwindlerc

This is much simpler than C language. Unlike the c language, perl's Perl array can be resized dynamically. So we can append and delete the elements in the Perl array. You can append an element in this way:

@ c = (@ c, "hello")

There is a special function in perl to wake up the append operation called push,push. The usage is as follows:

Push (@ c, "hello")

Push (@ c, ("hello", "halloha"))

Push (@ c <... >)

As you can see here, you can append an element, a set of elements, or even another Perl array to the Perl array. In addition, you can put different types of scalars in perl's Perl array. This may seem a little weird to c programmers, but just remember that everything in perl is a reference. Or understand that this is a Perl array of pointers of type void. With push, there must be pop. Pop is to take an element from the * of the Perl array. The usage is:

Pop (@ a)

Since you can add elements from the end of the Perl array, you can certainly add elements from the beginning of the Perl array. Then this function is provided by the unshift function. The usage is:

Unshift (@ c, "hello")

Unshift (@ c, ("hello", "halloha"))

Unshift (@ c <... >)

With unshif, there must be shift. The function of shift is to fetch an element from the beginning of the Perl array. Usage: unshift (@ a)

If you have a function that manipulates both ends of the Perl array, you must also have a function that manipulates the middle part of the Perl array, which is the splice,splice function that has three functions. The purpose of * is to insert content into the middle part of the Perl array. For example:

@ d = (1.. 9)

My@e= ("a".. "f")

Splice (@ dpjol 2pr 2pr e)

You will get the 12abcdef56789, and notice that the insertion starts with the second, not the element with the subscript 2. The second function of splice is to delete, such as adding:

Splice (@ dmem2pr 6)

The third function of the 1256789 share splice is to delete it to the end. The syntax is:

Splice (@ dpj2)

Is to start from the second, delete to the end. We should also have a function that combines the entire Perl number into a scalar. This function is that join,join takes two parameters, the * * parameters are the delimiters that are placed between the elements when merged, and the second is the Perl array for the operation. For example:

My@g= (1, 2, 3)

Join ("", @ g)

We will get "123". There are three more functions we saw yesterday, undef,chop and chomp. The purpose of undef (@ a); is to empty the contents of @ a. The function of chop (@ a); is to remove one character from the end of each element in the Perl array. The function of chomp (@ a); is to remove the newline character at the end of each element in the Perl array. We should also introduce a function called scalar, which gets the length of the Perl array. For example:

My@h= (1, 2, 3)

Scalar (@ h)

You will get 3. But we often don't use it this way, because the length of the Perl array is returned when converting the Perl array to a scalar, such as the following operation.

My@i= (1, 2, 3, 4)

Print@i. "\ n"

4 will be printed out.

This is the end of the article on "how to use the Perl array in Perl". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report