Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: using foreach

by thezip (Vicar)
on Jul 25, 2008 at 23:57 UTC ( #700256=note: print w/replies, xml ) Need Help??

in reply to using foreach

Indeed, it is not a good idea to manipulate a stack unless you follow the specific interface that applies to a stack:

  1. A stack is like a stack of dishes in that you may only place items onto the top of the stack, and you may only pop items from the top of the stack.

  2. You must always check to see if the stack isEmpty() before attempting to pop.

  3. If desired, you may implement a sizeOf feature, and its purpose is to return the current number of items in the stack. (This count can be used to implement the isEmpty() function.)

In your code, I do not see any place where you check for the empty stack, so in another context (where it's not obvious that there are items on the stack), it would be a potential error to attempt to pop from a potentially empty stack.

While it is true that a "while loop" is would act in this capacity, you have used a "for loop", and should therefore check for empty prior to a pop.

Your wish is my commandline.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://700256]
[Corion]: Oh yay. $project has faffed around for 2 years and now pushed a hasty 2 weeks solution into production without telling me. Of course, the new data also needs changes on my side to be processed correctly. Now they try to raise incidents against my ...
[Corion]: ... input systems to make the change outside of the organized project because I don't have ressources for the project to make the change.

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2017-08-17 09:12 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (285 votes). Check out past polls.