Home arrow Perl Programming arrow Page 2 - Perl Lists: More on Manipulation

Pop() Goes Your Data - Perl

In our last article we talked about lists, going somewhat in-depth on their capabilities. We talked about creating them, printing from them, and even used a slicing technique to add and remove from our lists. In this article we will start off looking at more traditional ways to add and remove from a list, and move on from there.

TABLE OF CONTENTS:
  1. Perl Lists: More on Manipulation
  2. Pop() Goes Your Data
  3. Unshift My Heart
  4. Splice...It Sounds Like a New Citrus Soda, But It's Not
By: James Payne
Rating: starstarstarstarstar / 4
March 24, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

What else is there to say about the pop() function? Nothing, that's what. Here is the code:


#!/usr/bin/perl

@gladiators=('Nitro ', 'Blaze ', 'CountFistula ', 'TheNutcracker ', 'Glutious-Minimus ');

@new=('Max Fightmaster ');

push(@gladiators, @new);

print @gladiators;

print "\n\n";

$deleted=pop(@gladiators);

print @gladiators;

print "\n\n";

print $deleted;

This code again creates the two lists and adds the values to them, then uses push() to add the data from @new to @gladiators. We print out the value of @gladiators to show it worked, and then create a new variable named $deleted, using the pop() function to remove the last element in the @gladiators array and store it in $deleted. Finally, we print out the values of both the @gladiator list, and the $deleted variable, resulting in (takes a deep breath):

  Nitro Blaze CountFistula TheNutcracker Glutious-Minimus Max Fightmaster

  Nitro Blaze CountFistula TheNutcracker Glutious-Minimus

  Max Fightmaster

You will note that when we remove data from a list in this manner it gets stored in a variable, not a list. Likewise, you cannot, with this method, deduct data from a list using another list; again, this is because you are simply removing the end element, and not "elements."



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