Decision tables: Difference between revisions

m
(→‎{{header|REXX}}: added/changed whitespace and comments, elided deadcode, added highlighting to the output, added a template for the output section.)
m (→‎{{header|Wren}}: Minor tidy)
 
(19 intermediate revisions by 13 users not shown)
Line 11:
Use this [[wp:Decision_table#Example|example]] of Printer Troubleshooting given in the Wikipedia article.
<br><br>
 
=={{header|11l}}==
{{trans|Kotlin}}
 
<syntaxhighlight lang="11l">V conditions = [
(‘Printer prints’ , ‘NNNNYYYY’),
(‘A red light is flashing’ , ‘YYNNYYNN’),
(‘Printer is recognized by computer’, ‘NYNYNYNY’)]
 
V actions = [
(‘Check the power cable’ , ‘NNYNNNNN’),
(‘Check the printer-computer cable’ , ‘YNYNNNNN’),
(‘Ensure printer software is installed’, ‘YNYNYNYN’),
(‘Check/replace ink’ , ‘YYNNNYNN’),
(‘Check for paper jam’ , ‘NYNYNNNN’)]
 
V nr = conditions[0][1].len
V np = 7
print(‘Please answer the following questions with a y or n:’)
 
[Bool] answers
L(condition) conditions
V inp = ‘’
L
inp = input(‘ ’condition[0]‘ ? ’).uppercase()
I inp C (‘Y’, ‘N’)
L.break
answers.append(inp == ‘Y’)
 
print("\nRecommended action(s):")
L(r) 0 .< nr
L(c) 0 .< conditions.len
V yn = I answers[c] {‘Y’} E ‘N’
I conditions[c][1][r] != yn
L.break
L.was_no_break
I r == np
print(‘ None (no problem detected)’)
E
L(action) actions
I action[1][r] == ‘Y’
print(‘ ’action[0])
L.break</syntaxhighlight>
 
{{out}}
<pre>
Please answer the following questions with a y or n:
Printer prints ? n
A red light is flashing ? n
Printer is recognized by computer ? n
 
Recommended action(s):
Check the power cable
Check the printer-computer cable
Ensure printer software is installed
</pre>
 
=={{header|Ada}}==
First the specification of a generic decision table package:
<langsyntaxhighlight Adalang="ada">generic
type Condition is (<>);
type Action is (<>);
Line 33 ⟶ 89:
 
end Generic_Decision_Table;
</syntaxhighlight>
</lang>
Next, the implementation of the generic decision table package:
<langsyntaxhighlight Adalang="ada">package body Generic_Decision_Table is
procedure React(Rules: Rule_A) is
A: Answers;
Line 55 ⟶ 111:
 
end Generic_Decision_Table;
</syntaxhighlight>
</lang>
That was easy! Now we implement the printer troubleshooting application:
<langsyntaxhighlight Adalang="ada">with Generic_Decision_Table, Ada.Text_IO;
 
procedure Printer_Decision_Table is
Line 124 ⟶ 180:
begin
DT.React(R);
end Printer_Decision_Table;</langsyntaxhighlight>
Sample output:
<pre>> ./printer_decision_table
Line 155 ⟶ 211:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">Conditions =
(
Printer does not print |Y|Y|Y|Y|N|N|N|N|
Line 229 ⟶ 285:
Res .= Act%A_Index% "`n"
GuiControl,, Output, % Res
return</langsyntaxhighlight>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f DECISION_TABLES.AWK DECISION_TABLES.TXT
BEGIN {
Line 310 ⟶ 366:
return
}
</syntaxhighlight>
</lang>
<p>decision table:</p>
<pre>
Line 345 ⟶ 401:
 
=={{header|C}}==
With flaky keyboard input:<langsyntaxhighlight Clang="c">#include <stdio.h>
#define N_COND 3
Line 385 ⟶ 441:
}
return 0;
}</langsyntaxhighlight>output<syntaxhighlight lang="text">Printer does not print? y
A red light is flashing? n
Printer is unrecognised? y
Line 392 ⟶ 448:
Check the power cable
Check the printer-computer cable
Ensure printer software is installed</langsyntaxhighlight>
 
=={{header|C++}}==
{{trans|Java}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <string>
#include <vector>
Line 459 ⟶ 515:
delete[] answers;
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Please answer the following questions with a y or n:
Line 471 ⟶ 527:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> >> SOURCE FORMAT FREE
identification division.
program-id. 'decisiontable'.
Line 565 ⟶ 621:
 
end program 'decisiontable'.
</syntaxhighlight>
</lang>
 
{{out}}
Line 578 ⟶ 634:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.exception, std.array;
 
immutable struct DecisionTable {
Line 661 ⟶ 717:
 
d.consult;
}</langsyntaxhighlight>
{{out}}
<pre>Printer is unrecognised? [y=yes/others=no] no
Line 670 ⟶ 726:
===Alternative Version===
{{trans|C}}
<langsyntaxhighlight lang="d">import std.stdio, std.string;
 
struct DataPair(size_t N) {
Line 710 ⟶ 766:
writeln(" ", sol.message);
}
}</langsyntaxhighlight>
{{out}}
<pre>Printer does not print? [y=yes/others=no] no
Line 717 ⟶ 773:
 
So, you don't have a problem then?</pre>
 
Keyboard handling is sketchy, just type "true" or "false" to each question.
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">dim as boolean rules(0 to 2, 0 to 7) =_
{ {false, false, false, false, true, true, true, true},_
{ true, true, false, false, true, true, false, false},_
{false, true, false, true, false, true, false, true} }
 
dim as boolean actions(0 to 4, 0 to 7) =_
{ {false, false, true, false, false, false, false, false},_
{ true, false, true, false, false, false, false, false},_
{ true, false, true, false, true, false, true, false},_
{ true, true, false, false, false, true, false, false},_
{false, true, false, true, false, false, false, false} }
dim as string rule_names(0 to 2) =_
{"Printer prints", "A red light is flashing",_
"Printer is recognized by computer"}
dim as string action_names(0 to 4) = _
{"Check the power cable", "Check the printer-computer cable",_
"Ensure printer software is installed", "Check/replace ink",_
"Check for paper jam" }
 
dim as boolean answers(0 to 2)
 
for i as uinteger = 0 to 2
print rule_names(i),
input answers(i)
next i
 
print "---- Suggested solutions: ----"
 
for i as uinteger = 0 to 4
for j as uinteger = 0 to 7
if not actions(i, j) then continue for
for k as uinteger = 0 to 2
if rules(k, j)<>answers(j) then goto dontdoit
next k
print action_names(i)
dontdoit:
next j
next i</syntaxhighlight>
 
=={{header|Go}}==
Go has no specific support for decision tables, but they can be implemented easily. With a little ingenuity, literal data can be arranged in rows and columns in a way that preserves the visual associations of decision tables. Go has an init function that might be useful for compiling decision tables at program startup. And Go maps allow efficient lookup of actions given conditions.
<langsyntaxhighlight lang="go">package main
 
import (
Line 803 ⟶ 903:
fmt.Println(a)
}
}</langsyntaxhighlight>
Output:
<pre>
Line 811 ⟶ 911:
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">record cond(text,flags)
record act(text,flags,aflags)
 
