Algorithms and gawk.

c-declarations
Joshua Potter 2024-02-09 08:08:41 -07:00
parent 00f23d5f94
commit c3d266882f
11 changed files with 557 additions and 169 deletions

View File

@ -77,7 +77,7 @@
"selection-sort.gif"
],
"File Hashes": {
"algorithms/index.md": "1583c07edea4736db27c38fe2b6c4c31",
"algorithms/index.md": "c9cf76a46508d3a1b3aeb54e7fb1f67d",
"algorithms/sorting/index.md": "6fada1f3d5d3af64687719eb465a5b97",
"algorithms/sorting/insertion-sort.md": "a531d611f4e2908fc4153b1b92077661",
"bash/index.md": "3b5296277f095acdf16655adcdf524af",
@ -87,8 +87,8 @@
"bash/shebang.md": "9006547710f9a079a3666169fbeda7aa",
"c/escape-sequences.md": "0d6219ebb51f6f21e026de67603e25b8",
"c/index.md": "aa8a34c62e7bc284ff589e28609222dc",
"gawk/index.md": "d23f281fcfef7169a1ab68b974359c5b",
"gawk/variables.md": "4482c297e7f4f5987f42f1926a880ca7",
"gawk/index.md": "b1e9209e0ab1f7837b004f51d72200c2",
"gawk/variables.md": "ba568df484571c8e0fa89d0005c827c0",
"index.md": "e48e895feeed7046425bb2ee15419770",
"journal/2024-01-31.md": "7c7fbfccabc316f9e676826bf8dfe970",
"journal/2024-02-01.md": "3aa232387d2dc662384976fd116888eb",
@ -113,15 +113,18 @@
"_journal/2024-02-02.md": "a3b222daee8a50bce4cbac699efc7180",
"_journal/2024-02-01.md": "3aa232387d2dc662384976fd116888eb",
"_journal/2024-01-31.md": "7c7fbfccabc316f9e676826bf8dfe970",
"logic/equiv-trans.md": "660a2a08ddcf47c05af3f8704feb5931",
"logic/equiv-trans.md": "e8f25b1425d4dfc8c5dc01a3ff71d08f",
"_journal/2024-02-07.md": "8d81cd56a3b33883a7706d32e77b5889",
"algorithms/loop-invariants.md": "cbefc346842c21a6cce5c5edce451eb2",
"algorithms/loop-invariant.md": "d883dfc997ee28a7a1e24b995377792b",
"algorithms/running-time.md": "5efc0791097d2c996f931c9046c95f65",
"algorithms/order-growth.md": "bf43ad8c16037baf1e865839f5e46704",
"_journal/2024-02-08.md": "26ba491937c92e55d3a43f8800677dcb",
"algorithms/order-growth.md": "7f87c7ab55979d66fcda8776a35e3682",
"_journal/2024-02-08.md": "19092bdfe378f31e2774f20d6afbfbac",
"algorithms/sorting/selection-sort.md": "f31cf7e706504b9be48bed7af6e37074",
"algorithms/index 1.md": "6fada1f3d5d3af64687719eb465a5b97"
"algorithms/index 1.md": "6fada1f3d5d3af64687719eb465a5b97",
"binary/hexadecimal.md": "a9633bbc9b53cc8c16ce6e56022f62e0",
"binary/index.md": "d41d8cd98f00b204e9800998ecf8427e",
"_journal/2024-02-09.md": "60e953482e5d649a669d270a192ef7e5"
},
"fields_dict": {
"Basic": [

View File

@ -4,10 +4,13 @@ title: "2024-02-08"
- [x] Anki Flashcards
- [x] KoL
- [ ] Sheet Music (10 min.)
- [x] Sheet Music (10 min.)
- [ ] OGS (1 Life & Death Problem)
- [ ] Korean (Read 1 Story)
- [x] Korean (Read 1 Story)
- [ ] Interview Prep (1 Practice Problem)
- [ ] Log Work Hours (Max 3 hours)
- [x] Log Work Hours (Max 3 hours)
* Add notes on selection sort.
* Add notes on insertion/selection sort.
* Continue working through "GAWK: Effective AWK Programming". Focused on fields (sections 4.2 and 4.3).
* Call with Kevin on columnar databases and streaming/message queue platforms.
* Also opened "Computer Systems: A Programmer's Perspective" to work through in parallel.

View File

@ -0,0 +1,13 @@
---
title: "2024-02-09"
---
- [x] Anki Flashcards
- [x] KoL
- [x] Sheet Music (10 min.)
- [ ] OGS (1 Life & Death Problem)
- [ ] Korean (Read 1 Story)
- [ ] Interview Prep (1 Practice Problem)
- [ ] Log Work Hours (Max 3 hours)
* Logged information about prominent predefined `awk` variables.

View File

@ -1,99 +0,0 @@
---
title: Sorting
TARGET DECK: Obsidian::STEM
FILE TAGS: algorithm::sorting
tags:
- algorithm
- sorting
---
## Overview
Let $n \geq 0$. The **sorting problem** refers to permuting **records** $a_1, a_2, \ldots, a_n$ into a new sequence $\langle a_1', a_2', \ldots, a_n' \rangle$ such that $key(a_1') \leq key(a_2') \leq \cdots \leq key(a_n')$.
%%ANKI
Cloze
In the sorting problem, a "{record}" refers to {the entries being sorted}.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1706981319280-->
END%%
%%ANKI
Cloze
In the sorting problem, a "{key}" refers to {the value records are sorted by}.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1706981319310-->
END%%
%%ANKI
Cloze
In the sorting problem, "{satellite data}" refers to {the non-key values of records}.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1706981319317-->
END%%
%%ANKI
Basic
What term does Cormen et al. use to describe the record value used for sorting?
Back: Keys.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1706981319324-->
END%%
%%ANKI
Basic
What makes a sorting algorithm stable?
Back: "Equal" values are ordered the same in the output as they are in the input.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1706925787139-->
END%%
%%ANKI
Basic
What is an in place sorting algorithm?
Back: One in which only a constant number of input values are ever stored outside the array.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1706925787146-->
END%%
## Structural Comparison
The #Elixir documentation makes a point that there exist two types of comparisons between data types.[^structural] The first is **structural** in which comparisons are made on the underlying data structures used to describe the data types. The second is **semantic** which focuses on making the comparison with respect to what the data types represent.
```elixir
iex> 1 < :atom # structural
true
iex> Date.compare(~D[2017-03-31], ~D[2017-04-01]) # semantic
:lt
```
%%ANKI
Basic
What are the two types of comparisons made between data types?
Back: Structural and semantic.
Reference: “Kernel — Elixir v1.16.1,” accessed February 2, 2024, [https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison](https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison).
<!--ID: 1706913303147-->
END%%
%%ANKI
Basic
What is structural comparison of two data types?
Back: Comparison of the underlying data structures making up data types.
Reference: “Kernel — Elixir v1.16.1,” accessed February 2, 2024, [https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison](https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison).
<!--ID: 1706913303155-->
END%%
%%ANKI
Basic
What is semantic comparison of two data types?
Back: Comparison made with respect to what the data types represent.
Reference: “Kernel — Elixir v1.16.1,” accessed February 2, 2024, [https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison](https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison).
<!--ID: 1706913303160-->
END%%
## References
* Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
* “Kernel — Elixir v1.16.1,” accessed February 2, 2024, [https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison](https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison).
[^structural]: [Structural Comparison](https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison)

View File

@ -1,3 +1,99 @@
---
title: Algorithms
title: Sorting
TARGET DECK: Obsidian::STEM
FILE TAGS: algorithm::sorting
tags:
- algorithm
- sorting
---
## Overview
Let $n \geq 0$. The **sorting problem** refers to permuting **records** $a_1, a_2, \ldots, a_n$ into a new sequence $\langle a_1', a_2', \ldots, a_n' \rangle$ such that $key(a_1') \leq key(a_2') \leq \cdots \leq key(a_n')$.
%%ANKI
Cloze
In the sorting problem, a "{record}" refers to {the entries being sorted}.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1706981319280-->
END%%
%%ANKI
Cloze
In the sorting problem, a "{key}" refers to {the value records are sorted by}.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1706981319310-->
END%%
%%ANKI
Cloze
In the sorting problem, "{satellite data}" refers to {the non-key values of records}.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1706981319317-->
END%%
%%ANKI
Basic
What term does Cormen et al. use to describe the record value used for sorting?
Back: Keys.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1706981319324-->
END%%
%%ANKI
Basic
What makes a sorting algorithm stable?
Back: "Equal" values are ordered the same in the output as they are in the input.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1706925787139-->
END%%
%%ANKI
Basic
What is an in place sorting algorithm?
Back: One in which only a constant number of input values are ever stored outside the array.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!--ID: 1706925787146-->
END%%
## Structural Comparison
The #elixir documentation makes a point that there exist two types of comparisons between data types.[^structural] The first is **structural** in which comparisons are made on the underlying data structures used to describe the data types. The second is **semantic** which focuses on making the comparison with respect to what the data types represent.
```elixir
iex> 1 < :atom # structural
true
iex> Date.compare(~D[2017-03-31], ~D[2017-04-01]) # semantic
:lt
```
%%ANKI
Basic
What are the two types of comparisons made between data types?
Back: Structural and semantic.
Reference: “Kernel — Elixir v1.16.1,” accessed February 2, 2024, [https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison](https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison).
<!--ID: 1706913303147-->
END%%
%%ANKI
Basic
What is structural comparison of two data types?
Back: Comparison of the underlying data structures making up data types.
Reference: “Kernel — Elixir v1.16.1,” accessed February 2, 2024, [https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison](https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison).
<!--ID: 1706913303155-->
END%%
%%ANKI
Basic
What is semantic comparison of two data types?
Back: Comparison made with respect to what the data types represent.
Reference: “Kernel — Elixir v1.16.1,” accessed February 2, 2024, [https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison](https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison).
<!--ID: 1706913303160-->
END%%
## References
* Thomas H. Cormen et al., *Introduction to Algorithms*, 3rd ed (Cambridge, Mass: MIT Press, 2009).
* “Kernel — Elixir v1.16.1,” accessed February 2, 2024, [https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison](https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison).
[^structural]: [Structural Comparison](https://hexdocs.pm/elixir/1.16/Kernel.html#module-structural-comparison)

View File

@ -9,7 +9,7 @@ tags:
## Overview
The **running time** of an algorithm is usually considered as a function of its **input size**. How input size is measured depends on the problem at hand. For instance, [[index 1|sorting]] algorithms have an input size corresponding to the number of elements to sort.
The **running time** of an algorithm is usually considered as a function of its **input size**. How input size is measured depends on the problem at hand. For instance, [[algorithms/index|sorting]] algorithms have an input size corresponding to the number of elements to sort.
%%ANKI
Basic

227
notes/binary/hexadecimal.md Normal file
View File

@ -0,0 +1,227 @@
---
title: Hexadecimal
TARGET DECK: Obsidian::STEM
FILE TAGS: binary::hex
tags:
- binary
- hexadecimal
---
## Overview
Hexadecimal encoding refers to the 16-base representation of binary numbers. Distinguish potentially ambiguous values like $32$ with the base as a subscript, e.g. $32_{10}$ vs $32_{16}$.
%%ANKI
Cloze
A byte consists of {8} bits.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641557-->
END%%
%%ANKI
Cloze
A byte consists of {2} nibbles.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641560-->
END%%
%%ANKI
Cloze
A nibble consists of {4} bits.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641562-->
END%%
%%ANKI
Cloze
A byte consists of {2} hexadecimal digits.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641563-->
END%%
%%ANKI
A nibble consists of {1} hexadecimal digits.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
END%%
%%ANKI
Basic
Hexadecimal digits are represented by what characters?
Back: `a` to `f`, `A` to `F`, and `0` to `9`.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641565-->
END%%
%%ANKI
Basic
How does C denote a hexadecimal numeric constant?
Back: With `0x` or `0X`.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641567-->
END%%
%%ANKI
Basic
What is the decimal equivalent of hex `A`, `C`, and `F`?
Back: `10`, `12`, and `15` respectively.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641568-->
END%%
%%ANKI
Basic
What is the hexadecimal equivalent of decimal `11`, `12`, and `14`?
Back: `B`, `C`, and `E` respectively.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641570-->
END%%
%%ANKI
Basic
*When* should padding be introduced on converting binary to hexadecimal?
Back: When the number of bits is not a multiple of `4`.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641571-->
END%%
%%ANKI
Basic
*Where* is padding introduced on binary to hexadecimal conversion?
Back: To the left of the binary sequence.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641573-->
END%%
%%ANKI
Basic
How is $2^n$ written in binary?
Back: As `1` followed by $n$ zeros.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641574-->
END%%
%%ANKI
Basic
How is $2^n$ written using bitwise shift operators?
Back: `1 << n`
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
Tag: c
<!--ID: 1707432641576-->
END%%
%%ANKI
Basic
What decimal value does `1 << n` translate to?
Back: $2^n$
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
Tag: c
<!--ID: 1707432641577-->
END%%
%%ANKI
Basic
What are the possible hex values the first digit of $2^n$ can take on?
Back: `1`, `2`, `4`, and `8`.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641579-->
END%%
%%ANKI
Basic
What are the possible values the first nibble of $2^n$ can take on?
Back: `0001`, `0010`, `0100`, and `1000`.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641580-->
END%%
%%ANKI
Basic
How is $j$ interpreted in the hex representation of $2^{i + 4j}$?
As the number of `0`s in the encoding.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641582-->
END%%
%%ANKI
Basic
How is the $0$ in $2^{0 + 4j}$ translated to hex?
Back: As hex digit `1`.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641583-->
END%%
%%ANKI
Basic
How is the $1$ in $2^{1 + 4j}$ translated to hex?
Back: As hex digit `2`.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641585-->
END%%
%%ANKI
Basic
How is the $2$ (power) in $2^{2 + 4j}$ translated to hex?
Back: As hex digit `4`.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641586-->
END%%
%%ANKI
Basic
How is the $3$ in $2^{3 + 4j}$ translated to hex?
Back: As hex digit `8`.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641587-->
END%%
%%ANKI
Basic
How is $n$ in $2^n$ factored to quickly write the decimal value's hex representation?
Back: $n = i + 4j$ where $0 \leq i \leq 3$.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641589-->
END%%
%%ANKI
Basic
What process is used to convert from e.g. decimal to another base?
Back: Divide repeatedly by the base. Maintain remainders right to left.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641591-->
END%%
%%ANKI
Basic
Why does converting from e.g. decimal to another base involve repeated division?
Back: The position of a digit corresponds to the base raised to the position.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641592-->
END%%
%%ANKI
Basic
How is the *remainder* of e.g. `158 / 16` managed in decimal to hex conversion?
Back: As the next least significant bit of our conversion.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641594-->
END%%
%%ANKI
Basic
How is the *quotient* of e.g. `158 / 16` managed in decimal to hex conversion?
Back: As the next value to divide by `16`.
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641595-->
END%%
%%ANKI
Basic
How is e.g. `0xAC32` expressed as a sum of decimal values?
Back: $(16^3 \times 10) + (16^2 \times 12) + (16^1 \times 3) + (16^0 \times 2)$
Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
<!--ID: 1707432641596-->
END%%
## Reference
* Reference: Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.

0
notes/binary/index.md Normal file
View File

View File

@ -137,59 +137,6 @@ Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 202
<!--ID: 1707310981380-->
END%%
%%ANKI
Cloze
The {`RS`} variable is used to change the {record separator}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981382-->
END%%
%%ANKI
Cloze
If `RS` is a string with {more than one character}, it is treated as a {regexp}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981384-->
END%%
%%ANKI
Cloze
The {`RT`} variable matches the {input characters that matched `RS`}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981386-->
END%%
%%ANKI
Basic
Barring the final record, when is `RT` always equal to `RS`?
Back: When `RS` is a string containing a single character.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981388-->
END%%
%%ANKI
Basic
What value of `RS` may `gawk` not process correctly?
Back: A regexp with optional trailing part, e.g. `AB(XYZ)?`.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981390-->
END%%
%%ANKI
Basic
What implementation detail inspires avoiding `RS = "\0"`?
Back: Most `awk` implementations store strings internally as C-style strings?
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981392-->
END%%
%%ANKI
Basic
What equivalent assignment do most `awk` implementations interpret `RS = "\0"` as?
Back: `RS = ""`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981394-->
END%%
%%ANKI
Basic
In `awk`, what does a "field" refer to?
@ -198,6 +145,30 @@ Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 202
<!--ID: 1707310981395-->
END%%
%%ANKI
Basic
By default, fields are separated by what?
Back: A sequence of spaces, tabs, and newlines.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707405259928-->
END%%
%%ANKI
Basic
How does `awk` define whitespace?
Back: As *only* spaces, tabs, and newlines.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707405259932-->
END%%
%%ANKI
Basic
How are fields referenced?
Back: Via the `$$` sign.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707405259933-->
END%%
%%ANKI
Basic
What is `$0` a placeholder for?
@ -214,6 +185,55 @@ Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 202
<!--ID: 1706823790233-->
END%%
%%ANKI
Basic
How can you remove trailing fields of `$0`?
Back: Assign a smaller value to `$NF`.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707491299858-->
END%%
%%ANKI
Basic
How do you typically recompute the value of `$0`?
Back: `$1 = $1`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707491299860-->
END%%
%%ANKI
Basic
*Why* does the following not output what you want?
```bash
$ ls -l | awk '{ OFS=":"; print $0 }'
```
Back: `$0` wasn't recomputed so it maintains the previous `OFS` value.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707491299862-->
END%%
%%ANKI
Basic
How can you update the following to behave correctly?
```bash
$ ls -l | awk '{ OFS=":"; print $0 }'
```
Back:
```bash
$ ls -l | awk '{ OFS=":"; $1 = $1; print $0 }'
```
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707491299864-->
END%%
%%ANKI
Basic
When is the behavior of the field reference operator (i.e. `$$`) undefined?
Back: When given a negative number.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707405259938-->
END%%
%%ANKI
Basic
How is the `BEGIN` pattern interpreted?

View File

@ -6,7 +6,7 @@ tags:
- gawk
---
## Variables
## Overview
Variables are defined like `var=val`. They can be specified in two different places:
@ -48,6 +48,123 @@ Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 202
<!--ID: 1706973587236-->
END%%
## Predefined Variables
There exists a number of useful predefined variables:
* `NR` (**N**umber of **R**ecords)
* The 1-indexed number of records so far read.
* The count includes the current record.
* `FNR` (**F**ile **N**umber of **R**ecords)
* The 1-indexed number of records so far read from the current file.
* The count includes the current record.
%%ANKI
Cloze
The {`NR`} variable specifies the {number of read input records}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707405259922-->
END%%
%%ANKI
Cloze
The {`FNR`} variable specifies the {number of read input records for the current file}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707405259925-->
END%%
* `RS` (**R**ecord **S**eparator)
* The separator used to distinguish records from one another.
* `RT` (**R**ecord **T**ext)
* The matching separator used to distinguish the currently read record.
%%ANKI
Cloze
The {`RS`} variable is used to change the {record separator}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981382-->
END%%
%%ANKI
Cloze
If `RS` is a string with {more than one character}, it is treated as a {regexp}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981384-->
END%%
%%ANKI
Cloze
The {`RT`} variable matches the {input characters that matched `RS`}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981386-->
END%%
%%ANKI
Basic
Barring the final record, when is `RT` always equal to `RS`?
Back: When `RS` is a string containing a single character.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981388-->
END%%
%%ANKI
Basic
What value of `RS` may `gawk` not process correctly?
Back: A regexp with optional trailing part, e.g. `AB(XYZ)?`.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981390-->
END%%
%%ANKI
Basic
What implementation detail inspires avoiding `RS = "\0"`?
Back: Most `awk` implementations store strings internally as C-style strings?
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981392-->
END%%
%%ANKI
Basic
What equivalent assignment do most `awk` implementations interpret `RS = "\0"` as?
Back: `RS = ""`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707310981394-->
END%%
* `NF` (**N**umber of **F**ields)
* The 1-indexed number of fields found in the current record.
%%ANKI
Basic
What is the arithmetical value of `${NF + 1}`?
Back: `0`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707405259936-->
END%%
%%ANKI
Basic
What is the printed value of `${NF + 1}`?
Back: `""`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707405259935-->
END%%
%%ANKI
Basic
What value is `${NF + 1}` given when we run `${NF + 2} = "test"`?
Back: `""`
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707491299854-->
END%%
%%ANKI
Cloze
The {`NF`} variable specifies the {number of fields in the current record}.
Reference: Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)
<!--ID: 1707405259930-->
END%%
## References
* Robbins, Arnold D. “GAWK: Effective AWK Programming,” October 2023. [https://www.gnu.org/software/gawk/manual/gawk.pdf](https://www.gnu.org/software/gawk/manual/gawk.pdf)

View File

@ -21,10 +21,10 @@ END%%
%%ANKI
Basic
What are the constant propositions?
Back: $T$ and $F$
What are constant propositions?
Back: Propositions that contain only constants as operands.
Reference: Gries, David. *The Science of Programming*. Texts and Monographs in Computer Science. New York: Springer-Verlag, 1981.
<!--ID: 1706994861289-->
<!--ID: 1707422675517-->
END%%
%%ANKI
@ -522,6 +522,14 @@ Reference: Gries, David. *The Science of Programming*. Texts and Monographs in
<!--ID: 1707253246450-->
END%%
%%ANKI
Basic
Which of the two inference rules that make up the equivalence-transformation formal system is redundant?
Back: Transitivity.
Reference: Gries, David. *The Science of Programming*. Texts and Monographs in Computer Science. New York: Springer-Verlag, 1981.
<!--ID: 1707432641598-->
END%%
%%ANKI
Basic
What does the rule of substitution say in the system of evaluation?