
Print scalar # 9 - there are other ways to get this, too.įor and foreach loops require an array in order to work. my = qw(The quick brown fox jumped over the lazy dog) Often you'll want to know how many elements are in an array just refer to it as a scalar, and you'll get it. If you want to throw the contents of the array away, just assign emptiness to it: = () # dog - the capital letter forces The to the zeroth element!

In this snippet, we'll sort them and pull off another element.

If you want to do things to the whole array, use the common use case is to assign a group of words to an array, like the words of a sentence. Notice how the changed to a $ for the print statement I wanted it to return a scalar, a single thing, not a list of things. You can retrieve them with the index number.
#Hashing in perl code#
To define an empty array in the current code scope, use my: my assign some values to the array, just enclose them in parentheses. The set is numbered from zero, and as with all Perl variables, they aren't typed-there's nothing requiring all the members of the set even to be the same type. ArraysĪs in many other languages, arrays describe an ordered set of things-they could be strings of characters, numbers, or even code blocks. If you're an experienced Perl developer, you know all about these, but if you're new to Perl or just thinking about picking it up, this article is for you. eBook: An introduction to programming with Bashįrom a technical standpoint, one of the features of Perl that I most appreciate is the easy tools for manipulating complicated data with arrays and hashes.

Try for free: Red Hat Learning Subscription.