Line 861 ⟶ 961:
}
}
end</langsyntaxhighlight>
 
Sample Output:<pre>Printer does not print ? Y
Line 870 ⟶ 970:
 
=={{header|J}}==
'''Solution''':<langsyntaxhighlight lang="j">require'strings'
 
'RULE_NAMES RULES'=: |:':'&cut;._2 noun define
Line 896 ⟶ 996:
options=. a #~ y {~ #. 'Y'={.@toupper@deb@(1!:1)@1:@smoutput@,&'?'@dtb"1 q
(,~ ('/'cut'Suggested resolutions:/Solution unknown.') {::~ 0=#) options
)</langsyntaxhighlight>
 
'''Example''' (''solution found''):<langsyntaxhighlight lang="j"> troubleshoot ''
Having trouble? Let's track down the problem:
Printer does not print?
Line 909 ⟶ 1,009:
Check the printer-computer cable
Ensure printer software is installed
Check/replace ink </langsyntaxhighlight>
'''Example''' (''solution not found''):<langsyntaxhighlight lang="j"> troubleshoot ''
Having trouble? Let's track down the problem:
Printer does not print?
Line 919 ⟶ 1,019:
N
Solution unknown.
</syntaxhighlight>
</lang>
 
=== Comments ===
Line 928 ⟶ 1,028:
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Line 1,011 ⟶ 1,111:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Please answer the following questions with a y or n:
Line 1,026 ⟶ 1,126:
===Interactive===
Since this is an interactive web page, results can't be listed. See http://jsfiddle.net/rGP6C/1/ for a live demo.
<langsyntaxhighlight lang="javascript"><html>
<head></head>
<body>
Line 1,081 ⟶ 1,181:
});
</script>
</html></langsyntaxhighlight>
===DecTab object===
This implements a DecisionTable object called <code>DecTab</code>. The JavaScript dialect is Microsoft's JScript.
<langsyntaxhighlight lang="javascript">
var DecTab = function () {
this.conditions = [];
Line 1,133 ⟶ 1,233:
}
}
</syntaxhighlight>
</lang>
The task is rendered as follows
<langsyntaxhighlight lang="javascript">
function Ask(q) {
WScript.StdOut.Write(q);
Line 1,187 ⟶ 1,287:
.RulesActions(["Y", "N", "Y"], [])
.Decide();
</syntaxhighlight>
</lang>
And a sample run
<langsyntaxhighlight lang="text">
C:\>CScript DecisionTable.js
Printer prints [Y/N]?Y
Line 1,195 ⟶ 1,295:
Printer is recognized by computer [Y/N]?Y
Check/replace ink.
</syntaxhighlight>
</lang>
 
=={{header|jq}}==
{{trans|Wren}}
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
In the following solution, `input` is used to read the user's
yes/no responses, and so jq should be invoked with the -R and -n command-line options.
 
Because of the limitations of jq's I/O capabilities, the
prompts for this task are written to stderr, which unfortunately makes them
untidy.
<syntaxhighlight lang="jq">def get_response:
input
| if . == "Y" or . == "y" then "Y"
elif . == "N" or . == "n" then "N"
else null
end;
 
def ask($prompt):
def ask:
$prompt|stderr
| get_response
| if . then . else ask end;
ask;
def conditions: [
["Printer prints" , "NNNNYYYY"],
["A red light is flashing" , "YYNNYYNN"],
["Printer is recognized by computer", "NYNYNYNY"]
];
def actions: [
["Check the power cable" , "NNYNNNNN"],
["Check the printer-computer cable" , "YNYNNNNN"],
["Ensure printer software is installed", "YNYNYNYN"],
["Check/replace ink" , "YYNNNYNN"],
["Check for paper jam" , "NYNYNNNN"]
];
 
def interact:
conditions as $conditions
| actions as $actions
| ($conditions|length) as $nc
| ($actions|length) as $na
| ($conditions[0][1]|length) as $nr # number of rules
| 7 as $np # index of 'no problem' rule
| "Please answer the following questions with a y or n:",
( reduce range(0; $nc) as $c ( null;
.[$c] = ask("\($conditions[$c][0])? ") )) as $answers
| ("\nRecommended action(s):",
label $out
| range(0; $nr) as $r
| if all( range(0; $nc); $conditions[.][1][$r:$r+1] == $answers[.])
then if $r == $np
then "None (no problem detected)."
else $actions[] | select(.[1][$r:$r+1] == "Y") | .[0]
end,
break $out
else empty
end ) ;
 
interact
 
</syntaxhighlight>
'''Example (transcript)'''
<pre>
$ jq -rRn -f decision-tables.jq
Please answer the following questions with a y or n:
"Printer prints? "n
"A red light is flashing? "n
"Printer is recognized by computer? "n
 
Recommended action(s):
Check the power cable
Check the printer-computer cable
Ensure printer software is installed
</pre>
 
 
=={{header|Julia}}==
{{trans|Perl}}
<langsyntaxhighlight lang="julia">const queries = [("Printer does not print", 0b11110000),
("A red light is flashing", 0b11001100),
("Printer is unrecognised", 0b10101010)]
Line 1,232 ⟶ 1,411:
 
decide(queries, answers)
</langsyntaxhighlight>{{out}}
<pre>
Printer does not print?: y
Line 1,242 ⟶ 1,421:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.51
 
val conditions = listOf(
Line 1,290 ⟶ 1,469:
return
}
}</langsyntaxhighlight>
 
Sample input/output:
Line 1,304 ⟶ 1,483:
Ensure printer software is installed
</pre>
 
=={{header|Lua}}==
{{trans|C++}}
<syntaxhighlight lang="lua">function promptYN(q)
local ans = '?'
repeat
io.write(q..'? ')
ans = string.upper(io.read())
until ans == 'Y' or ans == 'N'
return ans == 'Y'
end
 
function getCharAt(s,i)
return string.sub(s,i,i)
end
 
conditions = {
{"Printer prints", "NNNNYYYY"},
{"A red light is flashing", "YYNNYYNN"},
{"Printer is recognized by computer", "NYNYNYNY"},
}
 
actions = {
{"Check the power cable", "NNYNNNNN"},
{"Check the printer-computer cable", "YNYNNNNN"},
{"Ensure printer software is installed", "YNYNYNYN"},
{"Check/replace ink", "YYNNNYNN"},
{"Check for paper jam", "NYNYNNNN"},
}
 
nc = #conditions
nr = string.len(conditions[1][2])
answers = {}
 
print('Please answer the following questions with a y or n:')
for i,v in pairs(conditions) do
answers[i] = promptYN(v[1])
end
 
print('Recommended action(s)')
for r=1,nr do
local skip = false
for c=1,nc do
local yn = answers[c] and 'Y' or 'N'
if getCharAt(conditions[c][2], r) ~= yn then
skip = true
break
end
end
if not skip then
if r == nr then
print(" None (no problem detected)")
else
for i,a in pairs(actions) do
if getCharAt(a[2], r) == 'Y' then
print(" ", a[1])
end
end
end
end
end</syntaxhighlight>
{{out}}
<pre>Please answer the following questions with a y or n:
Printer prints? n
A red light is flashing? n
Printer is recognized by computer? n
Recommended action(s)
Check the power cable
Check the printer-computer cable
Ensure printer software is installed</pre>
 
=={{header|Nim}}==
We have defined a generic decision table object and used it with the Wikipedia printer example.
<syntaxhighlight lang="nim">import strutils, tables
 
####################################################################################################
# Generic defintion of the decision table.
 
type
 
DTable[Cond, Act: Ordinal] = object
questions: array[Cond, string]
answers: array[Act, string]
rules: Table[set[Cond], seq[Act]]
 
proc initDTable[Cond, Act](): DTable[Cond, Act] = discard
 
proc addCondition[Cond, Act](dt: var DTable[Cond, Act]; cond: Cond; question: string) =
if dt.questions[cond].len != 0:
raise newException(ValueError, "condition already set: " & $cond)
dt.questions[cond] = question
 
proc addAction[Cond, Act](dt: var DTable[Cond, Act]; act: Act; answer: string) =
if dt.answers[act].len != 0:
raise newException(ValueError, "action already set: " & $act)
dt.answers[act] = answer
 
proc addRule[Cond, Act](dt: var DTable[Cond, Act]; rule: set[Cond]; acts: openArray[Act]) =
if rule in dt.rules:
raise newException(ValueError, "rule already set.")
dt.rules[rule] = @acts
 
proc askQuestion(question: string): string =
while true:
stdout.write question & ' '
stdout.flushFile()
result = stdin.readLine().strip()
if result in ["y", "n"]: break
 
 
proc apply[Cond, Act](dt: DTable[Cond, Act]) =
# Build condition set.
var conds: set[Cond]
echo "Answer questions with 'y' or 'n'"
for cond, question in dt.questions:
if question.len == 0:
raise newException(ValueError, "missing question for condition: " & $cond)
let answer = askQuestion(question)
if answer == "y":
conds.incl cond
# Apply rule.
echo()
if conds in dt.rules:
echo "Actions recommended:"
for act in dt.rules[conds]:
echo "- ", dt.answers[act]
else:
echo "No action recommended."
 
 
####################################################################################################
# Decision table for Wikipedia printer example.
 
type
 
Condition {.pure.} = enum PrinterPrints, RedLightFlashing, PrinterRecognized
Action {.pure.} = enum CheckPowerCable, CheckPrinterCable, CheckSoftware, CheckInk, CheckPaperJam
 
const
 
Questions = [PrinterPrints: "Does printer print?",
RedLightFlashing: "Does red light is flashing?",
PrinterRecognized: "Is printer recognized by computer?"]
 
Answers = [CheckPowerCable: "check the power cable",
CheckPrinterCable: "check the printer-computer cable",
CheckSoftware: "ensure printer software is installed",
CheckInk: "check/replace ink",
CheckPaperJam: "check for paper jam"]
 
var dt = initDTable[Condition, Action]()
 
for cond, question in Questions:
dt.addCondition(cond, question)
 
for act, answer in Answers:
dt.addAction(act, answer)
 
dt.addRule({}, [CheckPowerCable, CheckPrinterCable, CheckSoftware])
dt.addRule({PrinterPrints}, [CheckSoftware])
dt.addRule({RedLightFlashing}, [CheckPrinterCable, CheckSoftware, CheckInk])
dt.addRule({PrinterRecognized}, [CheckPaperJam])
dt.addRule({PrinterPrints, RedLightFlashing}, [CheckSoftware])
dt.addRule({RedLightFlashing, PrinterRecognized}, [CheckInk, CheckPaperJam])
dt.addRule({PrinterPrints, RedLightFlashing, PrinterRecognized}, [CheckInk])
 
