Hans Ginzel wrote:
Hello!

Hello,

Is there a shorter way to write $a = ! $a, please?

Something analogous to ++ and -- operators like $a !! or !! $a would negate
the variable $a and return its previous or new value respectively.

You can do that if you use a reference to a scalar like this:

$ perl -le'my $x = do { \vec my $y, 0, 1 }; for ( 1 .. 6 ) { print ++$$x }'
1
0
1
0
1
0
$ perl -le'my $x = do { \vec my $y, 0, 1 }; for ( 1 .. 6 ) { print --$$x }'
1
0
1
0
1
0
$ perl -le'my $x = do { \vec my $y, 0, 1 }; for ( 1 .. 6 ) { print $$x++ }'
0
1
0
1
0
1
$ perl -le'my $x = do { \vec my $y, 0, 1 }; for ( 1 .. 6 ) { print $$x-- }'
0
1
0
1
0
1

I used the do {} block so that $y doesn't leak out into the rest of the program, but you can omit it if you want.



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to