Perl Lists: More on Manipulation - Pop() Goes Your Data (
Page 2 of 4 )
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."