Arrays in Perl: A How to Guide on Creating Arrays

shutterstock_132019721When you’re programming, you may have to create a list of data. This list of data can hold a large number of items, such as different prices for products or different types of clothing or different people’s names. Perl, just like many other similar programs, gives you the ability to conveniently store this data for usage later. With the ability to store this data into an array, you don’t have to make several individual variables, and you can make various changes to the array as a whole.

If you’re new to Perl, then arrays are a great concept to learn about, but if you’re new to programming entirely then arrays may be a bit complicated for you to begin with. You can learn about some of the key fundamentals of programming for all sorts of languages by checking out the Udemy course Programming for Non-Programmers: Fundamentals

What is an Array?

As mentioned before, an array stores a specific type of data. For example, say you had a list of students in a class, and you wanted to conveniently have a list of all of their names. An array would be a perfect way to compile this list. All you would have to do is set up an array and write would be contained in it. It’s easy to setup an array, in fact below is an example of one.

@students = (“Michael”, “John”, “Sarah”);

What the above statement has down is create an array that consists of three students. Now all you have to do is write the following line of code and the array will write out the student’s names.

print “@coins”;

This is how you establish an array, but there’s more to it than that. You can manipulate arrays in various ways.

Working with Arrays

Now that you know how to create an array, you can learn how to manipulate one. This process includes adding in new elements from the array, taking out elements in the array, and identifying specific elements of it.

Say that you only wanted the program to write down one person’s name in the array. Instead of writing print “@students”; you can just write print $students[0];, which will print out the name Michael.

The way that arrays are indexed is that they start from the number 0 and increase by one for each element in the array. This means that the 0 element in the array is Michael, and the 1st and 2nd element in the array is John and Sarah respectively.

These are simple programming concepts, but they can be very important when you’re developing a program or an app. A lot of people like to learn how to program for one device in order to make things easier. If you’re interested in using a programming language to create software for a particular device, then you should check out the Udemy course Windows Phone – Programming for Beginners and Android Programming for Absolute Beginners.

Adding and Removing Elements in Your Array

After you’ve created your array, you may want to add in a new element. You can do that at any time. There are a few simple things you have to write in order to add and remove elements to your array.

Shift() will remove the first element of your array.

Pop() will remove the last element of your array.

Unshift() will add any element to the beginning of your array

Push() will add an element to the end of your array.

Now you can add onto the array that was previously established. Say a new student named Alex just entered the class. You can easily add her to the array by typing in the following code.

unshift(@students, “Alex”);

This puts Alex at the beginning of the array, which will now read as Alex, Michael, John, and Sarah.

You can also do the same thing to remove a student. Say Sarah moves away and leaves the class. Then you can remove Sarah from the array. Remember that Sarah is the last element in the array, which means that you just have to use the pop statement.

pop(@students);

That’s all there is to modifying your array. Of course Perl consists of other functions outside of arrays. Another common thing you will work with when using arrays is string functions. You can learn a lot about Perl string functions and how to manipulate them in order to do more with your programs and arrays.

Learning More about Perl

Perl can be a complicated programming language to learn, but if you master it you can create tons of web applications and programs. One way to learn Perl is simply by creating your own programs. The Udemy course Learn Perl 5 By Doing It helps you with coming up with simple and useful programs that you can make to improve your skills.