Perl Programming Page 4 - Beginning Programming Perl(s) |
Sometimes you store stuff in a folder in real life. Now imagine that you have 26 folders in a drawer alphabetized from A-Z. Since you are a programmer, those files probably contain the names and addresses of girls that have rejected you. If you are a young programmer, you are only using one drawer. If you are older, well...you've switched from drawers to outdoor storage sheds. Either way, that is an example of a List Data Type. Simply put, a List is a group of scalars put in order. There are a variety of ways to order the scalars, which we will get into later on. If you are familiar with arrays in other programming languages, you'll know that a List is the same thing. Here is how you store data in a List: @my_cats = {'Joni ', 'Chachi ', 'Richie ', 'Fonzie ', 'Potsy '}; The above creates a list with five scalars in it. If we do the following code: #!/usr/local/bin/perl @my_cats = {'Joni ', 'Chachi ', 'Richie ', 'Fonzie ', 'Potsy '}; print @my_cats; This would print the following: Joni Chachi Richie Fonzie Potsy I can store numbers in Lists also, or mix numbers and characters if I choose. Perl doesn't care. It's too busy unwrapping that Hershey bar (the one with almonds). As you can see, Lists must have the @ symbol as the first part of their name. They use the lowercase naming convention (or at least I do) and it's best to separate words with underscores, though in truth you don't have to. So let's say a girl wants to use my cat program to see the names of all my cats. I'm clearly embarrassed to have so many (or I would be if I really did have that many). So I would create a program that would only show the name of one cat from my List. #!/usr/local/bin/perl @my_cats = {'Joni ', 'Chachi ', 'Richie ', 'Fonzie ', 'Potsy '}; print $my_cats[1]; This would print the name Chachi to the screen. I know what you are thinking and you can cool your biscuits right there (within arms reach...I'm hungry). Why didn't it print Joni? Simple. Lists start off at 0 and work their way up. To print Joni and Richie, you would use the following code: #!/usr/local/bin/perl @my_cats = {'Joni ', 'Chachi ', 'Richie ', 'Fonzie ', 'Potsy '}; print $my_cats[0,2]; You may have noticed in the above examples that I switched the @ symbol to the $ symbol on the last line. In addition to wanting to confuse you, this is also necessary because you are calling individual portions of the item (scalars) and not the entire item itself. You will note that the sample rules apply for quotes when working with arrays. If I wanted a list of contractions, I would do it this way #!/usr/local/bin/perl @my_contractions = {'Won't ', 'Can't ', 'Don't '}; print @my_contractions; The above code prints: Won't Can't Don't Conclusion While we are not finished with data types or this series, I am finished with this current tutorial. In the next tutorial, we will wrap up Lists and move on to the third data type, and use operators to manipulate data. Till then...
blog comments powered by Disqus |
|
|
|
|
|
|
|