Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Adding 2 + 2 (stack machine)

by ikegami (Pope)
on Jan 30, 2007 at 18:10 UTC ( #597421=note: print w/replies, xml ) Need Help??


in reply to Adding 2 + 2

Stack machine!
my @stack; my $acc = 0; sub PUSH() { push(@stack, $acc); } sub POP() { pop(@stack); } sub INC() { $acc++; } sub ADD() { $acc += POP; } INC; INC; PUSH; ADD; print($acc, "\n");

Replies are listed 'Best First'.
Re^2: Adding 2 + 2 (stack machine,bug fix)
by ikegami (Pope) on Apr 21, 2008 at 19:12 UTC
    The parent assumed the accumulator was initially zero. That's not likely in a real-life scenario, so this removes the assumption:
    my @stack; my $acc = rand; sub PUSH() { push(@stack, $acc); } sub POP() { pop(@stack); } sub INC() { $acc++; } sub ADD() { $acc += POP; } sub SUB() { $acc -= POP; } PUSH; SUB; INC; INC; PUSH; ADD; print("$acc\n");

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://597421]
help
Chatterbox?
[marioroy]: Discipulus Thanks, nice thread.
[r@j]: I am new to perl script, seeking your advice for the following requirement in perl script - Need to retrieve the status of running process with the process name without using ps -ef command.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2017-10-24 07:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (286 votes). Check out past polls.

    Notices?