Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Boolean counter?

by vitoco (Friar)
on Dec 07, 2009 at 12:47 UTC ( #811492=note: print w/ replies, xml ) Need Help??


in reply to Boolean counter?

Two more approaches:

- Using hashes:

my %Next = ( 0 => 1, 1 => 0); my $Counter = 0; #... $Counter = $Next{$Counter};

- Decreasing instead of increasing:

$Counter = abs --$Counter;


Comment on Re: Boolean counter?
Select or Download Code
Re^2: Boolean counter?
by shawnhcorey (Pilgrim) on Dec 07, 2009 at 14:07 UTC
    $Counter = - --$Counter;
      The behaviour of that is actually not defined. You may end up with $Counter being -1, or with purple daemons coming out of your USB port, ready to chew off your fingers.
        What about it is not defined?
        $i appears only once, the behaviour is defined
        $ perl -MO=Deparse,-p -le"print - --$i" BEGIN { $/ = "\n"; $\ = "\n"; } print((-(--$i))); -e syntax OK
        You were thinking of
        print(-( ++$i - --$i ))

      Don't modify a variable that you're using elsewhere in an expression. At best, it's unclear. At worse, the result is undefined.

      $toggle = - --$toggle;
      should be
      $toggle = -( $toggle - 1 );
      which can be shortened to previously mentioned
      $toggle = 1 - $toggle;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://811492]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-08-20 13:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (113 votes), past polls