dt.apply()</syntaxhighlight>
 
{{out}}
<pre>Answer questions with 'y' or 'n'
Does printer print? n
Does red light is flashing? y
Is printer recognized by computer? y
 
Actions recommended:
- check/replace ink
- check for paper jam</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl" line>use strictv5.36;
 
use warnings;
sub decide($q,$a) {
my @q = @{$q};
my %a = %{$a};
my($cnt,$bit) = (1,0);
 
sub decide {
our(@q,%a);
local *q = shift;
local *a = shift;
my $bit;
my $cnt = 1;
for my $prompt (@q) {
print "$prompt: ";
Line 1,332 ⟶ 1,688:
'Printer does not print',
);
 
my %answers = (
0b00100000 => 'Check the power cable',
Line 1,340 ⟶ 1,697:
);
 
decide(\@queries,\%answers);</langsyntaxhighlight>
{{out}}
<pre>Printer is unrecognised: n
Line 1,350 ⟶ 1,707:
Written such that conditions and actions could easily be read from a plain text file,
and should cope fairly well with different lengths, missing/conflicting options, etc.
=== console version ===
<lang Phix>constant conditions = """
<!--<syntaxhighlight lang="phix">-->
YYYYNNNN:Printer does not print
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (wait_key)</span>
YYNNYYNN:A red light is flashing
<span style="color: #008080;">constant</span> <span style="color: #000000;">conditions</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
YNYNYNYN:Printer is unrecognized
YYYYNNNN:Printer does not print
"""
YYNNYYNN:A red light is flashing
constant actions = """
YNYNYNYN:Printer is unrecognized
--X-----:Check the power cable
"""</span>
X-X-----:Check the printer-computer cable
<span style="color: #008080;">constant</span> <span style="color: #000000;">actions</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
X-X-X-X-:Ensure printer software is installed
XX --X---XX--:Check/replace inkthe power cable
- X-X-----:Check forthe paperprinter-computer jamcable
X-X-X-X-:Ensure printer software is installed
"""
XX--XX--:Check/replace ink
 
-X-X----:Check for paper jam
procedure validate(sequence s, integer l, string letters, name)
"""</span>
if remainder(length(s),2) then
throw(sprintf("uneven %s pairings",{name}))
<span style="color: #008080;">procedure</span> <span style="color: #000000;">validate</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">letters</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
for i=1 to length(s) by 2 do
<span style="color: #7060A8;">throw</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"uneven %s pairings"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">name</span><span style="color: #0000FF;">}))</span>
string si = s[i]
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if length(si)!=l then
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
throw(sprintf("mismatched lengths (line %d)",i))
<span style="color: #004080;">string</span> <span style="color: #000000;">si</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
end if
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">si</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">l</span> <span style="color: #008080;">then</span>
for j=1 to length(si) do
<span style="color: #7060A8;">throw</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"mismatched lengths (line %d)"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">))</span>
if not find(si[j],letters) then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
throw(sprintf("invalid letter [%c not in %s]",{si[j],letters}))
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">si</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end if
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">si</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span><span style="color: #000000;">letters</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
end for
<span style="color: #7060A8;">throw</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"invalid letter [%c not in %s]"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">si</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span><span style="color: #000000;">letters</span><span style="color: #0000FF;">}))</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
constant qa = split_any(conditions,"\n:",no_empty:=true),
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
sa = split_any(actions,"\n:",no_empty:=true)
integer l = length(qa[1])
<span style="color: #008080;">constant</span> <span style="color: #000000;">qa</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split_any</span><span style="color: #0000FF;">(</span><span style="color: #000000;">conditions</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n:"</span><span style="color: #0000FF;">),</span>
 
<span style="color: #000000;">sa</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split_any</span><span style="color: #0000FF;">(</span><span style="color: #000000;">actions</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n:"</span><span style="color: #0000FF;">)</span>
function ask_questions()
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">qa</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
sequence valid = repeat(true,l)
integer a
<span style="color: #008080;">function</span> <span style="color: #000000;">ask_questions</span><span style="color: #0000FF;">()</span>
for i=1 to length(qa) by 2 do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">valid</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)</span>
while true do
<span style="color: #004080;">integer</span> <span style="color: #000000;">a</span>
puts(1,qa[i+1]&":? ")
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">qa</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
a = upper(wait_key())
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
if find(a,"YN") then exit end if
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">qa</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]&</span><span style="color: #008000;">":? "</span><span style="color: #0000FF;">)</span>
puts(1,"\nplease enter Y or N\n")
<span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">upper</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">())</span>
end while
<span style="color: #008080;">if</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"YN"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
printf(1,"%c\n",a)
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nplease enter Y or N\n"</span><span style="color: #0000FF;">)</span>
for j=1 to l do
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
if a!=qa[i][j] then valid[j] = false end if
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%c\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">l</span> <span style="color: #008080;">do</span>
end for
<span style="color: #008080;">if</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">qa</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span> <span style="color: #000000;">valid</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return valid
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
 
<span style="color: #008080;">return</span> <span style="color: #000000;">valid</span>
procedure show_answers(integer k)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
integer count = 0
for i=1 to length(sa) by 2 do
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show_answers</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
if sa[i][k]='X' then
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
puts(1,sa[i+1]&"\n")
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sa</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
count += 1
<span style="color: #008080;">if</span> <span style="color: #000000;">sa</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'X'</span> <span style="color: #008080;">then</span>
end if
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sa</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
printf(1,"%d answers\n",count)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
 
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d answers\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count</span><span style="color: #0000FF;">)</span>
procedure main()
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
try
validate(qa,l,"YN","condition")
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
validate(sa,l,"-X","action")
<span style="color: #008080;">try</span>
catch e
<span style="color: #000000;">validate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">qa</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"YN"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"condition"</span><span style="color: #0000FF;">)</span>
?e
<span style="color: #000000;">validate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sa</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"-X"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"action"</span><span style="color: #0000FF;">)</span>
abort(0)
<span style="color: #008080;">catch</span> <span style="color: #000000;">e</span>
end try
<span style="color: #0000FF;">?</span><span style="color: #000000;">e</span>
sequence valid = ask_questions()
<span style="color: #7060A8;">abort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
integer sv = sum(valid)
<span style="color: #008080;">end</span> <span style="color: #008080;">try</span>
if sv=0 then
<span style="color: #004080;">sequence</span> <span style="color: #000000;">valid</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ask_questions</span><span style="color: #0000FF;">()</span>
puts(1,"no answers")
<span style="color: #004080;">integer</span> <span style="color: #000000;">sv</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">valid</span><span style="color: #0000FF;">)</span>
elsif sv=1 then
<span style="color: #008080;">if</span> <span style="color: #000000;">sv</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
show_answers(find(true,valid))
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"no answers"</span><span style="color: #0000FF;">)</span>
else
<span style="color: #008080;">elsif</span> <span style="color: #000000;">sv</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
puts(1,"multiple answer sets\n")
<span style="color: #000000;">show_answers</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #000000;">valid</span><span style="color: #0000FF;">))</span>
for i=1 to length(valid) do
<span style="color: #008080;">else</span>
if valid[i] then
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"multiple answer sets\n"</span><span style="color: #0000FF;">)</span>
printf(1,"for %d:\n",i)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">valid</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
show_answers(i)
<span style="color: #008080;">if</span> <span style="color: #000000;">valid</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
end if
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"for %d:\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #000000;">show_answers</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
main()</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,446 ⟶ 1,807:
Check/replace ink
2 answers
</pre>
=== GUI version ===
{{libheader|Phix/pGUI}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">conditions</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
YYYYNNNN:Printer does not print
YYNNYYNN:A red light is flashing
YNYNYNYN:Printer is unrecognized
"""</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">actions</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
--X-----:Check the power cable
X-X-----:Check the printer-computer cable
X-X-X-X-:Ensure printer software is installed
XX--XX--:Check/replace ink
-X-X----:Check for paper jam
"""</span>
<span style="color: #008080;">constant</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">qyn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">qus</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">conditions</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">),</span><span style="color: #008000;">':'</span><span style="color: #0000FF;">})),</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">a_x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">act</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">actions</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">),</span><span style="color: #008000;">':'</span><span style="color: #0000FF;">}))</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">nq</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">qus</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">na</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">act</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">ynl</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">qyn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">qyn</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">)=</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ynl</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nq</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"mismatched YN lengths"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a_x</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">)=</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ynl</span><span style="color: #0000FF;">,</span><span style="color: #000000;">na</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"mismatched X- lengths"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">unique</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">qyn</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">))=</span><span style="color: #008000;">"NY"</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">unique</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a_x</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">))=</span><span style="color: #008000;">"-X"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">title</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Decision_tables"</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">help_text</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
Check all that apply and press the "Get Answers" button.
"""</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tbox</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hbox</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">vbox</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">okbtn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">answers</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">toggles</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">show_help</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupMessage</span><span style="color: #0000FF;">(</span><span style="color: #000000;">title</span><span style="color: #0000FF;">,</span><span style="color: #000000;">help_text</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_IGNORE</span> <span style="color: #000080;font-style:italic;">-- (don't open browser help!)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">button_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*okbtn*/</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">valid</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ynl</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">nq</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">yn</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"YN"</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">IupGetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">toggles</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #008000;">"VALUE"</span><span style="color: #0000FF;">)]</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">ynl</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">yn</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">qyn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span> <span style="color: #000000;">valid</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">sv</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">valid</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">answer</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sv</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"no answers"</span><span style="color: #0000FF;">:</span>
<span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sv</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #008000;">""</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"multiple answer sets\n"</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">sv</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">ynl</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">valid</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">sv</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">answer</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"for %d:\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">actions</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">na</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">a_x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">][</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'X'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">actions</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">actions</span><span style="color: #0000FF;">,</span><span style="color: #000000;">act</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">actions</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">actions</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d answers\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">actions</span><span style="color: #0000FF;">)))</span>
<span style="color: #000000;">answer</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">actions</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">IupSetStrAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">answers</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">answer</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"SIZE"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">NULL</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupRefresh</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">key_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*dlg*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_ESC</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">IUP_CLOSE</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- (standard practice for me)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_F5</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- (let browser reload work)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_F1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">show_help</span><span style="color: #0000FF;">()</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_CR</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">button_cb</span><span style="color: #0000FF;">(</span><span style="color: #000000;">okbtn</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_IGNORE</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">toggles</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">IupToggle</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">qus</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"RIGHTBUTTON=YES, CANFOCUS=NO"</span><span style="color: #0000FF;">}})</span>
<span style="color: #000000;">okbtn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupButton</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Get Answers"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"button_cb"</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"PADDING=5x5"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">answers</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"EXPAND=BOTH, PADDING=10x10"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">tbox</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupVbox</span><span style="color: #0000FF;">(</span><span style="color: #000000;">toggles</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ALIGNMENT=ARIGHT"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">hbox</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupHbox</span><span style="color: #0000FF;">({</span><span style="color: #000000;">tbox</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">IupVbox</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">(),</span><span style="color: #000000;">okbtn</span><span style="color: #0000FF;">})})</span>
<span style="color: #000000;">vbox</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupVbox</span><span style="color: #0000FF;">({</span><span style="color: #000000;">hbox</span><span style="color: #0000FF;">,</span><span style="color: #000000;">answers</span><span style="color: #0000FF;">},</span><span style="color: #008000;">"MARGIN=20x10"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">vbox</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE="%s", MINSIZE=540x200`</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">title</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallback</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"KEY_CB"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"key_cb"</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">IupSetAttributeHandle</span><span style="color: #0000FF;">(</span><span style="color: #004600;">NULL</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"PARENTDIALOG"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</syntaxhighlight>-->
 
=={{header|Picat}}==
===Interactive version===
<syntaxhighlight lang="picat">import util.
%
% Interactive version.
% Answer with YnNn
%
go =>
conditions(Conditions),
solutions(Solutions),
 
Answers=query(Conditions),
println(join(decision_table(Solutions,Answers),'\n')),
nl.
 
%
% Query the user about the problem.
% Valid responses: YyNn
%
query(Conditions) = Answers =>
Answers = [0 : _I in 1..Conditions[1,2].length],
println("Please answer the questions with YyNn."),
foreach([Text,Cond] in Conditions)
A = "",
while (not(membchk(A, ["Y","N","y","n"])))
printf("%s? ", Text),
A := read_line(),
if membchk(A,["Y","y"]) then
Answers := map(\/, Answers,Cond)
end
end
end,
nl.
 
%
% Get the result given the answers (list of 01).
%
decision_table(Solutions,Answers) = Result =>
Result = [],
if sum(Answers) = 0 then
Result := ["Nice, no problem."]
else
foreach([Text,Sol] in Solutions)
if map(/\,Sol,Answers) == Sol then
Result := Result ++ [Text]
end
end
end.
 
conditions(Conditions) =>
Conditions = [
["Printer does not print", [1,1,1,1,0,0,0,0]],
["A red light is flashing", [1,1,0,0,1,1,0,0]],
["Printer is unrecognised", [1,0,1,0,1,0,1,0]]
].
 
solutions(Solutions) =>
Solutions = [
["Check the power cable", [0,0,1,0,0,0,0,0]],
["Check the printer-computer cable", [1,0,1,0,0,0,0,0]],
["Ensure printer software is installed",[1,0,1,0,1,0,1,0]],
["Check/replace ink", [1,1,0,0,1,1,0,0]],
["Check for paper jam", [0,1,0,1,0,0,0,0]]
].</syntaxhighlight>
 
{{out}}
<pre>
Please answer the questions with YyNn.
Printer does not print? y
A red light is flashing? n
Printer is unrecognised? y
 
Check the power cable
Check the printer-computer cable
Ensure printer software is installed
Check for paper jam</pre>
 
===All possible answers/combinations===
For debugging purposes it it's nice to be able to print all the possible combinations of answers/solutions.
 
<syntaxhighlight lang="picat">%
% Go through all combinations of answers
%
go2 =>
PS = power_set([1,2,3]).delete([]).sort(),
println(PS),
conditions(Conditions),
solutions(Solutions),
AllCond = [Cond : [_,Cond] in Conditions],
foreach(P in PS)
println(p=P),
println(conditions=join([Conditions[I].first() : I in P],', ')),
A = [0:_I in 1..Conditions[1,2].length],
foreach(C in [AllCond[I] : I in P])
A := map(\/,A,C)
end,
println(a=A),
println(join(decision_table(Solutions,A),'\n')),
nl
end,
nl.</syntaxhighlight>
 
{{out}}
(truncated)
<pre>
p = [1]
conditions = Printer does not print
a = [1,1,1,1,0,0,0,0]
Check the power cable
Check the printer-computer cable
Check for paper jam
 
p = [1,2]
conditions = Printer does not print, A red light is flashing
a = [1,1,1,1,1,1,0,0]
Check the power cable
Check the printer-computer cable
Check/replace ink
Check for paper jam
 
p = [1,2,3]
conditions = Printer does not print, A red light is flashing, Printer is unrecognised
a = [1,1,1,1,1,1,1,0]
Check the power cable
Check the printer-computer cable
Ensure printer software is installed
Check/replace ink
Check for paper jam
 
...
</pre>
 
=={{header|PicoLisp}}==
We allow ourselves a luxurious user interface:
<langsyntaxhighlight PicoLisplang="picolisp">(de yes? (Cond)
(out NIL (prin (car Cond) "? "))
(in NIL
Line 1,459 ⟶ 2,048:
T )
(T (member Reply '(NIL N NO No n no false 0)))
(prinl "Please answer 'Yes' or 'No'") ) ) ) )</langsyntaxhighlight>
The decision table used in the example:
<langsyntaxhighlight PicoLisplang="picolisp">(de *Conditions
("Printer does not print" T T T T NIL NIL NIL NIL)
("A red light is flashing" T T NIL NIL T T NIL NIL)
Line 1,471 ⟶ 2,060:
("Ensure printer software is installed" T NIL T NIL T NIL T)
("Check/replace ink" T T NIL NIL T T)
("Check for paper jam" NIL T NIL T) )</langsyntaxhighlight>
The decision can be made directly on the condition and action data, without the need to create intermediate tables:
<langsyntaxhighlight PicoLisplang="picolisp">(de decide ()
(let Reply (mapcar yes? *Conditions)
(extract and
Line 1,480 ⟶ 2,069:
(unless (pick '((Flg) (<> Flg (next))) Reply)
(rest) ) ) )
(mapcar car *Actions) ) ) )</langsyntaxhighlight>
Output:
<pre>: (decide)
Line 1,501 ⟶ 2,090:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
'''
Create a Decision table then use it
Line 1,540 ⟶ 2,129:
dt_user(dt)
dt_user(dt)
dt_user(dt)</langsyntaxhighlight>
 
'''Sample Run'''
Line 1,591 ⟶ 2,180:
Try this:
Pray!</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ say "Check power cable." cr ] is cpc ( --> )
[ say "Check printer-computer cable." cr ] is cpcc ( --> )
[ say "Ensure printer software installed." cr ] is epsi ( --> )
[ say "Check/replace ink." cr ] is cri ( --> )
[ say "Check for paper jam." cr ] is cpj ( --> )
[ say "No problem detected." cr ] is npd ( --> )
 
[ [ dup $ " (y/n) " join input
dup $ "" = iff drop again ]
nip 0 peek upper char Y = ] is condition ( $ --> b )
 
[ $ "Does the printer print?"
condition
$ "Is a red light flashing?"
condition
$ "Does the computer recognise the printer?"
condition
1 << | 1 << |
[ table
( n n n ) [ cpc cpcc epsi ]
( y n n ) epsi
( n y n ) [ cpcc epsi cri ]
( y y n ) epsi
( n n y ) cpj
( y n y ) npd
( n y y ) [ cri cpj ]
( y y y ) cri ]
cr do ] is diagnose ( --> )</syntaxhighlight>
 
{{out}}
 
As a dialogue in the Quackery shell.
 
<pre>/O> diagnose
...
Does the printer print? (y/n) n
Is a red light flashing? (y/n) n
Does the computer recognise the printer? (y/n) y
 
Check for paper jam.
 
</pre>
 
=={{header|Racket}}==
Line 1,597 ⟶ 2,231:
I thought it might be fun to merge them.
 
<langsyntaxhighlight lang="racket">#lang unstable/2d racket
 
(define (ask-y/n q)
Line 1,684 ⟶ 2,318:
╚═╩════════════╩════════════════════════════════════════════╩═╩═╩═╩═╩═══════╝)
 
(run-decision-table printer-troubleshooting-2dtable)</langsyntaxhighlight>
 
{{out}}
Line 1,696 ⟶ 2,330:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub decide (@q, @s) {
my $bit = 2 ** [+] (1,2,4...*) Z* reverse @q.map: {
so prompt(.value ~ "? ") ~~ /:i ^y/;
Line 1,718 ⟶ 2,352:
);
say '';
}</langsyntaxhighlight>
A bit of explanation: we pass in two pair lists for the questions and solutions; we ignore the keys of the questions, since they can be
generated by regarding them as a binary counter from right to left, with the least significant bit on the bottom. The <tt>@q.map</tt> runs the prompts and maps them to booleans
Line 1,757 ⟶ 2,391:
::* &nbsp; visual fidelity aids for postmortem analysis (as in logs)
::* &nbsp; a method of allowing the user to quit (opt-out of) the interrogation
<langsyntaxhighlight lang="rexx">/*REXX program demonstrates a (query) decision table and possible corrective actions.*/
Q.=; Q.1 = 'Does the printer not print?'
Q.2 = 'Is there a red light flashing on the printer?'
Q.3 = 'Is the printer unrecognized by the software?'
Q.0 = 3 /*the number of questions to be asked. */
action.= /* Y=yes N=no if character isn't a letter = don't care.*/
 
Line 1,775 ⟶ 2,410:
 
do i=1 while Q.i\==''; ans.i= asker(i) /*display the question, obtain response*/
end /*i*/
end /*i*/; Q.0= i - 1 /*adjust Q.0 for the DO loop indexing. */
say /*display a blank line before questions*/
possible= 0 /*we'll be counting possible solutions.*/
Line 1,806 ⟶ 2,441:
if (abbrev('YES', u) | abbrev("NO", u)) & words(x)==1 then return u1
say 'invalid response: ' x; oops= 1
end /*forever*/</langsyntaxhighlight>
{{out|output|text=&nbsp; (a screen scraping using a DOS prompt window for some of the possible responses):}}
 
('''decision.rex''' &nbsp; is the name of the REXX program that produced this output.)
<pre style="height:50ex72ex">
D:\►rexx decision
 
───────── (question 1 of 3) Does the printer not print?
no ◄■■■■■■■■ user input.
no
───────── (question 2 of 3) Is there a red light flashing on the printer?
No ◄■■■■■■■■ user input.
No
───────── (question 3 of 3) Is the printer unrecognized by the software?
n ◄■■■■■■■■ user input.
n
 
═════════ There are no solutions for the information supplied.
Line 1,824 ⟶ 2,459:
D:\►rexx decision
───────── (question 1 of 3) Does the printer not print?
n ◄■■■■■■■■ user input.
n
───────── (question 2 of 3) Is there a red light flashing on the printer?
n ◄■■■■■■■■ user input.
n
───────── (question 3 of 3) Is the printer unrecognized by the software?
yes ◄■■■■■■■■ user input.
yes
 
═════════ Ensure printer software is installed.
Line 1,834 ⟶ 2,469:
D:\►rexx decision
───────── (question 1 of 3) Does the printer not print?
n ◄■■■■■■■■ user input.
n
───────── (question 2 of 3) Is there a red light flashing on the printer?
Ye ◄■■■■■■■■ user input.
Ye
───────── (question 3 of 3) Is the printer unrecognized by the software?
n ◄■■■■■■■■ user input.
n
 
═════════ Check/replace ink.
Line 1,844 ⟶ 2,479:
D:\►rexx decision
───────── (question 1 of 3) Does the printer not print?
n ◄■■■■■■■■ user input.
n
───────── (question 2 of 3) Is there a red light flashing on the printer?
yes. ◄■■■■■■■■ user input.
yes.
───────── (question 3 of 3) Is the printer unrecognized by the software?
yes ◄■■■■■■■■ user input.
yes
 
═════════ Ensure printer software is installed.
Line 1,855 ⟶ 2,490:
D:\►rexx decision
───────── (question 1 of 3) Does the printer not print?
y ◄■■■■■■■■ user input.
y
───────── (question 2 of 3) Is there a red light flashing on the printer?
no ◄■■■■■■■■ user input.
no
───────── (question 3 of 3) Is the printer unrecognized by the software?
no ◄■■■■■■■■ user input.
no
 
═════════ Check for paper jam.
Line 1,865 ⟶ 2,500:
D:\►rexx decision
───────── (question 1 of 3) Does the printer not print?
y ◄■■■■■■■■ user input.
y
───────── (question 2 of 3) Is there a red light flashing on the printer?
n ◄■■■■■■■■ user input.
n
───────── (question 3 of 3) Is the printer unrecognized by the software?
y ◄■■■■■■■■ user input.
y
 
═════════ Check the power cable.
Line 1,877 ⟶ 2,512:
D:\►rexx decision
───────── (question 1 of 3) Does the printer not print?
y ◄■■■■■■■■ user input.
y
───────── (question 2 of 3) Is there a red light flashing on the printer?
y ◄■■■■■■■■ user input.
y
───────── (question 3 of 3) Is the printer unrecognized by the software?
n ◄■■■■■■■■ user input.
n
 
═════════ Check/replace ink.
Line 1,888 ⟶ 2,523:
D:\►rexx decision
───────── (question 1 of 3) Does the printer not print?
y ◄■■■■■■■■ user input.
y
───────── (question 2 of 3) Is there a red light flashing on the printer?
y ◄■■■■■■■■ user input.
y
───────── (question 3 of 3) Is the printer unrecognized by the software?
y ◄■■■■■■■■ user input.
y
 
═════════ check the printer-computer cable.
Line 1,902 ⟶ 2,537:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Decision tables
 
Line 1,937 ⟶ 2,572:
see "invalid input: " + upper(notprinting) + " " + upper(flashing) + " " + upper(notrecognized) + nl
ok
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,949 ⟶ 2,584:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">class DecisionTable
def initialize(conditions, actions)
@conditions = conditions
Line 2,001 ⟶ 2,636:
]
)
loop {dt.run}</langsyntaxhighlight>
 
Example
Line 2,068 ⟶ 2,703:
=={{header|Scala}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import java.io.{BufferedReader, InputStreamReader}
 
import scala.util.control.Breaks
Line 2,143 ⟶ 2,778:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Please answer the following questions with a y or n:
Line 2,155 ⟶ 2,790:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func decide (q, s) {
 
var bits = q.map { |p|
Line 2,190 ⟶ 2,825:
)
say ''
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,219 ⟶ 2,854:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">package require TclOO
 
#http://rosettacode.org/wiki/Keyboard_Input/Obtain_a_Y_or_N_response#Tcl
Line 2,258 ⟶ 2,893:
}
}
}</langsyntaxhighlight>
Demonstration:
<langsyntaxhighlight lang="tcl">DecisionTable create printerDiagnosisTable {
"Printer does not print"
"A red light is flashing"
Line 2,271 ⟶ 2,906:
"Check for paper jam" {0 1 0 1}
}
printerDiagnosisTable consult</langsyntaxhighlight>
Output:
<pre>
Line 2,279 ⟶ 2,914:
Ensure printer software is installed
Check/replace ink
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-str}}
<syntaxhighlight lang="wren">import "io" for Stdin, Stdout
import "./str" for Str
 
var conditions = [
["Printer prints" , "NNNNYYYY"],
["A red light is flashing" , "YYNNYYNN"],
["Printer is recognized by computer", "NYNYNYNY"]
]
 
var actions = [
["Check the power cable" , "NNYNNNNN"],
["Check the printer-computer cable" , "YNYNNNNN"],
["Ensure printer software is installed", "YNYNYNYN"],
["Check/replace ink" , "YYNNNYNN"],
["Check for paper jam" , "NYNYNNNN"]
]
 
var nc = conditions.count
var na = actions.count
var nr = conditions[0][1].count // number of rules
var np = 7 // index of 'no problem' rule
System.print("Please answer the following questions with a y or n:")
var answers = List.filled(nc, false)
for (c in 0...nc) {
var input
while (true) {
System.write(" %(conditions[c][0]) ? ")
Stdout.flush()
input = Str.upper(Stdin.readLine())
if (input == "Y" || input == "N") break
}
answers[c] = (input == "Y")
}
System.print("\nRecommended action(s):")
for (r in 0...nr) {
var outer = false
for (c in 0...nc) {
var yn = answers[c] ? "Y" : "N"
if (conditions[c][1][r] != yn) {
outer = true
break
}
}
if (!outer) {
if (r == np) {
System.print(" None (no problem detected)")
} else {
for (a in 0...na) {
if (actions[a][1][r] == "Y") System.print(" %(actions[a][0])")
}
}
break
}
}</syntaxhighlight>
 
{{out}}
Sample input/output:
<pre>
Please answer the following questions with a y or n:
Printer prints ? n
A red light is flashing ? n
Printer is recognized by computer ? n
 
Recommended action(s):
Check the power cable
Check the printer-computer cable
Ensure printer software is installed
</pre>
 
=={{header|XPL0}}==
{{trans|Wren}}
<syntaxhighlight lang "XPL0">include xpllib; \for ToUpper, Print
 
int Conditions, Actions, NC, NA, NR, NP, Answers(3), Input, Outer, YN, R, C, A;
char S;
[Conditions:= [
["Printer prints" , "NNNNYYYY"],
["A red light is flashing" , "YYNNYYNN"],
["Printer is recognized by computer", "NYNYNYNY"]
];
Actions:= [
["Check the power cable" , "NNYNNNNN"],
["Check the printer-computer cable" , "YNYNNNNN"],
["Ensure printer software is installed", "YNYNYNYN"],
["Check/replace ink" , "YYNNNYNN"],
["Check for paper jam" , "NYNYNNNN"]
];
NC:= 3;
NA:= 5;
NR:= 8; \number of rules
NP:= 7; \index of 'no problem' rule
Print("Please answer the following questions with a y or n:\n");
for C:= 0 to NC-1 do
[loop [Print(" %s ? ", Conditions(C,0));
OpenI(0);
Input:= ToUpper(ChIn(0));
if Input = ^Y or Input = ^N then quit;
];
Answers(C):= Input = ^Y;
];
Print("\nRecommended action(s):\n");
for R:= 0 to NR-1 do
[Outer:= false;
for C:= 0 to NC-1 do
[YN:= if Answers(C) then ^Y else ^N;
S:= Conditions(C,1); \to access character bytes not integers
if S(R) # YN then
[Outer:= true;
C:= NC; \break
];
];
if not Outer then
[if R = NP then
Print(" None (no problem detected)\n")
else [for A:= 0 to NA-1 do
[S:= Actions(A,1);
if S(R) = ^Y then
Print(" %s\n", Actions(A,0));
];
];
R:= NR; \break
];
];
]</syntaxhighlight>
{{out}}
<pre>
Please answer the following questions with a y or n:
Printer prints ? n
A red light is flashing ? n
Printer is recognized by computer ? n
 
Recommended action(s):
Check the power cable
Check the printer-computer cable
Ensure printer software is installed
</pre>
9,476

edits