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

How to Add Rows to 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 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



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