Perl Programming Page 2 - Perl: More on Lists and Hashes |
Adding a row to a two-dimensional list is pretty simple. In this next example we are going to add a new book to our bookshelf using the push() function, which adds your new item to the end of the list (or the right-most side): #!/usr/bin/perl @Bookshelf = ( [" # ", " Author ", " Title ", " Genre "," Rating ", " Location "], [' 1 ', ' Stephen King ', ' It ', ' Horror ', ' 5 ', ' Top '], [' 2 ', ' Clive Barker ', ' Imajica ', ' Horror ', ' 5 ', ' Top '], [' 3 ', ' Neil Gaiman ', ' American Gods ', ' Dark Fantasy', ' 5 ', ' Top '], [' 4 ', ' Dean Koontz ', ' Tick-Tock ', ' Horror ', ' 1 ', ' GarbageCan '], [' 5 ', ' Charles Bukowski ', ' Letters from a Dirty Old Man ', ' Literature ', ' 5 ', ' Top '], [' 6 ', ' Chuck Pahluniak ', ' Fight Club ',' Dark Fantasy', ' 5 ', ' Middle '] ); @NewBook = (' 7 ', ' T.H. White ', ' The Once and Future King ', ' Fantasy ', ' 5 ', ' Middle '); push(@Bookshelf, [ @NewBook ]); print "\n\n"; print @{@Bookshelf[7]}; Here we create our 2-D list called @Bookshelf and assign a bunch of values to it. We then create a normal list called @NewBook and assign values to it. Next we use the push() function to place the values in the @NewBook list into our 2-D list, which it appends to the end. Finally, we print the newly added row. The result is: 7 T.H. White The Once and Future King Fantasy 5 Middle Another way to add a row to a two-dimensional list is like this: #!/usr/bin/perl @Bookshelf = ( [" # ", " Author ", " Title ", " Genre ", " Rating ", " Location "], [' 1 ', ' Stephen King ', ' It ', ' Horror ', ' 5 ', ' Top '], [' 2 ', ' Clive Barker ', ' Imajica ', ' Horror ', ' 5 ', ' Top '], [' 3 ', ' Neil Gaiman ', ' American Gods ', ' Dark Fantasy ', ' 5 ', ' Top '], [' 4 ', ' Dean Koontz ', ' Tick-Tock ', ' Horror ', ' 1 ', ' GarbageCan '], [' 5 ', ' Charles Bukowski ', ' Letters from a Dirty Old Man ', ' Literature ', ' 5 ', ' Top '], [' 6 ', ' Chuck Pahluniak ', ' Fight Club ', ' Dark Fantasy ', ' 5 ', ' Middle '] ); @Bookshelf[7]= ( [' 7 ', ' T.H. White ', ' The Once and Future King ', ' Fantasy ', ' 5 ', ' Middle '] ); print @{@Bookshelf[7]}; This gives us the same result without using the push() function. Of course there is one other, shorter way to add a row: #!/usr/bin/perl @Bookshelf = ( [" # ", " Author ", " Title ", " Genre ", " Rating ", " Location "], [' 1 ', ' Stephen King ', ' It ', ' Horror ', ' 5 ', ' Top '], [' 2 ', ' Clive Barker ', ' Imajica ', ' Horror ', ' 5 ', ' Top '], [' 3 ', ' Neil Gaiman ', ' American Gods ', ' Dark Fantasy ', ' 5 ', ' Top '], [' 4 ', ' Dean Koontz ', ' Tick-Tock ', ' Horror ', ' 1 ', ' GarbageCan '], [' 5 ', ' Charles Bukowski ', ' Letters from a Dirty Old Man ', ' Literature ', ' 5 ', ' Top '], [' 6 ', ' Chuck Pahluniak ', ' Fight Club ', ' Dark Fantasy ', ' 5 ', ' Middle '] ); push(@Bookshelf, [' 7 ', ' T.H. White ', ' The Once and Future King ','Fantasy ', ' 5 ', ' Middle ']); print @{@Bookshelf[7]}; Again, it gives us the same result: 7 T.H. White The Once and Future King Fantasy 5 Middle
blog comments powered by Disqus |
|
|
|
|
|
|
|