diff options
author | Arnold D. Robbins <arnold@skeeve.com> | 2014-06-16 22:53:15 +0300 |
---|---|---|
committer | Arnold D. Robbins <arnold@skeeve.com> | 2014-06-16 22:53:15 +0300 |
commit | 80dcefc97cb6255c20ff7a678980cac0896a3676 (patch) | |
tree | 415508062059342a34aa3dcf9380aff819ff33ba | |
parent | 10552910dbf1b61736ac1869e4c5a9f347039918 (diff) | |
parent | 3a73ec7f27db0fec9ca68e97800f30f9ad33e293 (diff) | |
download | egawk-80dcefc97cb6255c20ff7a678980cac0896a3676.tar.gz egawk-80dcefc97cb6255c20ff7a678980cac0896a3676.tar.bz2 egawk-80dcefc97cb6255c20ff7a678980cac0896a3676.zip |
Merge branch 'gawk-4.1-stable'
-rw-r--r-- | doc/ChangeLog | 4 | ||||
-rw-r--r-- | doc/gawk.info | 1065 | ||||
-rw-r--r-- | doc/gawk.texi | 200 | ||||
-rw-r--r-- | doc/gawktexi.in | 200 |
4 files changed, 801 insertions, 668 deletions
diff --git a/doc/ChangeLog b/doc/ChangeLog index d952d239..bb6711b5 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,7 @@ +2014-06-16 Arnold D. Robbins <arnold@skeeve.com> + + * gawktexi.in: Start adding exercises. + 2014-06-15 Arnold D. Robbins <arnold@skeeve.com> * gawktexi.in: Finish up summaries. Improvements in mystrtonum(). diff --git a/doc/gawk.info b/doc/gawk.info index 4b669731..2dcd925e 100644 --- a/doc/gawk.info +++ b/doc/gawk.info @@ -4044,6 +4044,7 @@ have to be named on the `awk' command line (*note Getline::). * Command line directories:: What happens if you put a directory on the command line. * Input Summary:: Input summary. +* Input Exercises:: Exercises. File: gawk.info, Node: Records, Next: Fields, Up: Reading Files @@ -5854,7 +5855,7 @@ error. usable data from an `awk' program. -File: gawk.info, Node: Input Summary, Prev: Command line directories, Up: Reading Files +File: gawk.info, Node: Input Summary, Next: Input Exercises, Prev: Command line directories, Up: Reading Files 4.12 Summary ============ @@ -5923,6 +5924,25 @@ File: gawk.info, Node: Input Summary, Prev: Command line directories, Up: Rea +File: gawk.info, Node: Input Exercises, Prev: Input Summary, Up: Reading Files + +4.13 Exercises +============== + + 1. Using the `FIELDWIDTHS' variable (*note Constant Size::), write a + program to read election data, where each record represents one + voter's votes. Come up with a way to define which columns are + associated with each ballot item, and print the total votes, + including abstentions, for each item. + + 2. *note Plain Getline::, presented a program to remove C-style + comments (`/* ... */') from the input. That program does not work + if one comment ends on one line and another one starts later on + the same line. Write a program that does handle multiple comments + on the line. + + + File: gawk.info, Node: Printing, Next: Expressions, Prev: Reading Files, Up: Top 5 Printing Output @@ -5957,6 +5977,7 @@ function. descriptors. * Close Files And Pipes:: Closing Input and Output Files and Pipes. * Output Summary:: Output summary. +* Output exercises:: Exercises. File: gawk.info, Node: Print, Next: Print Examples, Up: Printing @@ -6548,11 +6569,6 @@ be emphasized by storing it in a variable, like this: printf format, "----", "------" } { printf format, $1, $2 }' mail-list - At this point, it would be a worthwhile exercise to use the `printf' -statement to line up the headings and table data for the -`inventory-shipped' example that was covered earlier in the minor node -on the `print' statement (*note Print::). - File: gawk.info, Node: Redirection, Next: Special Files, Prev: Printf, Up: Printing @@ -7017,7 +7033,7 @@ call. See the system manual pages for information on how to decode this value. -File: gawk.info, Node: Output Summary, Prev: Close Files And Pipes, Up: Printing +File: gawk.info, Node: Output Summary, Next: Output exercises, Prev: Close Files And Pipes, Up: Printing 5.9 Summary =========== @@ -7043,6 +7059,30 @@ File: gawk.info, Node: Output Summary, Prev: Close Files And Pipes, Up: Print +File: gawk.info, Node: Output exercises, Prev: Output Summary, Up: Printing + +5.10 Exercises +============== + + 1. Rewrite the program: + + awk 'BEGIN { print "Month Crates" + print "----- ------" } + { print $1, " ", $2 }' inventory-shipped + + from *note Output Separators::, by using a new value of `OFS'. + + 2. Use the `printf' statement to line up the headings and table data + for the `inventory-shipped' example that was covered in *note + Print::. + + 3. What happens if you forget the double quotes when redirecting + output, as follows: + + BEGIN { print "Serious error detected!" > /dev/stderr } + + + File: gawk.info, Node: Expressions, Next: Patterns and Actions, Prev: Printing, Up: Top 6 Expressions @@ -14135,6 +14175,7 @@ for different implementations of `awk' is pretty straightforward. * Group Functions:: Functions for getting group information. * Walking Arrays:: A function to walk arrays of arrays. * Library Functions Summary:: Summary of library functions. +* Library exercises:: Exercises. ---------- Footnotes ---------- @@ -15020,12 +15061,6 @@ normal case. end of the command-line arguments. Note that the test in the condition of the `for' loop uses the `<=' operator, not `<'. - As an exercise, you might consider whether this same problem can be -solved without relying on `gawk''s `ARGIND' variable. - - As a second exercise, revise this code to handle the case where an -intervening value in `ARGV' is a variable assignment. - File: gawk.info, Node: Ignoring Assigns, Prev: Empty Files, Up: Data File Management @@ -15894,20 +15929,8 @@ value. Here is a main program to demonstrate: -| a[2][2] = 22 -| a[3] = 3 - Walking an array and processing each element is a general-purpose -operation. You might want to consider generalizing the `walk_array()' -function by adding an additional parameter named `process'. - - Then, inside the loop, instead of simply printing the array element's -index and value, use the indirect function call syntax (*note Indirect -Calls::) on `process', passing it the index and the value. - - When calling `walk_array()', you would pass the name of a -user-defined function that expects to receive an index and a value, and -then processes the element. - -File: gawk.info, Node: Library Functions Summary, Prev: Walking Arrays, Up: Library Functions +File: gawk.info, Node: Library Functions Summary, Next: Library exercises, Prev: Walking Arrays, Up: Library Functions 10.8 Summary ============ @@ -15944,6 +15967,37 @@ File: gawk.info, Node: Library Functions Summary, Prev: Walking Arrays, Up: L +File: gawk.info, Node: Library exercises, Prev: Library Functions Summary, Up: Library Functions + +10.9 Exercises +============== + + 1. In *note Empty Files::, we presented the `zerofile.awk' program, + which made use of `gawk''s `ARGIND' variable. Can this problem be + solved without relying on `ARGIND'? If so, how? + + 2. As a related challenge, revise that code to handle the case where + an intervening value in `ARGV' is a variable assignment. + + 3. *note Walking Arrays::, presented a function that walked a + multidimensional array to print it out. However, walking an array + and processing each element is a general-purpose operation. + Generalize the `walk_array()' function by adding an additional + parameter named `process'. + + Then, inside the loop, instead of printing the array element's + index and value, use the indirect function call syntax (*note + Indirect Calls::) on `process', passing it the index and the value. + + When calling `walk_array()', you would pass the name of a + user-defined function that expects to receive an index and a value, + and then processes the element. + + Test your new version by printing the array; you should end up with + output identical to that of the original version. + + + File: gawk.info, Node: Sample Programs, Next: Advanced Features, Prev: Library Functions, Up: Top 11 Practical `awk' Programs @@ -34086,483 +34140,486 @@ Node: Leftmost Longest176321 Node: Computed Regexps177522 Node: Regexp Summary180894 Node: Reading Files182366 -Node: Records184415 -Node: awk split records185158 -Node: gawk split records190016 -Ref: gawk split records-Footnote-1194537 -Node: Fields194574 -Ref: Fields-Footnote-1197538 -Node: Nonconstant Fields197624 -Ref: Nonconstant Fields-Footnote-1199854 -Node: Changing Fields200056 -Node: Field Separators206010 -Node: Default Field Splitting208712 -Node: Regexp Field Splitting209829 -Node: Single Character Fields213170 -Node: Command Line Field Separator214229 -Node: Full Line Fields217571 -Ref: Full Line Fields-Footnote-1218079 -Node: Field Splitting Summary218125 -Ref: Field Splitting Summary-Footnote-1221224 -Node: Constant Size221325 -Node: Splitting By Content225932 -Ref: Splitting By Content-Footnote-1229682 -Node: Multiple Line229722 -Ref: Multiple Line-Footnote-1235578 -Node: Getline235757 -Node: Plain Getline237973 -Node: Getline/Variable240068 -Node: Getline/File241215 -Node: Getline/Variable/File242599 -Ref: Getline/Variable/File-Footnote-1244198 -Node: Getline/Pipe244285 -Node: Getline/Variable/Pipe246984 -Node: Getline/Coprocess248091 -Node: Getline/Variable/Coprocess249343 -Node: Getline Notes250080 -Node: Getline Summary252884 -Ref: table-getline-variants253292 -Node: Read Timeout254204 -Ref: Read Timeout-Footnote-1258031 -Node: Command line directories258089 -Node: Input Summary258993 -Node: Printing262107 -Node: Print263786 -Node: Print Examples265127 -Node: Output Separators267906 -Node: OFMT269922 -Node: Printf271280 -Node: Basic Printf272186 -Node: Control Letters273725 -Node: Format Modifiers277579 -Node: Printf Examples283606 -Node: Redirection286313 -Node: Special Files293285 -Node: Special FD293816 -Ref: Special FD-Footnote-1297440 -Node: Special Network297514 -Node: Special Caveats298364 -Node: Close Files And Pipes299160 -Ref: Close Files And Pipes-Footnote-1306323 -Ref: Close Files And Pipes-Footnote-2306471 -Node: Output Summary306621 -Node: Expressions307593 -Node: Values308778 -Node: Constants309454 -Node: Scalar Constants310134 -Ref: Scalar Constants-Footnote-1310993 -Node: Nondecimal-numbers311243 -Node: Regexp Constants314243 -Node: Using Constant Regexps314718 -Node: Variables317788 -Node: Using Variables318443 -Node: Assignment Options320167 -Node: Conversion322042 -Ref: table-locale-affects327478 -Ref: Conversion-Footnote-1328102 -Node: All Operators328211 -Node: Arithmetic Ops328841 -Node: Concatenation331346 -Ref: Concatenation-Footnote-1334142 -Node: Assignment Ops334262 -Ref: table-assign-ops339245 -Node: Increment Ops340562 -Node: Truth Values and Conditions344000 -Node: Truth Values345083 -Node: Typing and Comparison346132 -Node: Variable Typing346925 -Ref: Variable Typing-Footnote-1350825 -Node: Comparison Operators350947 -Ref: table-relational-ops351357 -Node: POSIX String Comparison354907 -Ref: POSIX String Comparison-Footnote-1355991 -Node: Boolean Ops356129 -Ref: Boolean Ops-Footnote-1360199 -Node: Conditional Exp360290 -Node: Function Calls362017 -Node: Precedence365775 -Node: Locales369444 -Node: Expressions Summary371075 -Node: Patterns and Actions373572 -Node: Pattern Overview374688 -Node: Regexp Patterns376365 -Node: Expression Patterns376908 -Node: Ranges380689 -Node: BEGIN/END383795 -Node: Using BEGIN/END384557 -Ref: Using BEGIN/END-Footnote-1387293 -Node: I/O And BEGIN/END387399 -Node: BEGINFILE/ENDFILE389684 -Node: Empty392615 -Node: Using Shell Variables392932 -Node: Action Overview395215 -Node: Statements397542 -Node: If Statement399390 -Node: While Statement400888 -Node: Do Statement402932 -Node: For Statement404088 -Node: Switch Statement407240 -Node: Break Statement409343 -Node: Continue Statement411398 -Node: Next Statement413191 -Node: Nextfile Statement415581 -Node: Exit Statement418236 -Node: Built-in Variables420640 -Node: User-modified421767 -Ref: User-modified-Footnote-1429452 -Node: Auto-set429514 -Ref: Auto-set-Footnote-1442416 -Ref: Auto-set-Footnote-2442621 -Node: ARGC and ARGV442677 -Node: Pattern Action Summary446531 -Node: Arrays448754 -Node: Array Basics450303 -Node: Array Intro451129 -Ref: figure-array-elements453102 -Node: Reference to Elements455509 -Node: Assigning Elements457782 -Node: Array Example458273 -Node: Scanning an Array460005 -Node: Controlling Scanning463020 -Ref: Controlling Scanning-Footnote-1468193 -Node: Delete468509 -Ref: Delete-Footnote-1471274 -Node: Numeric Array Subscripts471331 -Node: Uninitialized Subscripts473514 -Node: Multidimensional475139 -Node: Multiscanning478232 -Node: Arrays of Arrays479821 -Node: Arrays Summary484484 -Node: Functions486589 -Node: Built-in487462 -Node: Calling Built-in488540 -Node: Numeric Functions490528 -Ref: Numeric Functions-Footnote-1494362 -Ref: Numeric Functions-Footnote-2494719 -Ref: Numeric Functions-Footnote-3494767 -Node: String Functions495036 -Ref: String Functions-Footnote-1518047 -Ref: String Functions-Footnote-2518176 -Ref: String Functions-Footnote-3518424 -Node: Gory Details518511 -Ref: table-sub-escapes520180 -Ref: table-sub-posix-92521534 -Ref: table-sub-proposed522885 -Ref: table-posix-sub524239 -Ref: table-gensub-escapes525784 -Ref: Gory Details-Footnote-1526960 -Ref: Gory Details-Footnote-2527011 -Node: I/O Functions527162 -Ref: I/O Functions-Footnote-1534285 -Node: Time Functions534432 -Ref: Time Functions-Footnote-1544896 -Ref: Time Functions-Footnote-2544964 -Ref: Time Functions-Footnote-3545122 -Ref: Time Functions-Footnote-4545233 -Ref: Time Functions-Footnote-5545345 -Ref: Time Functions-Footnote-6545572 -Node: Bitwise Functions545838 -Ref: table-bitwise-ops546400 -Ref: Bitwise Functions-Footnote-1550645 -Node: Type Functions550829 -Node: I18N Functions551971 -Node: User-defined553616 -Node: Definition Syntax554420 -Ref: Definition Syntax-Footnote-1559345 -Node: Function Example559414 -Ref: Function Example-Footnote-1562058 -Node: Function Caveats562080 -Node: Calling A Function562598 -Node: Variable Scope563553 -Node: Pass By Value/Reference566541 -Node: Return Statement570049 -Node: Dynamic Typing573033 -Node: Indirect Calls573962 -Node: Functions Summary583675 -Node: Library Functions586214 -Ref: Library Functions-Footnote-1589789 -Ref: Library Functions-Footnote-2589932 -Node: Library Names590103 -Ref: Library Names-Footnote-1593576 -Ref: Library Names-Footnote-2593796 -Node: General Functions593882 -Node: Strtonum Function594910 -Node: Assert Function597690 -Node: Round Function601016 -Node: Cliff Random Function602557 -Node: Ordinal Functions603573 -Ref: Ordinal Functions-Footnote-1606650 -Ref: Ordinal Functions-Footnote-2606902 -Node: Join Function607113 -Ref: Join Function-Footnote-1608884 -Node: Getlocaltime Function609084 -Node: Readfile Function612820 -Node: Data File Management614659 -Node: Filetrans Function615291 -Node: Rewind Function619360 -Node: File Checking620747 -Ref: File Checking-Footnote-1621879 -Node: Empty Files622080 -Node: Ignoring Assigns624310 -Node: Getopt Function625864 -Ref: Getopt Function-Footnote-1637167 -Node: Passwd Functions637370 -Ref: Passwd Functions-Footnote-1646349 -Node: Group Functions646437 -Ref: Group Functions-Footnote-1654379 -Node: Walking Arrays654592 -Node: Library Functions Summary656762 -Node: Sample Programs658124 -Node: Running Examples658851 -Node: Clones659579 -Node: Cut Program660803 -Node: Egrep Program670671 -Ref: Egrep Program-Footnote-1678642 -Node: Id Program678752 -Node: Split Program682416 -Ref: Split Program-Footnote-1685954 -Node: Tee Program686082 -Node: Uniq Program688889 -Node: Wc Program696319 -Ref: Wc Program-Footnote-1700587 -Ref: Wc Program-Footnote-2700787 -Node: Miscellaneous Programs700879 -Node: Dupword Program702092 -Node: Alarm Program704123 -Node: Translate Program708937 -Ref: Translate Program-Footnote-1713328 -Ref: Translate Program-Footnote-2713598 -Node: Labels Program713732 -Ref: Labels Program-Footnote-1717103 -Node: Word Sorting717187 -Node: History Sorting721230 -Node: Extract Program723066 -Ref: Extract Program-Footnote-1730641 -Node: Simple Sed730770 -Node: Igawk Program733832 -Ref: Igawk Program-Footnote-1749008 -Ref: Igawk Program-Footnote-2749209 -Node: Anagram Program749347 -Node: Signature Program752415 -Node: Programs Summary753662 -Node: Advanced Features754850 -Node: Nondecimal Data756798 -Node: Array Sorting758375 -Node: Controlling Array Traversal759072 -Node: Array Sorting Functions767352 -Ref: Array Sorting Functions-Footnote-1771259 -Node: Two-way I/O771453 -Ref: Two-way I/O-Footnote-1776969 -Node: TCP/IP Networking777051 -Node: Profiling779895 -Node: Advanced Features Summary787437 -Node: Internationalization789301 -Node: I18N and L10N790781 -Node: Explaining gettext791467 -Ref: Explaining gettext-Footnote-1796607 -Ref: Explaining gettext-Footnote-2796791 -Node: Programmer i18n796956 -Node: Translator i18n801181 -Node: String Extraction801975 -Ref: String Extraction-Footnote-1802936 -Node: Printf Ordering803022 -Ref: Printf Ordering-Footnote-1805804 -Node: I18N Portability805868 -Ref: I18N Portability-Footnote-1808317 -Node: I18N Example808380 -Ref: I18N Example-Footnote-1811102 -Node: Gawk I18N811174 -Node: I18N Summary811812 -Node: Debugger813151 -Node: Debugging814173 -Node: Debugging Concepts814614 -Node: Debugging Terms816470 -Node: Awk Debugging819067 -Node: Sample Debugging Session819959 -Node: Debugger Invocation820479 -Node: Finding The Bug821812 -Node: List of Debugger Commands828294 -Node: Breakpoint Control829626 -Node: Debugger Execution Control833290 -Node: Viewing And Changing Data836650 -Node: Execution Stack840008 -Node: Debugger Info841521 -Node: Miscellaneous Debugger Commands845515 -Node: Readline Support850699 -Node: Limitations851591 -Node: Debugging Summary853865 -Node: Arbitrary Precision Arithmetic855029 -Ref: Arbitrary Precision Arithmetic-Footnote-1856678 -Node: General Arithmetic856826 -Node: Floating Point Issues858546 -Node: String Conversion Precision859427 -Ref: String Conversion Precision-Footnote-1861132 -Node: Unexpected Results861241 -Node: POSIX Floating Point Problems863394 -Ref: POSIX Floating Point Problems-Footnote-1867215 -Node: Integer Programming867253 -Node: Floating-point Programming869064 -Ref: Floating-point Programming-Footnote-1875392 -Ref: Floating-point Programming-Footnote-2875662 -Node: Floating-point Representation875926 -Node: Floating-point Context877091 -Ref: table-ieee-formats877930 -Node: Rounding Mode879314 -Ref: table-rounding-modes879793 -Ref: Rounding Mode-Footnote-1882808 -Node: Gawk and MPFR882987 -Node: Arbitrary Precision Floats884396 -Ref: Arbitrary Precision Floats-Footnote-1886839 -Node: Setting Precision887160 -Ref: table-predefined-precision-strings887844 -Node: Setting Rounding Mode889989 -Ref: table-gawk-rounding-modes890393 -Node: Floating-point Constants891580 -Node: Changing Precision893032 -Ref: Changing Precision-Footnote-1894424 -Node: Exact Arithmetic894598 -Node: Arbitrary Precision Integers897732 -Ref: Arbitrary Precision Integers-Footnote-1900747 -Node: Dynamic Extensions900894 -Node: Extension Intro902403 -Node: Plugin License903668 -Node: Extension Mechanism Outline904353 -Ref: figure-load-extension904777 -Ref: figure-load-new-function906262 -Ref: figure-call-new-function907264 -Node: Extension API Description909248 -Node: Extension API Functions Introduction910698 -Node: General Data Types915563 -Ref: General Data Types-Footnote-1921256 -Node: Requesting Values921555 -Ref: table-value-types-returned922292 -Node: Memory Allocation Functions923250 -Ref: Memory Allocation Functions-Footnote-1925997 -Node: Constructor Functions926093 -Node: Registration Functions927851 -Node: Extension Functions928536 -Node: Exit Callback Functions930838 -Node: Extension Version String932087 -Node: Input Parsers932737 -Node: Output Wrappers942540 -Node: Two-way processors947056 -Node: Printing Messages949260 -Ref: Printing Messages-Footnote-1950337 -Node: Updating `ERRNO'950489 -Node: Accessing Parameters951228 -Node: Symbol Table Access952458 -Node: Symbol table by name952972 -Node: Symbol table by cookie954948 -Ref: Symbol table by cookie-Footnote-1959081 -Node: Cached values959144 -Ref: Cached values-Footnote-1962648 -Node: Array Manipulation962739 -Ref: Array Manipulation-Footnote-1963837 -Node: Array Data Types963876 -Ref: Array Data Types-Footnote-1966579 -Node: Array Functions966671 -Node: Flattening Arrays970545 -Node: Creating Arrays977397 -Node: Extension API Variables982128 -Node: Extension Versioning982764 -Node: Extension API Informational Variables984665 -Node: Extension API Boilerplate985751 -Node: Finding Extensions989555 -Node: Extension Example990115 -Node: Internal File Description990845 -Node: Internal File Ops994936 -Ref: Internal File Ops-Footnote-11006482 -Node: Using Internal File Ops1006622 -Ref: Using Internal File Ops-Footnote-11008969 -Node: Extension Samples1009237 -Node: Extension Sample File Functions1010761 -Node: Extension Sample Fnmatch1018329 -Node: Extension Sample Fork1019810 -Node: Extension Sample Inplace1021023 -Node: Extension Sample Ord1022803 -Node: Extension Sample Readdir1023639 -Ref: table-readdir-file-types1024495 -Node: Extension Sample Revout1025294 -Node: Extension Sample Rev2way1025885 -Node: Extension Sample Read write array1026626 -Node: Extension Sample Readfile1028505 -Node: Extension Sample API Tests1029605 -Node: Extension Sample Time1030130 -Node: gawkextlib1031445 -Node: Extension summary1034258 -Node: Language History1037923 -Node: V7/SVR3.11039566 -Node: SVR41041886 -Node: POSIX1043328 -Node: BTL1044714 -Node: POSIX/GNU1045448 -Node: Feature History1051047 -Node: Common Extensions1064159 -Node: Ranges and Locales1065471 -Ref: Ranges and Locales-Footnote-11070088 -Ref: Ranges and Locales-Footnote-21070115 -Ref: Ranges and Locales-Footnote-31070349 -Node: Contributors1070570 -Node: History summary1076032 -Node: Installation1077401 -Node: Gawk Distribution1078352 -Node: Getting1078836 -Node: Extracting1079662 -Node: Distribution contents1081304 -Node: Unix Installation1087021 -Node: Quick Installation1087638 -Node: Additional Configuration Options1090080 -Node: Configuration Philosophy1091818 -Node: Non-Unix Installation1094169 -Node: PC Installation1094627 -Node: PC Binary Installation1095938 -Node: PC Compiling1097786 -Ref: PC Compiling-Footnote-11100785 -Node: PC Testing1100890 -Node: PC Using1102066 -Node: Cygwin1106224 -Node: MSYS1107033 -Node: VMS Installation1107547 -Node: VMS Compilation1108343 -Ref: VMS Compilation-Footnote-11109565 -Node: VMS Dynamic Extensions1109623 -Node: VMS Installation Details1110996 -Node: VMS Running1113248 -Node: VMS GNV1116082 -Node: VMS Old Gawk1116805 -Node: Bugs1117275 -Node: Other Versions1121279 -Node: Installation summary1127533 -Node: Notes1128588 -Node: Compatibility Mode1129453 -Node: Additions1130235 -Node: Accessing The Source1131160 -Node: Adding Code1132596 -Node: New Ports1138774 -Node: Derived Files1143255 -Ref: Derived Files-Footnote-11148336 -Ref: Derived Files-Footnote-21148370 -Ref: Derived Files-Footnote-31148966 -Node: Future Extensions1149080 -Node: Implementation Limitations1149686 -Node: Extension Design1150934 -Node: Old Extension Problems1152088 -Ref: Old Extension Problems-Footnote-11153605 -Node: Extension New Mechanism Goals1153662 -Ref: Extension New Mechanism Goals-Footnote-11157022 -Node: Extension Other Design Decisions1157211 -Node: Extension Future Growth1159317 -Node: Old Extension Mechanism1160153 -Node: Notes summary1161915 -Node: Basic Concepts1163100 -Node: Basic High Level1163781 -Ref: figure-general-flow1164053 -Ref: figure-process-flow1164652 -Ref: Basic High Level-Footnote-11167881 -Node: Basic Data Typing1168066 -Node: Glossary1171393 -Node: Copying1196545 -Node: GNU Free Documentation License1234101 -Node: Index1259237 +Node: Records184458 +Node: awk split records185201 +Node: gawk split records190059 +Ref: gawk split records-Footnote-1194580 +Node: Fields194617 +Ref: Fields-Footnote-1197581 +Node: Nonconstant Fields197667 +Ref: Nonconstant Fields-Footnote-1199897 +Node: Changing Fields200099 +Node: Field Separators206053 +Node: Default Field Splitting208755 +Node: Regexp Field Splitting209872 +Node: Single Character Fields213213 +Node: Command Line Field Separator214272 +Node: Full Line Fields217614 +Ref: Full Line Fields-Footnote-1218122 +Node: Field Splitting Summary218168 +Ref: Field Splitting Summary-Footnote-1221267 +Node: Constant Size221368 +Node: Splitting By Content225975 +Ref: Splitting By Content-Footnote-1229725 +Node: Multiple Line229765 +Ref: Multiple Line-Footnote-1235621 +Node: Getline235800 +Node: Plain Getline238016 +Node: Getline/Variable240111 +Node: Getline/File241258 +Node: Getline/Variable/File242642 +Ref: Getline/Variable/File-Footnote-1244241 +Node: Getline/Pipe244328 +Node: Getline/Variable/Pipe247027 +Node: Getline/Coprocess248134 +Node: Getline/Variable/Coprocess249386 +Node: Getline Notes250123 +Node: Getline Summary252927 +Ref: table-getline-variants253335 +Node: Read Timeout254247 +Ref: Read Timeout-Footnote-1258074 +Node: Command line directories258132 +Node: Input Summary259036 +Node: Input Exercises262174 +Node: Printing262907 +Node: Print264630 +Node: Print Examples265971 +Node: Output Separators268750 +Node: OFMT270766 +Node: Printf272124 +Node: Basic Printf273030 +Node: Control Letters274569 +Node: Format Modifiers278423 +Node: Printf Examples284450 +Node: Redirection286914 +Node: Special Files293886 +Node: Special FD294417 +Ref: Special FD-Footnote-1298041 +Node: Special Network298115 +Node: Special Caveats298965 +Node: Close Files And Pipes299761 +Ref: Close Files And Pipes-Footnote-1306924 +Ref: Close Files And Pipes-Footnote-2307072 +Node: Output Summary307222 +Node: Output exercises308219 +Node: Expressions308899 +Node: Values310084 +Node: Constants310760 +Node: Scalar Constants311440 +Ref: Scalar Constants-Footnote-1312299 +Node: Nondecimal-numbers312549 +Node: Regexp Constants315549 +Node: Using Constant Regexps316024 +Node: Variables319094 +Node: Using Variables319749 +Node: Assignment Options321473 +Node: Conversion323348 +Ref: table-locale-affects328784 +Ref: Conversion-Footnote-1329408 +Node: All Operators329517 +Node: Arithmetic Ops330147 +Node: Concatenation332652 +Ref: Concatenation-Footnote-1335448 +Node: Assignment Ops335568 +Ref: table-assign-ops340551 +Node: Increment Ops341868 +Node: Truth Values and Conditions345306 +Node: Truth Values346389 +Node: Typing and Comparison347438 +Node: Variable Typing348231 +Ref: Variable Typing-Footnote-1352131 +Node: Comparison Operators352253 +Ref: table-relational-ops352663 +Node: POSIX String Comparison356213 +Ref: POSIX String Comparison-Footnote-1357297 +Node: Boolean Ops357435 +Ref: Boolean Ops-Footnote-1361505 +Node: Conditional Exp361596 +Node: Function Calls363323 +Node: Precedence367081 +Node: Locales370750 +Node: Expressions Summary372381 +Node: Patterns and Actions374878 +Node: Pattern Overview375994 +Node: Regexp Patterns377671 +Node: Expression Patterns378214 +Node: Ranges381995 +Node: BEGIN/END385101 +Node: Using BEGIN/END385863 +Ref: Using BEGIN/END-Footnote-1388599 +Node: I/O And BEGIN/END388705 +Node: BEGINFILE/ENDFILE390990 +Node: Empty393921 +Node: Using Shell Variables394238 +Node: Action Overview396521 +Node: Statements398848 +Node: If Statement400696 +Node: While Statement402194 +Node: Do Statement404238 +Node: For Statement405394 +Node: Switch Statement408546 +Node: Break Statement410649 +Node: Continue Statement412704 +Node: Next Statement414497 +Node: Nextfile Statement416887 +Node: Exit Statement419542 +Node: Built-in Variables421946 +Node: User-modified423073 +Ref: User-modified-Footnote-1430758 +Node: Auto-set430820 +Ref: Auto-set-Footnote-1443722 +Ref: Auto-set-Footnote-2443927 +Node: ARGC and ARGV443983 +Node: Pattern Action Summary447837 +Node: Arrays450060 +Node: Array Basics451609 +Node: Array Intro452435 +Ref: figure-array-elements454408 +Node: Reference to Elements456815 +Node: Assigning Elements459088 +Node: Array Example459579 +Node: Scanning an Array461311 +Node: Controlling Scanning464326 +Ref: Controlling Scanning-Footnote-1469499 +Node: Delete469815 +Ref: Delete-Footnote-1472580 +Node: Numeric Array Subscripts472637 +Node: Uninitialized Subscripts474820 +Node: Multidimensional476445 +Node: Multiscanning479538 +Node: Arrays of Arrays481127 +Node: Arrays Summary485790 +Node: Functions487895 +Node: Built-in488768 +Node: Calling Built-in489846 +Node: Numeric Functions491834 +Ref: Numeric Functions-Footnote-1495668 +Ref: Numeric Functions-Footnote-2496025 +Ref: Numeric Functions-Footnote-3496073 +Node: String Functions496342 +Ref: String Functions-Footnote-1519353 +Ref: String Functions-Footnote-2519482 +Ref: String Functions-Footnote-3519730 +Node: Gory Details519817 +Ref: table-sub-escapes521486 +Ref: table-sub-posix-92522840 +Ref: table-sub-proposed524191 +Ref: table-posix-sub525545 +Ref: table-gensub-escapes527090 +Ref: Gory Details-Footnote-1528266 +Ref: Gory Details-Footnote-2528317 +Node: I/O Functions528468 +Ref: I/O Functions-Footnote-1535591 +Node: Time Functions535738 +Ref: Time Functions-Footnote-1546202 +Ref: Time Functions-Footnote-2546270 +Ref: Time Functions-Footnote-3546428 +Ref: Time Functions-Footnote-4546539 +Ref: Time Functions-Footnote-5546651 +Ref: Time Functions-Footnote-6546878 +Node: Bitwise Functions547144 +Ref: table-bitwise-ops547706 +Ref: Bitwise Functions-Footnote-1551951 +Node: Type Functions552135 +Node: I18N Functions553277 +Node: User-defined554922 +Node: Definition Syntax555726 +Ref: Definition Syntax-Footnote-1560651 +Node: Function Example560720 +Ref: Function Example-Footnote-1563364 +Node: Function Caveats563386 +Node: Calling A Function563904 +Node: Variable Scope564859 +Node: Pass By Value/Reference567847 +Node: Return Statement571355 +Node: Dynamic Typing574339 +Node: Indirect Calls575268 +Node: Functions Summary584981 +Node: Library Functions587520 +Ref: Library Functions-Footnote-1591138 +Ref: Library Functions-Footnote-2591281 +Node: Library Names591452 +Ref: Library Names-Footnote-1594925 +Ref: Library Names-Footnote-2595145 +Node: General Functions595231 +Node: Strtonum Function596259 +Node: Assert Function599039 +Node: Round Function602365 +Node: Cliff Random Function603906 +Node: Ordinal Functions604922 +Ref: Ordinal Functions-Footnote-1607999 +Ref: Ordinal Functions-Footnote-2608251 +Node: Join Function608462 +Ref: Join Function-Footnote-1610233 +Node: Getlocaltime Function610433 +Node: Readfile Function614169 +Node: Data File Management616008 +Node: Filetrans Function616640 +Node: Rewind Function620709 +Node: File Checking622096 +Ref: File Checking-Footnote-1623228 +Node: Empty Files623429 +Node: Ignoring Assigns625408 +Node: Getopt Function626962 +Ref: Getopt Function-Footnote-1638265 +Node: Passwd Functions638468 +Ref: Passwd Functions-Footnote-1647447 +Node: Group Functions647535 +Ref: Group Functions-Footnote-1655477 +Node: Walking Arrays655690 +Node: Library Functions Summary657293 +Node: Library exercises658681 +Node: Sample Programs659961 +Node: Running Examples660688 +Node: Clones661416 +Node: Cut Program662640 +Node: Egrep Program672508 +Ref: Egrep Program-Footnote-1680479 +Node: Id Program680589 +Node: Split Program684253 +Ref: Split Program-Footnote-1687791 +Node: Tee Program687919 +Node: Uniq Program690726 +Node: Wc Program698156 +Ref: Wc Program-Footnote-1702424 +Ref: Wc Program-Footnote-2702624 +Node: Miscellaneous Programs702716 +Node: Dupword Program703929 +Node: Alarm Program705960 +Node: Translate Program710774 +Ref: Translate Program-Footnote-1715165 +Ref: Translate Program-Footnote-2715435 +Node: Labels Program715569 +Ref: Labels Program-Footnote-1718940 +Node: Word Sorting719024 +Node: History Sorting723067 +Node: Extract Program724903 +Ref: Extract Program-Footnote-1732478 +Node: Simple Sed732607 +Node: Igawk Program735669 +Ref: Igawk Program-Footnote-1750845 +Ref: Igawk Program-Footnote-2751046 +Node: Anagram Program751184 +Node: Signature Program754252 +Node: Programs Summary755499 +Node: Advanced Features756687 +Node: Nondecimal Data758635 +Node: Array Sorting760212 +Node: Controlling Array Traversal760909 +Node: Array Sorting Functions769189 +Ref: Array Sorting Functions-Footnote-1773096 +Node: Two-way I/O773290 +Ref: Two-way I/O-Footnote-1778806 +Node: TCP/IP Networking778888 +Node: Profiling781732 +Node: Advanced Features Summary789274 +Node: Internationalization791138 +Node: I18N and L10N792618 +Node: Explaining gettext793304 +Ref: Explaining gettext-Footnote-1798444 +Ref: Explaining gettext-Footnote-2798628 +Node: Programmer i18n798793 +Node: Translator i18n803018 +Node: String Extraction803812 +Ref: String Extraction-Footnote-1804773 +Node: Printf Ordering804859 +Ref: Printf Ordering-Footnote-1807641 +Node: I18N Portability807705 +Ref: I18N Portability-Footnote-1810154 +Node: I18N Example810217 +Ref: I18N Example-Footnote-1812939 +Node: Gawk I18N813011 +Node: I18N Summary813649 +Node: Debugger814988 +Node: Debugging816010 +Node: Debugging Concepts816451 +Node: Debugging Terms818307 +Node: Awk Debugging820904 +Node: Sample Debugging Session821796 +Node: Debugger Invocation822316 +Node: Finding The Bug823649 +Node: List of Debugger Commands830131 +Node: Breakpoint Control831463 +Node: Debugger Execution Control835127 +Node: Viewing And Changing Data838487 +Node: Execution Stack841845 +Node: Debugger Info843358 +Node: Miscellaneous Debugger Commands847352 +Node: Readline Support852536 +Node: Limitations853428 +Node: Debugging Summary855702 +Node: Arbitrary Precision Arithmetic856866 +Ref: Arbitrary Precision Arithmetic-Footnote-1858515 +Node: General Arithmetic858663 +Node: Floating Point Issues860383 +Node: String Conversion Precision861264 +Ref: String Conversion Precision-Footnote-1862969 +Node: Unexpected Results863078 +Node: POSIX Floating Point Problems865231 +Ref: POSIX Floating Point Problems-Footnote-1869052 +Node: Integer Programming869090 +Node: Floating-point Programming870901 +Ref: Floating-point Programming-Footnote-1877229 +Ref: Floating-point Programming-Footnote-2877499 +Node: Floating-point Representation877763 +Node: Floating-point Context878928 +Ref: table-ieee-formats879767 +Node: Rounding Mode881151 +Ref: table-rounding-modes881630 +Ref: Rounding Mode-Footnote-1884645 +Node: Gawk and MPFR884824 +Node: Arbitrary Precision Floats886233 +Ref: Arbitrary Precision Floats-Footnote-1888676 +Node: Setting Precision888997 +Ref: table-predefined-precision-strings889681 +Node: Setting Rounding Mode891826 +Ref: table-gawk-rounding-modes892230 +Node: Floating-point Constants893417 +Node: Changing Precision894869 +Ref: Changing Precision-Footnote-1896261 +Node: Exact Arithmetic896435 +Node: Arbitrary Precision Integers899569 +Ref: Arbitrary Precision Integers-Footnote-1902584 +Node: Dynamic Extensions902731 +Node: Extension Intro904240 +Node: Plugin License905505 +Node: Extension Mechanism Outline906190 +Ref: figure-load-extension906614 +Ref: figure-load-new-function908099 +Ref: figure-call-new-function909101 +Node: Extension API Description911085 +Node: Extension API Functions Introduction912535 +Node: General Data Types917400 +Ref: General Data Types-Footnote-1923093 +Node: Requesting Values923392 +Ref: table-value-types-returned924129 +Node: Memory Allocation Functions925087 +Ref: Memory Allocation Functions-Footnote-1927834 +Node: Constructor Functions927930 +Node: Registration Functions929688 +Node: Extension Functions930373 +Node: Exit Callback Functions932675 +Node: Extension Version String933924 +Node: Input Parsers934574 +Node: Output Wrappers944377 +Node: Two-way processors948893 +Node: Printing Messages951097 +Ref: Printing Messages-Footnote-1952174 +Node: Updating `ERRNO'952326 +Node: Accessing Parameters953065 +Node: Symbol Table Access954295 +Node: Symbol table by name954809 +Node: Symbol table by cookie956785 +Ref: Symbol table by cookie-Footnote-1960918 +Node: Cached values960981 +Ref: Cached values-Footnote-1964485 +Node: Array Manipulation964576 +Ref: Array Manipulation-Footnote-1965674 +Node: Array Data Types965713 +Ref: Array Data Types-Footnote-1968416 +Node: Array Functions968508 +Node: Flattening Arrays972382 +Node: Creating Arrays979234 +Node: Extension API Variables983965 +Node: Extension Versioning984601 +Node: Extension API Informational Variables986502 +Node: Extension API Boilerplate987588 +Node: Finding Extensions991392 +Node: Extension Example991952 +Node: Internal File Description992682 +Node: Internal File Ops996773 +Ref: Internal File Ops-Footnote-11008319 +Node: Using Internal File Ops1008459 +Ref: Using Internal File Ops-Footnote-11010806 +Node: Extension Samples1011074 +Node: Extension Sample File Functions1012598 +Node: Extension Sample Fnmatch1020166 +Node: Extension Sample Fork1021647 +Node: Extension Sample Inplace1022860 +Node: Extension Sample Ord1024640 +Node: Extension Sample Readdir1025476 +Ref: table-readdir-file-types1026332 +Node: Extension Sample Revout1027131 +Node: Extension Sample Rev2way1027722 +Node: Extension Sample Read write array1028463 +Node: Extension Sample Readfile1030342 +Node: Extension Sample API Tests1031442 +Node: Extension Sample Time1031967 +Node: gawkextlib1033282 +Node: Extension summary1036095 +Node: Language History1039760 +Node: V7/SVR3.11041403 +Node: SVR41043723 +Node: POSIX1045165 +Node: BTL1046551 +Node: POSIX/GNU1047285 +Node: Feature History1052884 +Node: Common Extensions1065996 +Node: Ranges and Locales1067308 +Ref: Ranges and Locales-Footnote-11071925 +Ref: Ranges and Locales-Footnote-21071952 +Ref: Ranges and Locales-Footnote-31072186 +Node: Contributors1072407 +Node: History summary1077869 +Node: Installation1079238 +Node: Gawk Distribution1080189 +Node: Getting1080673 +Node: Extracting1081499 +Node: Distribution contents1083141 +Node: Unix Installation1088858 +Node: Quick Installation1089475 +Node: Additional Configuration Options1091917 +Node: Configuration Philosophy1093655 +Node: Non-Unix Installation1096006 +Node: PC Installation1096464 +Node: PC Binary Installation1097775 +Node: PC Compiling1099623 +Ref: PC Compiling-Footnote-11102622 +Node: PC Testing1102727 +Node: PC Using1103903 +Node: Cygwin1108061 +Node: MSYS1108870 +Node: VMS Installation1109384 +Node: VMS Compilation1110180 +Ref: VMS Compilation-Footnote-11111402 +Node: VMS Dynamic Extensions1111460 +Node: VMS Installation Details1112833 +Node: VMS Running1115085 +Node: VMS GNV1117919 +Node: VMS Old Gawk1118642 +Node: Bugs1119112 +Node: Other Versions1123116 +Node: Installation summary1129370 +Node: Notes1130425 +Node: Compatibility Mode1131290 +Node: Additions1132072 +Node: Accessing The Source1132997 +Node: Adding Code1134433 +Node: New Ports1140611 +Node: Derived Files1145092 +Ref: Derived Files-Footnote-11150173 +Ref: Derived Files-Footnote-21150207 +Ref: Derived Files-Footnote-31150803 +Node: Future Extensions1150917 +Node: Implementation Limitations1151523 +Node: Extension Design1152771 +Node: Old Extension Problems1153925 +Ref: Old Extension Problems-Footnote-11155442 +Node: Extension New Mechanism Goals1155499 +Ref: Extension New Mechanism Goals-Footnote-11158859 +Node: Extension Other Design Decisions1159048 +Node: Extension Future Growth1161154 +Node: Old Extension Mechanism1161990 +Node: Notes summary1163752 +Node: Basic Concepts1164937 +Node: Basic High Level1165618 +Ref: figure-general-flow1165890 +Ref: figure-process-flow1166489 +Ref: Basic High Level-Footnote-11169718 +Node: Basic Data Typing1169903 +Node: Glossary1173230 +Node: Copying1198382 +Node: GNU Free Documentation License1235938 +Node: Index1261074 End Tag Table diff --git a/doc/gawk.texi b/doc/gawk.texi index 0db8832a..81b3d87d 100644 --- a/doc/gawk.texi +++ b/doc/gawk.texi @@ -6041,6 +6041,7 @@ used with it do not have to be named on the @command{awk} command line * Command line directories:: What happens if you put a directory on the command line. * Input Summary:: Input summary. +* Input Exercises:: Exercises. @end menu @node Records @@ -7475,10 +7476,6 @@ program for processing such data could use the @code{FIELDWIDTHS} feature to simplify reading the data. (Of course, getting @command{gawk} to run on a system with card readers is another story!) -@ignore -Exercise: Write a ballot card reading program -@end ignore - @cindex @command{gawk}, splitting fields and Assigning a value to @code{FS} causes @command{gawk} to use @code{FS} for field splitting again. Use @samp{FS = FS} to make this happen, @@ -7915,11 +7912,6 @@ decommented input, such as searching for matches of a regular expression. (This program has a subtle problem---it does not work if one comment ends and another begins on the same line.) -@ignore -Exercise, -write a program that does handle multiple comments on the line. -@end ignore - This form of the @code{getline} command sets @code{NF}, @code{NR}, @code{FNR}, @code{RT}, and the value of @code{$0}. @@ -8118,7 +8110,6 @@ each one. @xref{Close Files And Pipes}. @end ifnotdocbook @end ifnottex -@c Exercise!! @c This example is unrealistic, since you could just use system Given the input: @@ -8589,6 +8580,26 @@ Directories on the command line are fatal for standard @command{awk}; @end itemize +@node Input Exercises +@section Exercises + +@enumerate +@item +Using the @code{FIELDWIDTHS} variable (@pxref{Constant Size}), +write a program to read election data, where each record represents +one voter's votes. Come up with a way to define which columns are +associated with each ballot item, and print the total votes, +including abstentions, for each item. + +@item +@ref{Plain Getline}, presented a program to remove C-style +comments (@samp{/* @dots{} */}) from the input. That program +does not work if one comment ends on one line and another one +starts later on the same line. +Write a program that does handle multiple comments on the line. + +@end enumerate + @node Printing @chapter Printing Output @@ -8629,6 +8640,7 @@ and discusses the @code{close()} built-in function. descriptors. * Close Files And Pipes:: Closing Input and Output Files and Pipes. * Output Summary:: Output summary. +* Output exercises:: Exercises. @end menu @node Print @@ -8806,16 +8818,6 @@ The following example prints the first and second fields of each input record, separated by a semicolon, with a blank line added after each newline: -@ignore -Exercise, -Rewrite the -@example -awk 'BEGIN @{ print "Month Crates" - print "----- ------" @} - @{ print $1, " ", $2 @}' inventory-shipped -@end example -program by using a new value of @code{OFS}. -@end ignore @example $ @kbd{awk 'BEGIN @{ OFS = ";"; ORS = "\n\n" @}} @@ -9377,12 +9379,6 @@ awk 'BEGIN @{ format = "%-10s %s\n" @{ printf format, $1, $2 @}' mail-list @end example -@c !!! exercise -At this point, it would be a worthwhile exercise to use the -@code{printf} statement to line up the headings and table data for the -@file{inventory-shipped} example that was covered earlier in the @value{SECTION} -on the @code{print} statement -(@pxref{Print}). @c ENDOFRANGE printfs @node Redirection @@ -9771,7 +9767,6 @@ Note the use of quotes around the @value{FN}. Like any other redirection, the value must be a string. It is a common error to omit the quotes, which leads to confusing results. -@c Exercise: What does it do? :-) Finally, using the @code{close()} function on a @value{FN} of the form @code{"/dev/fd/@var{N}"}, for file descriptor numbers @@ -10149,6 +10144,35 @@ communications. @end itemize +@node Output exercises +@section Exercises + +@enumerate +@item +Rewrite the program: + +@example +awk 'BEGIN @{ print "Month Crates" + print "----- ------" @} + @{ print $1, " ", $2 @}' inventory-shipped +@end example + +@noindent +from @ref{Output Separators}, by using a new value of @code{OFS}. + +@item +Use the @code{printf} statement to line up the headings and table data +for the @file{inventory-shipped} example that was covered in @ref{Print}. + +@item +What happens if you forget the double quotes when redirecting +output, as follows: + +@example +BEGIN @{ print "Serious error detected!" > /dev/stderr @} +@end example + +@end enumerate @c ENDOFRANGE prnt @@ -20151,6 +20175,7 @@ comparisons use only lowercase letters. * Group Functions:: Functions for getting group information. * Walking Arrays:: A function to walk arrays of arrays. * Library Functions Summary:: Summary of library functions. +* Library exercises:: Exercises. @end menu @node Library Names @@ -21311,46 +21336,6 @@ the end of the command-line arguments. Note that the test in the condition of the @code{for} loop uses the @samp{<=} operator, not @samp{<}. -As an exercise, you might consider whether this same problem can -be solved without relying on @command{gawk}'s @code{ARGIND} variable. - -As a second exercise, revise this code to handle the case where -an intervening value in @code{ARGV} is a variable assignment. - -@ignore -# zerofile2.awk --- same thing, portably - -BEGIN @{ - ARGIND = Argind = 0 - for (i = 1; i < ARGC; i++) - Fnames[ARGV[i]]++ - -@} -FNR == 1 @{ - while (ARGV[ARGIND] != FILENAME) - ARGIND++ - Seen[FILENAME]++ - if (Seen[FILENAME] == Fnames[FILENAME]) - do - ARGIND++ - while (ARGV[ARGIND] != FILENAME) -@} -ARGIND > Argind + 1 @{ - for (Argind++; Argind < ARGIND; Argind++) - zerofile(ARGV[Argind], Argind) -@} -ARGIND != Argind @{ - Argind = ARGIND -@} -END @{ - if (ARGIND < ARGC - 1) - ARGIND = ARGC - 1 - if (ARGIND > Argind) - for (Argind++; Argind <= ARGIND; Argind++) - zerofile(ARGV[Argind], Argind) -@} -@end ignore - @node Ignoring Assigns @subsection Treating Assignments as @value{FFN}s @@ -22559,21 +22544,6 @@ $ @kbd{gawk -f walk_array.awk} @print{} a[3] = 3 @end example -@c exercise! -Walking an array and processing each element is a general-purpose -operation. You might want to consider generalizing the @code{walk_array()} -function by adding an additional parameter named @code{process}. - -Then, inside the loop, instead of simply printing the array element's -index and value, use the indirect function call syntax -(@pxref{Indirect Calls}) on @code{process}, passing it the index -and the value. - -When calling @code{walk_array()}, you would pass the name of a user-defined -function that expects to receive an index and a value, and then processes -the element. - - @c ENDOFRANGE libfgdata @c ENDOFRANGE flibgdata @c ENDOFRANGE gdatar @@ -22622,6 +22592,72 @@ A simple function to traverse an array of arrays to any depth. @end itemize +@node Library exercises +@section Exercises + +@enumerate +@item +In @ref{Empty Files}, we presented the @file{zerofile.awk} program, +which made use of @command{gawk}'s @code{ARGIND} variable. Can this +problem be solved without relying on @code{ARGIND}? If so, how? + +@ignore +# zerofile2.awk --- same thing, portably + +BEGIN @{ + ARGIND = Argind = 0 + for (i = 1; i < ARGC; i++) + Fnames[ARGV[i]]++ + +@} +FNR == 1 @{ + while (ARGV[ARGIND] != FILENAME) + ARGIND++ + Seen[FILENAME]++ + if (Seen[FILENAME] == Fnames[FILENAME]) + do + ARGIND++ + while (ARGV[ARGIND] != FILENAME) +@} +ARGIND > Argind + 1 @{ + for (Argind++; Argind < ARGIND; Argind++) + zerofile(ARGV[Argind], Argind) +@} +ARGIND != Argind @{ + Argind = ARGIND +@} +END @{ + if (ARGIND < ARGC - 1) + ARGIND = ARGC - 1 + if (ARGIND > Argind) + for (Argind++; Argind <= ARGIND; Argind++) + zerofile(ARGV[Argind], Argind) +@} +@end ignore + +@item +As a related challenge, revise that code to handle the case where +an intervening value in @code{ARGV} is a variable assignment. + +@item +@ref{Walking Arrays}, presented a function that walked a multidimensional +array to print it out. However, walking an array and processing +each element is a general-purpose operation. Generalize the +@code{walk_array()} function by adding an additional parameter named +@code{process}. + +Then, inside the loop, instead of printing the array element's index and +value, use the indirect function call syntax (@pxref{Indirect Calls}) +on @code{process}, passing it the index and the value. + +When calling @code{walk_array()}, you would pass the name of a +user-defined function that expects to receive an index and a value, +and then processes the element. + +Test your new version by printing the array; you should end up with +output identical to that of the original version. + +@end enumerate @c ENDOFRANGE flib @c ENDOFRANGE fudlib diff --git a/doc/gawktexi.in b/doc/gawktexi.in index d37b51d7..34f216a0 100644 --- a/doc/gawktexi.in +++ b/doc/gawktexi.in @@ -5842,6 +5842,7 @@ used with it do not have to be named on the @command{awk} command line * Command line directories:: What happens if you put a directory on the command line. * Input Summary:: Input summary. +* Input Exercises:: Exercises. @end menu @node Records @@ -7094,10 +7095,6 @@ program for processing such data could use the @code{FIELDWIDTHS} feature to simplify reading the data. (Of course, getting @command{gawk} to run on a system with card readers is another story!) -@ignore -Exercise: Write a ballot card reading program -@end ignore - @cindex @command{gawk}, splitting fields and Assigning a value to @code{FS} causes @command{gawk} to use @code{FS} for field splitting again. Use @samp{FS = FS} to make this happen, @@ -7534,11 +7531,6 @@ decommented input, such as searching for matches of a regular expression. (This program has a subtle problem---it does not work if one comment ends and another begins on the same line.) -@ignore -Exercise, -write a program that does handle multiple comments on the line. -@end ignore - This form of the @code{getline} command sets @code{NF}, @code{NR}, @code{FNR}, @code{RT}, and the value of @code{$0}. @@ -7737,7 +7729,6 @@ each one. @xref{Close Files And Pipes}. @end ifnotdocbook @end ifnottex -@c Exercise!! @c This example is unrealistic, since you could just use system Given the input: @@ -8208,6 +8199,26 @@ Directories on the command line are fatal for standard @command{awk}; @end itemize +@node Input Exercises +@section Exercises + +@enumerate +@item +Using the @code{FIELDWIDTHS} variable (@pxref{Constant Size}), +write a program to read election data, where each record represents +one voter's votes. Come up with a way to define which columns are +associated with each ballot item, and print the total votes, +including abstentions, for each item. + +@item +@ref{Plain Getline}, presented a program to remove C-style +comments (@samp{/* @dots{} */}) from the input. That program +does not work if one comment ends on one line and another one +starts later on the same line. +Write a program that does handle multiple comments on the line. + +@end enumerate + @node Printing @chapter Printing Output @@ -8248,6 +8259,7 @@ and discusses the @code{close()} built-in function. descriptors. * Close Files And Pipes:: Closing Input and Output Files and Pipes. * Output Summary:: Output summary. +* Output exercises:: Exercises. @end menu @node Print @@ -8425,16 +8437,6 @@ The following example prints the first and second fields of each input record, separated by a semicolon, with a blank line added after each newline: -@ignore -Exercise, -Rewrite the -@example -awk 'BEGIN @{ print "Month Crates" - print "----- ------" @} - @{ print $1, " ", $2 @}' inventory-shipped -@end example -program by using a new value of @code{OFS}. -@end ignore @example $ @kbd{awk 'BEGIN @{ OFS = ";"; ORS = "\n\n" @}} @@ -8996,12 +8998,6 @@ awk 'BEGIN @{ format = "%-10s %s\n" @{ printf format, $1, $2 @}' mail-list @end example -@c !!! exercise -At this point, it would be a worthwhile exercise to use the -@code{printf} statement to line up the headings and table data for the -@file{inventory-shipped} example that was covered earlier in the @value{SECTION} -on the @code{print} statement -(@pxref{Print}). @c ENDOFRANGE printfs @node Redirection @@ -9352,7 +9348,6 @@ Note the use of quotes around the @value{FN}. Like any other redirection, the value must be a string. It is a common error to omit the quotes, which leads to confusing results. -@c Exercise: What does it do? :-) Finally, using the @code{close()} function on a @value{FN} of the form @code{"/dev/fd/@var{N}"}, for file descriptor numbers @@ -9668,6 +9663,35 @@ communications. @end itemize +@node Output exercises +@section Exercises + +@enumerate +@item +Rewrite the program: + +@example +awk 'BEGIN @{ print "Month Crates" + print "----- ------" @} + @{ print $1, " ", $2 @}' inventory-shipped +@end example + +@noindent +from @ref{Output Separators}, by using a new value of @code{OFS}. + +@item +Use the @code{printf} statement to line up the headings and table data +for the @file{inventory-shipped} example that was covered in @ref{Print}. + +@item +What happens if you forget the double quotes when redirecting +output, as follows: + +@example +BEGIN @{ print "Serious error detected!" > /dev/stderr @} +@end example + +@end enumerate @c ENDOFRANGE prnt @@ -19324,6 +19348,7 @@ comparisons use only lowercase letters. * Group Functions:: Functions for getting group information. * Walking Arrays:: A function to walk arrays of arrays. * Library Functions Summary:: Summary of library functions. +* Library exercises:: Exercises. @end menu @node Library Names @@ -20455,46 +20480,6 @@ the end of the command-line arguments. Note that the test in the condition of the @code{for} loop uses the @samp{<=} operator, not @samp{<}. -As an exercise, you might consider whether this same problem can -be solved without relying on @command{gawk}'s @code{ARGIND} variable. - -As a second exercise, revise this code to handle the case where -an intervening value in @code{ARGV} is a variable assignment. - -@ignore -# zerofile2.awk --- same thing, portably - -BEGIN @{ - ARGIND = Argind = 0 - for (i = 1; i < ARGC; i++) - Fnames[ARGV[i]]++ - -@} -FNR == 1 @{ - while (ARGV[ARGIND] != FILENAME) - ARGIND++ - Seen[FILENAME]++ - if (Seen[FILENAME] == Fnames[FILENAME]) - do - ARGIND++ - while (ARGV[ARGIND] != FILENAME) -@} -ARGIND > Argind + 1 @{ - for (Argind++; Argind < ARGIND; Argind++) - zerofile(ARGV[Argind], Argind) -@} -ARGIND != Argind @{ - Argind = ARGIND -@} -END @{ - if (ARGIND < ARGC - 1) - ARGIND = ARGC - 1 - if (ARGIND > Argind) - for (Argind++; Argind <= ARGIND; Argind++) - zerofile(ARGV[Argind], Argind) -@} -@end ignore - @node Ignoring Assigns @subsection Treating Assignments as @value{FFN}s @@ -21703,21 +21688,6 @@ $ @kbd{gawk -f walk_array.awk} @print{} a[3] = 3 @end example -@c exercise! -Walking an array and processing each element is a general-purpose -operation. You might want to consider generalizing the @code{walk_array()} -function by adding an additional parameter named @code{process}. - -Then, inside the loop, instead of simply printing the array element's -index and value, use the indirect function call syntax -(@pxref{Indirect Calls}) on @code{process}, passing it the index -and the value. - -When calling @code{walk_array()}, you would pass the name of a user-defined -function that expects to receive an index and a value, and then processes -the element. - - @c ENDOFRANGE libfgdata @c ENDOFRANGE flibgdata @c ENDOFRANGE gdatar @@ -21766,6 +21736,72 @@ A simple function to traverse an array of arrays to any depth. @end itemize +@node Library exercises +@section Exercises + +@enumerate +@item +In @ref{Empty Files}, we presented the @file{zerofile.awk} program, +which made use of @command{gawk}'s @code{ARGIND} variable. Can this +problem be solved without relying on @code{ARGIND}? If so, how? + +@ignore +# zerofile2.awk --- same thing, portably + +BEGIN @{ + ARGIND = Argind = 0 + for (i = 1; i < ARGC; i++) + Fnames[ARGV[i]]++ + +@} +FNR == 1 @{ + while (ARGV[ARGIND] != FILENAME) + ARGIND++ + Seen[FILENAME]++ + if (Seen[FILENAME] == Fnames[FILENAME]) + do + ARGIND++ + while (ARGV[ARGIND] != FILENAME) +@} +ARGIND > Argind + 1 @{ + for (Argind++; Argind < ARGIND; Argind++) + zerofile(ARGV[Argind], Argind) +@} +ARGIND != Argind @{ + Argind = ARGIND +@} +END @{ + if (ARGIND < ARGC - 1) + ARGIND = ARGC - 1 + if (ARGIND > Argind) + for (Argind++; Argind <= ARGIND; Argind++) + zerofile(ARGV[Argind], Argind) +@} +@end ignore + +@item +As a related challenge, revise that code to handle the case where +an intervening value in @code{ARGV} is a variable assignment. + +@item +@ref{Walking Arrays}, presented a function that walked a multidimensional +array to print it out. However, walking an array and processing +each element is a general-purpose operation. Generalize the +@code{walk_array()} function by adding an additional parameter named +@code{process}. + +Then, inside the loop, instead of printing the array element's index and +value, use the indirect function call syntax (@pxref{Indirect Calls}) +on @code{process}, passing it the index and the value. + +When calling @code{walk_array()}, you would pass the name of a +user-defined function that expects to receive an index and a value, +and then processes the element. + +Test your new version by printing the array; you should end up with +output identical to that of the original version. + +@end enumerate @c ENDOFRANGE flib @c ENDOFRANGE fudlib |