Perl: More on Lists and Hashes - Adding a Column to Individual Rows in a Two-Dimensional List (
Page 4 of 4 )
Adding columns one row at a time is a lot simpler. Below we will add columns to two of of our rows, but not the rest:
#!/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[1][7]='READ';
$Bookshelf[2][7]='UNREAD';
print @{@Bookshelf[1]};
print "\n\n";
print @{@Bookshelf[2]};
print "\n\n";
print @{@Bookshelf[3]};
Here we get the following output:
1 Stephen King It Horror 5 Top READ
2 Clive Barker Imajica Horror 5 Top UNREAD
3 Neil Gaiman American Gods Dark Fantasy 5 Top
Note that the last row we printed did not have the read/unread column, because we did not add it.
Well that's it for this article. In our next article we will wrap up our discussion of lists and hashes (until a more advanced tutorial at any rate). Hope to see you there.
Till then...