Re:I can code that FizzBuzz function with only two tests
by rethab on Tuesday June 09, 2020 @03:50AM
(#23464648)
Or with AWK:
echo 15 | awk '$1%3==0{s=s"Fizz"} $1%5==0{s=s"Buzz"} END{print s}'
[ Parent
| on HN ]
Re:I can code that FizzBuzz function with only two tests
by eesmith on Tuesday June 09, 2020 @05:16AM
(#23465041)
Not that it affects your overall point, but you probably want:
awk '$1%3==0{s=s"Fizz"} $1%5==0{s=s"Buzz"} {print s;s=""}'
Otherwise, consider: % printf "3\n5\n9\n15\n16\n" | awk '$1%3==0{s=s"Fizz"} $1%5==0{s=s"Buzz"} END{print s}'
FizzBuzzFizzFizzBuzz
For bonus points, the following won't treat non-numerics as FizzBuzz: awk '$1+0\!=$1 {print"";next} $1%3==0{s=s"Fizz"} $1%5==0{s=s"Buzz"} {print s;s=""}'