I've been messing around with writing FizzBuzz in perl...to be specific I want to make a nightmare looking line of code just to see if I can do it. This means nested ternary statements of course!
However I'm finding that on the 15's it never prints FizzBuzz, just Fizz. I cannot find a reason because that implies that if the first ternary statement returns true it's just skipping the second statement.
Here's the little nightmare I've come up with. Yes it can be way worse, but I'm really not that strong with perl and this is just an exercise for myself:
#!/usr/bin/perl
for (my $i=1; $i<=100; $i++) {
( !( ($i % 3 == 0) ? print "Fizz" : 0) && !( ($i % 5 == 0) ? print "Buzz" : 0) ) ? print "$i\n" : print "\n";
}
Here's the first 20 lines of output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz
16
17
Fizz
19
Buzz
What would the print statement be doing that would cause this to happen?
Simplified:
for my $i ( 1 .. 20 ) {
print +( ( $i % 3 ? '' : 'Fizz' ) . ( $i % 5 ? '' : 'Buzz' ) ) || $i, "\n";
}
Outputs:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz