Perl Programming Perl: Sailing the List(less) Seas |
I know what you are thinking: haven't we talked about this subject before? Yes, yes we did. But only briefly, way back when I went over the various data types in Perl. Here, we will look at them much more closely. So put on your trifocals and get out your bullets, because its time to make us some lists. Lists: Just a Fancy Way of Saying Array If you remember the basics of lists, or just can't wait to get to the end of this article, feel free to skip ahead. If you need a refresher in what lists are, then stay tuned. A list is simply a group of items or elements that are in order. If you know about arrays, then this should seem familiar, as lists are just arrays. If you aren't, then think of a list as a group of variables. Each variable resides within the list and has an index number. The values within the list can be numbers or strings, or numbers and strings. Below is a sample showing how to store data in a list and print out the values: #!/usr/bin/perl @gladiators=('Nitro ', 'Blaze ', 'CountFistula ', 'TheNutcracker '); print "These gladiators will mess you up: "; print @gladiators; The above code prints out: These gladiators will mess you up: Nitro Blaze CountFistula TheNutcracker Pretty simple right? You will note a few things about the above code; first, unlike scalar variables, you start the variable name with an @ symbol when dealing with lists, instead of the $ symbol. Each element in the list is encased in a quotation mark, and separated from the other elements in the array by a comma. I also added a space at the end of each element. Had I not, the words would have all printed together. To print the list, I simply typed print followed by the name of the array. But what if I wanted to print out only certain values in the list? That is covered in the next section
blog comments powered by Disqus |