Perl Programming Page 4 - Perl: Dimensional Lists |
Sometimes it is necessary to create an array from a value in a two-dimensional array. Maybe we want to see the secret lair of Man-Girl. Here is how we could do so: #!/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"; $Lair=$StuporHeroes[0][0]; print $Lair; This code takes the value located in the first row and first column of @Stuporheroes, and stores it in $Lair. We then print out the value, giving us: Mount Tittikinaka I could also store a complete row in a list like so: #!/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"; @First = @{@StuporHeroes[0]}; print @First; Just as in our earlier example of how to print an entire row, we specify which row to store in the variable in between the square brackets[]. In the above example, the result is: Mount Tittikinaka Man-Girl Is a good listener Has Man-Boobs To see how that two-dimensional list gets stored in a normal list, let's try this code: #!/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"; @First = @{@StuporHeroes[0]}; print @First[0]; Here we print the first element in the @First list, which we made from the first row of @StuporHeroes. Here is the result: Mount Tittikinaka For a better example, let's print out each element in the list: #!/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"; @First = @{@StuporHeroes[0]}; print @First[0] . "\n"; print @First[1] . "\n"; print @First[2] . "\n"; print @First[3] . "\n"; The result: Mount Tittikinaka Man-Girl Is a good listener Has Man-Boobs And lastly, we can store the values in a hash as well: #!/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"; %First = @{@StuporHeroes[0]}; print %First; Returning the same result as above. Well that is all the time we have for this episode. We didn't get to all the topics we wanted to cover, but never fear; there are more articles to come. So be sure to check back often. Till then...
blog comments powered by Disqus |
|
|
|
|
|
|
|