Perl Programming Page 2 - Perl: Dimensional Lists |
As you will recall from our discussion of lists, when you print elements within them you refer to them by their location within the list, or their element number. Printing from a two-dimensional list is not much different. Instead of printing from the lone element number, you reference both the row number and column number. It's easiest to understand if you see it in action: #!/usr/bin/perl @StuporHeroes = ( [' Mount Tittikanaka ', ' Man-Girl ', ' Is a good listener ', ' Has Man-Boobs '], ['Trailer Park', 'Deaf Leapard', 'Has a super sonic guitar', 'Is deaf and has one arm making him unable to play his guitar'] ); print $StuporHeroes[0][0]; Here we assign the values to our two-dimensional list, and then call one of them out by referring to its position on the grid. Can you guess what gets printed? Guess no more: Mount Tittikanaka This value is in the first row and first column (remember rows and columns begin at zero, just like list/array elements). For a better example, let's print out the rest of the individual values: #!/usr/bin/perl @StuporHeroes = ( [' Mount Tittikanaka ', ' Man-Girl ', ' Is a good listener ', ' Has Man-Boobs '], [' Trailer Park ', ' Deaf Leapard ', ' Has a super sonic guitar ', ' Is deaf and has one arm making him unable to play his guitar ']
); print "\n\n"; print $StuporHeroes[0][0] . "\n\t"; print $StuporHeroes[0][1] . "\n\t"; print $StuporHeroes[0][2] . "\n\t"; print $StuporHeroes[0][3] . "\n\n"; print $StuporHeroes[1][0] . "\n\t"; print $StuporHeroes[1][1] . "\n\t"; print $StuporHeroes[1][2] . "\n\t"; print $StuporHeroes[1][3]; Before I show you the results, just note that I formatted the printing to give a better view of the elements: Mount Tittikanaka Man-Girl Is a good listener Has Man-Boobs Trailer Park Deaf Leapard Has a super sonic guitar Is deaf and has one arm making him unable to play his guitar
blog comments powered by Disqus |
|
|
|
|
|
|
|