Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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;

Replies are listed 'Best First'.
Re^2: Boolean counter?
by shawnhcorey (Friar) on Dec 07, 2009 at 14:07 UTC
    $Counter = - --$Counter;

      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;
      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.
        $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 ))
        What about it is not defined?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://811492]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2018-04-20 00:08 GMT
Find Nodes?
    Voting Booth?