Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Open Source > Perl
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 25th, 2007, 02:20 PM
Authorized User
Join Date: Apr 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Define:Push and pop

How doe's push and pop work? I have been reading alot about perl but I don't understand how this script Works.I have been trying to break it down one by one but i still don't get it Can some help.

# stacks.plx
use warnings;
use strict;

my $hand;
my @pileofpaper = ("letter", "newspaper", "gas bill", "notepad");

print "Here's what's on the desk: @pileofpaper\n";

print "You pick up something off the top of the pile.\n";
$hand = pop @pileofpaper;
print "You have now a $hand in your hand.\n";

print "You put the $hand away, and pick up something else.\n";
$hand = pop @pileofpaper;
print "You picked up a $hand.\n";

print "Left on the desk is: @pileofpaper\n";

print "You pick up the next thing, and throw it away.\n";
pop @pileofpaper;

print "You put the $hand back on the pile.\n";
push @pileofpaper, $hand;

print "You also put a leaflet and a bank statement on the pile.\n";
push @pileofpaper, "leaflet", "bank statement";

print "Left on the desk is: @pileofpaper\n";

Old April 27th, 2007, 10:17 AM
Friend of Wrox
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts

Imagine your stack goes from left to right like this:

1 2 3 4

pop($stack) removes the 1 from the left of the stack and you're left with

2 3 4

push($stack, 5) pushes 5 on to the left of the stack so you're left with

5 2 3 4

Normally people think of stacks vertically with what I've called the left at the top.


Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk

Similar Threads
Thread Thread Starter Forum Replies Last Post
push to multi dimensional harpua PHP How-To 0 December 4th, 2006 01:47 AM
push to users dfeuerborn VB Databases Basics 2 August 29th, 2005 10:18 AM
push to users dfeuerborn Access VBA 3 August 22nd, 2005 03:10 AM
Pull or Push? sureshvb Crystal Reports 0 August 23rd, 2004 01:13 AM
how far to push Access DB ? seanUK Access 2 July 31st, 2004 05:41 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.