#!/usr/local/bin/kermit + # (change the above to the path of C-Kermit on your computer if necessary) # # Frank da Cruz # The Kermit Project, Bronx NY # 12 July 2023 # # Reads a table of monthly income and rent for a series of years (in this # case for a USA retiree) and shows the increases in each in dollars and as # percents for each year. The table has four columns: year, monthly # annuity income, monthly Social Security income, and monthly rent. # # Illustrates how to embed tabular data in the script itself rather than # reading it from an external file; how to break a table row up into its # elements; the use of multiple associative arrays at the same time; how to # address associative array elements symbolically; and the use of # S-expressions when floating-point results are needed. # # Works in C-Kermit 10.0 and 9.0; does not work in earlier versions # (at least on NetBSD 9.3) because of library mismatches. # # YEAR = Year # ANNU = Monthly annuity (pension) income # SSEC = Monthly Social Security income # INCM = Total monthly income # RENT = Total monthly rent # DIFF = Income minus rent # INCI = Increase in income since previous year # INCR = Increase in rent since previous year # PCTI = Annual increase in income (multiplier) # PCTR = Annual annual increase in rent (multiplier) # RAPOI = Rent as percentage of total net income # # Data for the script as an "inline table". Of course this could also be # in an external file accessed by fopen, fread /line, fclose, but this # shows how to embed a "paragraph" of text within the script itself without # lot of special notation of commas and backslashes (it's not obvious). The # columns are year, monthly pension income, monthly Social Security income, # and monthly rent, all but the year in dollars; the rows are in ascending # consecutive chronological order. # define table { # (ficticious but realistic numbers) 2012 1877 1219 1650 2013 1914 1038 1650 2014 2013 1263 1716 2015 2115 1314 1793 2016 2189 1314 1793 2017 2265 1315 1847 2018 2310 1343 1893 2019 2389 1411 1973 2020 2437 1441 2002 2021 2521 1471 2002 2022 2574 1607 2052 2023 2806 1798 2052 2024 2806 1798 2114 } void \fsplit(\m(table),&t,{,-},ALL) # Convert table into an array of lines # Make an associative array for each year for i 1 \fdimension(&t) 1 { # For each array element (line)... .line := \&t[i] if not defined line continue # Skip any blank lines void \fsplit(\m(line),&a) # Extract data items into array '&a' .year := \&a[1] if == i 1 .minyear := \m(year) # Remember first year # Make an associative array for each year named after the year _asg \m(year) \&a[1] # Calendar year of this entry _asg \m(year) \&a[2] # Monthly annuity for this year _asg \m(year) \&a[3] # Social Security for this year _asg \m(year) \&a[4] # Monthly rent for this year } .maxyear := \m(year) # Remember last year # Print result table heading echo "YEAR ANNU SSEC INCM RENT DIFF INCI INCR PCTI PCTR RAPOI" # Loop through years for \%i \m(minyear) \m(maxyear) 1 { # Create each line of the report .\%j ::= \%i - 1 # set \%j to the previous year xecho "\%i " # This year xecho "\m(\%i) " # Annuity xecho "\m(\%i) " # Social Security .incm ::= \m(\%i) + \m(\%i) # This month's total income xecho "\m(incm) " xecho "\m(\%i) " # Rent .diff ::= \m(incm) - \m(\%i) # What's left after paying rent xecho "\m(diff) " if >= \%j minyear { .inci ::= \m(\%i) - \m(\%j) # Annual increase in annuity xecho \flpad(\m(inci),4)\32 .incr ::= \m(\%i) - \m(\%j) # Annual increase in rent xecho \flpad(\m(incr),5) # The following calculations require floating-point numbers, # which are handled in C-Kermit by its built-in LISP subsystem. # First, year-over-year percentage increase in total income: (setq tmp (+ \m(\%j) + \m(\%j))) (setq pcti (round (* (- (/ \m(incm) \m(tmp)) 1) 100) 2)) xecho \flpad(\m(pcti),6) # Year-over-year percentage increase in rent: (setq pctr (round (* (- (/ \m(\%i) \m(\%j)) 1) 100) 2)) xecho \flpad(\m(pctr),5) # Rent as percantage of income: (setq rapoi (round (/ (* 100 \m(\%i)) \m(incm)) 2)) xecho \flpad(\m(rapoi),6) } else { # For the first year we have no previous-year info so no ratios xecho " N/A N/A N/A N/A N/A" } echo # Terminate the line } exit ; Local Variables: ; comment-column:40 ; comment-start:"# " ; End: