FizzBuzz/AWK: Difference between revisions
Content added Content deleted
(→Custom FizzBuzz: Example 2) |
|||
Line 117: | Line 117: | ||
==Custom FizzBuzz== |
==Custom FizzBuzz== |
||
Example program |
===Example program 1=== |
||
generated from [[General_FizzBuzz#AWK]], |
|||
for factors 2, 3, 5 using the words A, B, C. <br> |
for factors 2, 3, 5 using the words A, B, C. <br> |
||
The list of numbers is also generated (much like in version 2 and 3), |
The list of numbers is also generated (much like in version 2 and 3), |
||
and comes in as the file numbers.txt. |
and comes in as the file numbers.txt. |
||
;Input: |
|||
<pre>31 |
|||
2 A |
|||
3 B |
|||
5 C</pre> |
|||
<!-- http://ideone.com/yw1oEK --> |
<!-- http://ideone.com/yw1oEK --> |
||
<lang AWK># usage: awk -f fizzbuzzCustom.awk numbers.txt |
<lang AWK># usage: awk -f fizzbuzzCustom.awk numbers.txt |
||
Line 169: | Line 177: | ||
31 |
31 |
||
# Done.</pre> |
# Done.</pre> |
||
===Example program 2=== |
|||
;Input: |
|||
<pre>105 |
|||
3 Fizz |
|||
5 Buzz |
|||
7 Baxx |
|||
</pre> |
|||
<lang AWK>BEGIN {print "# CustomFizzBuzz:"} |
|||
$1 % 3 == 0 {x = x "Fizz"} |
|||
$1 % 5 == 0 {x = x "Buzz"} |
|||
$1 % 7 == 0 {x = x "Baxx"} |
|||
!x {print $1; next} |
|||
{print " ", x; x=""} |
|||
END {print "# Done."}</lang> |