Perl: Bon Voyage Lists and Hashes

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.

In our previous article we created a “database” of a bookshelf with some books on it. We then learned how to add rows to it, add a single column, and automate a process to add columns to every row. Here, we will continue working with the @Bookshelf “database.” Just so you don’t have to go seeking out our previous article, here is the list of fields:

  • Book Number

  • Author

  • Title

  • Genre

  • Number of Stars (how good the book was in a ranking of 1-5, with five being the best)

  • Which shelf on the bookshelf it is located

And the code to create the two-dimensional list:


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

);

{mospagebreak title=Adding and Removing Values}

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.

{mospagebreak title=Push}

Again, push() works in the same manner as it did with normal lists; that is, it adds a record to the rightmost position (or end) of a two-dimensional list:


#!/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, [' 1 ', ' T.H. White ', ' The Once and Future King

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

print @{@Bookshelf[7]};

This code places the “Once and Future King” book at the end of the list, which we then print out in the final line of code, resulting in:

 

You can also do the same thing using a variable to insert the data, like so:


#!/usr/bin/perl

@NewBook = (‘ 1 ‘, ‘ T.H. White ‘, ‘ The Once and Future King

‘,’Fantasy ‘, ‘ 5 ‘, ‘ Middle ‘);

@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, [@NewBook]);

print @{@Bookshelf[7]};

Here, we create a variable called @NewBook and store some info about T.H. White’s book inside of it. Next we created @Bookshelf and stored even more information into it. Finally, we used push() to append the data in @NewBook to the end of @Bookshelf. When we print out the last element, this is what we get:


That’s right…the same as before.

Pop and Shift

As you will recall from previous articles, Pop() removes items from the right side of a list and shift removes items from the left side of a list. Since I have already covered these two in the past, and since I have shown how to use Unshift and Push on two dimensional lists above, I won’t cover them again here, in an effort to save space and not be too repetitive. If you would like to read more about them, then simply follow this link: http://www.devshed.com/c/a/Perl/Perl-Lists-More-on-Manipulation/

{mospagebreak title=Making Hashes Out of Lists}

You can create hashes that contain list values pretty easily in Perl. This allows you to find list items using a key, as showcased in earlier tutorials. Here we will store some list values in our hash %Hawtie:


#!/usr/bin/perl

%Hawtie =(

’10’ => ['Angelina Jolie', 'Actress', 'My Baby Mama'],

‘1’ => ['Valerie Bertonelli', 'Professional Boxer', 'I would have to

be pretty drunk']

);

$Hawt = $Hawtie{’10’}[0];

print $Hawt . "nn";

$Not = $Hawtie{‘1′}[0];

print $Not;

Above we created the hash %Hawtie and stored two list values in it, the first containing our “10,” Angelina Jolie and the second containing our “1,” Valerie BertandErnie. We then stored each value in a hot ($Hawt) variable or a not ($Not) variable and printed each out.

Here is what we got:

Conclusion

Well that is all the time we have left for this article, and for the time being, it wraps up our discussion of lists and hashes. In our next series we will be looking more in-depth at Conditionals and Loops and learning to use them in a more advanced way.

Till then…

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan