Home arrow Perl Programming arrow Page 2 - Perl: Bon Voyage Lists and Hashes

Adding and Removing Values - Perl

Thanks for stopping by for our final article on Perl lists and hashes. This marks our twelfth issue on the subject (at least until we get to some more advanced techniques later on) and in it, we will look at a few of the functions for manipulating two-dimensional lists, how to create lists with more than two dimensions, and finally, how to make a hash full of lists, which is really quite a handy tool.

TABLE OF CONTENTS:
  1. Perl: Bon Voyage Lists and Hashes
  2. Adding and Removing Values
  3. Push
  4. Making Hashes Out of Lists
By: James Payne
Rating: starstarstarstarstar / 1
August 12, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In our last article we learned to add rows and columns to a two-dimensional list. We are going to continue this discussion, briefly, by showing you how to use some functions to accomplish the same goals.

You may remember from our conversation about normal lists that there are several functions available to add and remove items. Namely they are pop, push, shift, and unshift. They work pretty similar with two-dimensional lists, but with some minor changes. Don't worry; I won't eat up a lot of time showing how to use these functions here, as that would mostly be repetitive. Still, the changes are important to note.

Unshift

As you may recall, unshift() adds an element to the leftmost position, or beginning of a list. It does the same thing in two-dimensional lists. The only difference is the code:


#!/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 ']

);

unshift(@Bookshelf, [' 1 ', ' T.H. White ', ' The Once and Future King

','Fantasy ', ' 5 ', ' Middle ']);

print @{@Bookshelf[0]};

When you run this code, it prints out the new record in row 0:

  1 T.H. White The Once and Future King Fantasy 5 Middle

You will note that the record that previously held the 0 row position is now moved to row 1, and row 1 is now row 2, and so forth. This of course messes up our use of row 0 as a header with all of the column names, but we can fix that later.



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