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...
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |