Home arrow Perl Programming arrow Page 4 - Perl: More on Lists and Hashes

Adding a Column to Individual Rows in a Two-Dimensional List - Perl

Welcome to the first part of what should be the final two articles in our series on working with hashes and lists in Perl. This makes our eleventh article in the series and in it, we will wrap up the intermediate ways of working with hashes and lists. We'll learn to add rows and columns to a two-dimensional list, replace them, create multi-dimensional lists, and write hashes and files to lists, then open and read from those same files over the next two articles.

TABLE OF CONTENTS:
  1. Perl: More on Lists and Hashes
  2. How to Add Rows to a Two-Dimensional List
  3. Adding a Column to a Two-Dimensional List
  4. Adding a Column to Individual Rows in a Two-Dimensional List
By: James Payne
Rating: starstarstarstarstar / 3
May 27, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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...



 
 
>>> More Perl Programming Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: