Decision tables: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: added a gui version)
m (syntax highlighting fixup automation)
Line 15: Line 15:
{{trans|Kotlin}}
{{trans|Kotlin}}


<lang 11l>V conditions = [
<syntaxhighlight lang="11l">V conditions = [
(‘Printer prints’ , ‘NNNNYYYY’),
(‘Printer prints’ , ‘NNNNYYYY’),
(‘A red light is flashing’ , ‘YYNNYYNN’),
(‘A red light is flashing’ , ‘YYNNYYNN’),
Line 53: Line 53:
I action[1][r] == ‘Y’
I action[1][r] == ‘Y’
print(‘ ’action[0])
print(‘ ’action[0])
L.break</lang>
L.break</syntaxhighlight>


{{out}}
{{out}}
Line 70: Line 70:
=={{header|Ada}}==
=={{header|Ada}}==
First the specification of a generic decision table package:
First the specification of a generic decision table package:
<lang Ada>generic
<syntaxhighlight lang="ada">generic
type Condition is (<>);
type Condition is (<>);
type Action is (<>);
type Action is (<>);
Line 89: Line 89:


end Generic_Decision_Table;
end Generic_Decision_Table;
</syntaxhighlight>
</lang>
Next, the implementation of the generic decision table package:
Next, the implementation of the generic decision table package:
<lang Ada>package body Generic_Decision_Table is
<syntaxhighlight lang="ada">package body Generic_Decision_Table is
procedure React(Rules: Rule_A) is
procedure React(Rules: Rule_A) is
A: Answers;
A: Answers;
Line 111: Line 111:


end Generic_Decision_Table;
end Generic_Decision_Table;
</syntaxhighlight>
</lang>
That was easy! Now we implement the printer troubleshooting application:
That was easy! Now we implement the printer troubleshooting application:
<lang Ada>with Generic_Decision_Table, Ada.Text_IO;
<syntaxhighlight lang="ada">with Generic_Decision_Table, Ada.Text_IO;


procedure Printer_Decision_Table is
procedure Printer_Decision_Table is
Line 180: Line 180:
begin
begin
DT.React(R);
DT.React(R);
end Printer_Decision_Table;</lang>
end Printer_Decision_Table;</syntaxhighlight>
Sample output:
Sample output:
<pre>> ./printer_decision_table
<pre>> ./printer_decision_table
Line 211: Line 211:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>Conditions =
<syntaxhighlight lang="autohotkey">Conditions =
(
(
Printer does not print |Y|Y|Y|Y|N|N|N|N|
Printer does not print |Y|Y|Y|Y|N|N|N|N|
Line 285: Line 285:
Res .= Act%A_Index% "`n"
Res .= Act%A_Index% "`n"
GuiControl,, Output, % Res
GuiControl,, Output, % Res
return</lang>
return</syntaxhighlight>


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f DECISION_TABLES.AWK DECISION_TABLES.TXT
# syntax: GAWK -f DECISION_TABLES.AWK DECISION_TABLES.TXT
BEGIN {
BEGIN {
Line 366: Line 366:
return
return
}
}
</syntaxhighlight>
</lang>
<p>decision table:</p>
<p>decision table:</p>
<pre>
<pre>
Line 401: Line 401:


=={{header|C}}==
=={{header|C}}==
With flaky keyboard input:<lang C>#include <stdio.h>
With flaky keyboard input:<syntaxhighlight lang="c">#include <stdio.h>
#define N_COND 3
#define N_COND 3
Line 441: Line 441:
}
}
return 0;
return 0;
}</lang>output<lang>Printer does not print? y
}</syntaxhighlight>output<syntaxhighlight lang="text">Printer does not print? y
A red light is flashing? n
A red light is flashing? n
Printer is unrecognised? y
Printer is unrecognised? y
Line 448: Line 448:
Check the power cable
Check the power cable
Check the printer-computer cable
Check the printer-computer cable
Ensure printer software is installed</lang>
Ensure printer software is installed</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
{{trans|Java}}
{{trans|Java}}
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <string>
#include <string>
#include <vector>
#include <vector>
Line 515: Line 515:
delete[] answers;
delete[] answers;
return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Please answer the following questions with a y or n:
<pre>Please answer the following questions with a y or n:
Line 527: Line 527:


=={{header|COBOL}}==
=={{header|COBOL}}==
<lang cobol> >> SOURCE FORMAT FREE
<syntaxhighlight lang="cobol"> >> SOURCE FORMAT FREE
identification division.
identification division.
program-id. 'decisiontable'.
program-id. 'decisiontable'.
Line 621: Line 621:


end program 'decisiontable'.
end program 'decisiontable'.
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 634: Line 634:


=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio, std.algorithm, std.exception, std.array;
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.exception, std.array;


immutable struct DecisionTable {
immutable struct DecisionTable {
Line 717: Line 717:


d.consult;
d.consult;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Printer is unrecognised? [y=yes/others=no] no
<pre>Printer is unrecognised? [y=yes/others=no] no
Line 726: Line 726:
===Alternative Version===
===Alternative Version===
{{trans|C}}
{{trans|C}}
<lang d>import std.stdio, std.string;
<syntaxhighlight lang="d">import std.stdio, std.string;


struct DataPair(size_t N) {
struct DataPair(size_t N) {
Line 766: Line 766:
writeln(" ", sol.message);
writeln(" ", sol.message);
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Printer does not print? [y=yes/others=no] no
<pre>Printer does not print? [y=yes/others=no] no
Line 777: Line 777:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>dim as boolean rules(0 to 2, 0 to 7) =_
<syntaxhighlight lang="freebasic">dim as boolean rules(0 to 2, 0 to 7) =_
{ {false, false, false, false, true, true, true, true},_
{ {false, false, false, false, true, true, true, true},_
{ true, true, false, false, true, true, false, false},_
{ true, true, false, false, true, true, false, false},_
Line 816: Line 816:
dontdoit:
dontdoit:
next j
next j
next i</lang>
next i</syntaxhighlight>


=={{header|Go}}==
=={{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.
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.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 903: Line 903:
fmt.Println(a)
fmt.Println(a)
}
}
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 911: Line 911:


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
<lang Icon>record cond(text,flags)
<syntaxhighlight lang="icon">record cond(text,flags)
record act(text,flags,aflags)
record act(text,flags,aflags)


Line 961: Line 961:
}
}
}
}
end</lang>
end</syntaxhighlight>


Sample Output:<pre>Printer does not print ? Y
Sample Output:<pre>Printer does not print ? Y
Line 970: Line 970:


=={{header|J}}==
=={{header|J}}==
'''Solution''':<lang j>require'strings'
'''Solution''':<syntaxhighlight lang="j">require'strings'


'RULE_NAMES RULES'=: |:':'&cut;._2 noun define
'RULE_NAMES RULES'=: |:':'&cut;._2 noun define
Line 996: Line 996:
options=. a #~ y {~ #. 'Y'={.@toupper@deb@(1!:1)@1:@smoutput@,&'?'@dtb"1 q
options=. a #~ y {~ #. 'Y'={.@toupper@deb@(1!:1)@1:@smoutput@,&'?'@dtb"1 q
(,~ ('/'cut'Suggested resolutions:/Solution unknown.') {::~ 0=#) options
(,~ ('/'cut'Suggested resolutions:/Solution unknown.') {::~ 0=#) options
)</lang>
)</syntaxhighlight>


'''Example''' (''solution found''):<lang j> troubleshoot ''
'''Example''' (''solution found''):<syntaxhighlight lang="j"> troubleshoot ''
Having trouble? Let's track down the problem:
Having trouble? Let's track down the problem:
Printer does not print?
Printer does not print?
Line 1,009: Line 1,009:
Check the printer-computer cable
Check the printer-computer cable
Ensure printer software is installed
Ensure printer software is installed
Check/replace ink </lang>
Check/replace ink </syntaxhighlight>
'''Example''' (''solution not found''):<lang j> troubleshoot ''
'''Example''' (''solution not found''):<syntaxhighlight lang="j"> troubleshoot ''
Having trouble? Let's track down the problem:
Having trouble? Let's track down the problem:
Printer does not print?
Printer does not print?
Line 1,019: Line 1,019:
N
N
Solution unknown.
Solution unknown.
</syntaxhighlight>
</lang>


=== Comments ===
=== Comments ===
Line 1,028: Line 1,028:
=={{header|Java}}==
=={{header|Java}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang java>import java.io.BufferedReader;
<syntaxhighlight lang="java">import java.io.BufferedReader;
import java.io.IOException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InputStreamReader;
Line 1,111: Line 1,111:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Please answer the following questions with a y or n:
<pre>Please answer the following questions with a y or n:
Line 1,126: Line 1,126:
===Interactive===
===Interactive===
Since this is an interactive web page, results can't be listed. See http://jsfiddle.net/rGP6C/1/ for a live demo.
Since this is an interactive web page, results can't be listed. See http://jsfiddle.net/rGP6C/1/ for a live demo.
<lang javascript><html>
<syntaxhighlight lang="javascript"><html>
<head></head>
<head></head>
<body>
<body>
Line 1,181: Line 1,181:
});
});
</script>
</script>
</html></lang>
</html></syntaxhighlight>
===DecTab object===
===DecTab object===
This implements a DecisionTable object called <code>DecTab</code>. The JavaScript dialect is Microsoft's JScript.
This implements a DecisionTable object called <code>DecTab</code>. The JavaScript dialect is Microsoft's JScript.
<lang javascript>
<syntaxhighlight lang="javascript">
var DecTab = function () {
var DecTab = function () {
this.conditions = [];
this.conditions = [];
Line 1,233: Line 1,233:
}
}
}
}
</syntaxhighlight>
</lang>
The task is rendered as follows
The task is rendered as follows
<lang javascript>
<syntaxhighlight lang="javascript">
function Ask(q) {
function Ask(q) {
WScript.StdOut.Write(q);
WScript.StdOut.Write(q);
Line 1,287: Line 1,287:
.RulesActions(["Y", "N", "Y"], [])
.RulesActions(["Y", "N", "Y"], [])
.Decide();
.Decide();
</syntaxhighlight>
</lang>
And a sample run
And a sample run
<lang text>
<syntaxhighlight lang="text">
C:\>CScript DecisionTable.js
C:\>CScript DecisionTable.js
Printer prints [Y/N]?Y
Printer prints [Y/N]?Y
Line 1,295: Line 1,295:
Printer is recognized by computer [Y/N]?Y
Printer is recognized by computer [Y/N]?Y
Check/replace ink.
Check/replace ink.
</syntaxhighlight>
</lang>


=={{header|jq}}==
=={{header|jq}}==
Line 1,308: Line 1,308:
prompts for this task are written to stderr, which unfortunately makes them
prompts for this task are written to stderr, which unfortunately makes them
untidy.
untidy.
<lang jq>def get_response:
<syntaxhighlight lang="jq">def get_response:
input
input
| if . == "Y" or . == "y" then "Y"
| if . == "Y" or . == "y" then "Y"
Line 1,360: Line 1,360:
interact
interact


</syntaxhighlight>
</lang>
'''Example (transcript)'''
'''Example (transcript)'''
<pre>
<pre>
Line 1,378: Line 1,378:
=={{header|Julia}}==
=={{header|Julia}}==
{{trans|Perl}}
{{trans|Perl}}
<lang julia>const queries = [("Printer does not print", 0b11110000),
<syntaxhighlight lang="julia">const queries = [("Printer does not print", 0b11110000),
("A red light is flashing", 0b11001100),
("A red light is flashing", 0b11001100),
("Printer is unrecognised", 0b10101010)]
("Printer is unrecognised", 0b10101010)]
Line 1,411: Line 1,411:


decide(queries, answers)
decide(queries, answers)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Printer does not print?: y
Printer does not print?: y
Line 1,421: Line 1,421:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.1.51
<syntaxhighlight lang="scala">// version 1.1.51


val conditions = listOf(
val conditions = listOf(
Line 1,469: Line 1,469:
return
return
}
}
}</lang>
}</syntaxhighlight>


Sample input/output:
Sample input/output:
Line 1,486: Line 1,486:
=={{header|Lua}}==
=={{header|Lua}}==
{{trans|C++}}
{{trans|C++}}
<lang lua>function promptYN(q)
<syntaxhighlight lang="lua">function promptYN(q)
local ans = '?'
local ans = '?'
repeat
repeat
Line 1,543: Line 1,543:
end
end
end
end
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>Please answer the following questions with a y or n:
<pre>Please answer the following questions with a y or n:
Line 1,556: Line 1,556:
=={{header|Nim}}==
=={{header|Nim}}==
We have defined a generic decision table object and used it with the Wikipedia printer example.
We have defined a generic decision table object and used it with the Wikipedia printer example.
<lang Nim>import strutils, tables
<syntaxhighlight lang="nim">import strutils, tables


####################################################################################################
####################################################################################################
Line 1,651: Line 1,651:
dt.addRule({PrinterPrints, RedLightFlashing, PrinterRecognized}, [CheckInk])
dt.addRule({PrinterPrints, RedLightFlashing, PrinterRecognized}, [CheckInk])


dt.apply()</lang>
dt.apply()</syntaxhighlight>


{{out}}
{{out}}
Line 1,664: Line 1,664:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;


Line 1,698: Line 1,698:
);
);


decide(\@queries,\%answers);</lang>
decide(\@queries,\%answers);</syntaxhighlight>
{{out}}
{{out}}
<pre>Printer is unrecognised: n
<pre>Printer is unrecognised: n
Line 1,709: Line 1,709:
and should cope fairly well with different lengths, missing/conflicting options, etc.
and should cope fairly well with different lengths, missing/conflicting options, etc.
=== console version ===
=== console version ===
<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (wait_key)</span>
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (wait_key)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">conditions</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
<span style="color: #008080;">constant</span> <span style="color: #000000;">conditions</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
Line 1,799: Line 1,799:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,811: Line 1,811:
=== GUI version ===
=== GUI version ===
{{libheader|Phix/pGUI}}
{{libheader|Phix/pGUI}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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;">"""
<span style="color: #008080;">constant</span> <span style="color: #000000;">conditions</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
Line 1,903: Line 1,903:
<span style="color: #7060A8;">IupClose</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>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


=={{header|Picat}}==
=={{header|Picat}}==
===Interactive version===
===Interactive version===
<lang Picat>import util.
<syntaxhighlight lang="picat">import util.
%
%
% Interactive version.
% Interactive version.
Line 1,968: Line 1,968:
["Check/replace ink", [1,1,0,0,1,1,0,0]],
["Check/replace ink", [1,1,0,0,1,1,0,0]],
["Check for paper jam", [0,1,0,1,0,0,0,0]]
["Check for paper jam", [0,1,0,1,0,0,0,0]]
].</lang>
].</syntaxhighlight>


{{out}}
{{out}}
Line 1,985: Line 1,985:
For debugging purposes it it's nice to be able to print all the possible combinations of answers/solutions.
For debugging purposes it it's nice to be able to print all the possible combinations of answers/solutions.


<syntaxhighlight lang="picat">%
<lang Picat>%
% Go through all combinations of answers
% Go through all combinations of answers
%
%
Line 2,006: Line 2,006:
nl
nl
end,
end,
nl.</lang>
nl.</syntaxhighlight>


{{out}}
{{out}}
Line 2,040: Line 2,040:
=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
We allow ourselves a luxurious user interface:
We allow ourselves a luxurious user interface:
<lang PicoLisp>(de yes? (Cond)
<syntaxhighlight lang="picolisp">(de yes? (Cond)
(out NIL (prin (car Cond) "? "))
(out NIL (prin (car Cond) "? "))
(in NIL
(in NIL
Line 2,049: Line 2,049:
T )
T )
(T (member Reply '(NIL N NO No n no false 0)))
(T (member Reply '(NIL N NO No n no false 0)))
(prinl "Please answer 'Yes' or 'No'") ) ) ) )</lang>
(prinl "Please answer 'Yes' or 'No'") ) ) ) )</syntaxhighlight>
The decision table used in the example:
The decision table used in the example:
<lang PicoLisp>(de *Conditions
<syntaxhighlight lang="picolisp">(de *Conditions
("Printer does not print" T T T T NIL NIL NIL NIL)
("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)
("A red light is flashing" T T NIL NIL T T NIL NIL)
Line 2,061: Line 2,061:
("Ensure printer software is installed" T NIL T NIL T NIL T)
("Ensure printer software is installed" T NIL T NIL T NIL T)
("Check/replace ink" T T NIL NIL T T)
("Check/replace ink" T T NIL NIL T T)
("Check for paper jam" NIL T NIL T) )</lang>
("Check for paper jam" NIL T NIL T) )</syntaxhighlight>
The decision can be made directly on the condition and action data, without the need to create intermediate tables:
The decision can be made directly on the condition and action data, without the need to create intermediate tables:
<lang PicoLisp>(de decide ()
<syntaxhighlight lang="picolisp">(de decide ()
(let Reply (mapcar yes? *Conditions)
(let Reply (mapcar yes? *Conditions)
(extract and
(extract and
Line 2,070: Line 2,070:
(unless (pick '((Flg) (<> Flg (next))) Reply)
(unless (pick '((Flg) (<> Flg (next))) Reply)
(rest) ) ) )
(rest) ) ) )
(mapcar car *Actions) ) ) )</lang>
(mapcar car *Actions) ) ) )</syntaxhighlight>
Output:
Output:
<pre>: (decide)
<pre>: (decide)
Line 2,091: Line 2,091:


=={{header|Python}}==
=={{header|Python}}==
<lang python>
<syntaxhighlight lang="python">
'''
'''
Create a Decision table then use it
Create a Decision table then use it
Line 2,130: Line 2,130:
dt_user(dt)
dt_user(dt)
dt_user(dt)
dt_user(dt)
dt_user(dt)</lang>
dt_user(dt)</syntaxhighlight>


'''Sample Run'''
'''Sample Run'''
Line 2,187: Line 2,187:
I thought it might be fun to merge them.
I thought it might be fun to merge them.


<lang racket>#lang unstable/2d racket
<syntaxhighlight lang="racket">#lang unstable/2d racket


(define (ask-y/n q)
(define (ask-y/n q)
Line 2,274: Line 2,274:
╚═╩════════════╩════════════════════════════════════════════╩═╩═╩═╩═╩═══════╝)
╚═╩════════════╩════════════════════════════════════════════╩═╩═╩═╩═╩═══════╝)


(run-decision-table printer-troubleshooting-2dtable)</lang>
(run-decision-table printer-troubleshooting-2dtable)</syntaxhighlight>


{{out}}
{{out}}
Line 2,286: Line 2,286:
=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<lang perl6>sub decide (@q, @s) {
<syntaxhighlight lang="raku" line>sub decide (@q, @s) {
my $bit = 2 ** [+] (1,2,4...*) Z* reverse @q.map: {
my $bit = 2 ** [+] (1,2,4...*) Z* reverse @q.map: {
so prompt(.value ~ "? ") ~~ /:i ^y/;
so prompt(.value ~ "? ") ~~ /:i ^y/;
Line 2,308: Line 2,308:
);
);
say '';
say '';
}</lang>
}</syntaxhighlight>
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
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
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 2,347: Line 2,347:
::* &nbsp; visual fidelity aids for postmortem analysis (as in logs)
::* &nbsp; visual fidelity aids for postmortem analysis (as in logs)
::* &nbsp; a method of allowing the user to quit (opt-out of) the interrogation
::* &nbsp; a method of allowing the user to quit (opt-out of) the interrogation
<lang rexx>/*REXX program demonstrates a (query) decision table and possible corrective actions.*/
<syntaxhighlight lang="rexx">/*REXX program demonstrates a (query) decision table and possible corrective actions.*/
Q.=; Q.1 = 'Does the printer not print?'
Q.=; Q.1 = 'Does the printer not print?'
Q.2 = 'Is there a red light flashing on the printer?'
Q.2 = 'Is there a red light flashing on the printer?'
Line 2,397: Line 2,397:
if (abbrev('YES', u) | abbrev("NO", u)) & words(x)==1 then return u1
if (abbrev('YES', u) | abbrev("NO", u)) & words(x)==1 then return u1
say 'invalid response: ' x; oops= 1
say 'invalid response: ' x; oops= 1
end /*forever*/</lang>
end /*forever*/</syntaxhighlight>
{{out|output|text=&nbsp; (a screen scraping using a DOS prompt window for some of the possible responses):}}
{{out|output|text=&nbsp; (a screen scraping using a DOS prompt window for some of the possible responses):}}


Line 2,493: Line 2,493:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Decision tables
# Project : Decision tables


Line 2,528: Line 2,528:
see "invalid input: " + upper(notprinting) + " " + upper(flashing) + " " + upper(notrecognized) + nl
see "invalid input: " + upper(notprinting) + " " + upper(flashing) + " " + upper(notrecognized) + nl
ok
ok
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 2,540: Line 2,540:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>class DecisionTable
<syntaxhighlight lang="ruby">class DecisionTable
def initialize(conditions, actions)
def initialize(conditions, actions)
@conditions = conditions
@conditions = conditions
Line 2,592: Line 2,592:
]
]
)
)
loop {dt.run}</lang>
loop {dt.run}</syntaxhighlight>


Example
Example
Line 2,659: Line 2,659:
=={{header|Scala}}==
=={{header|Scala}}==
{{trans|Java}}
{{trans|Java}}
<lang scala>import java.io.{BufferedReader, InputStreamReader}
<syntaxhighlight lang="scala">import java.io.{BufferedReader, InputStreamReader}


import scala.util.control.Breaks
import scala.util.control.Breaks
Line 2,734: Line 2,734:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Please answer the following questions with a y or n:
<pre>Please answer the following questions with a y or n:
Line 2,746: Line 2,746:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Raku}}
{{trans|Raku}}
<lang ruby>func decide (q, s) {
<syntaxhighlight lang="ruby">func decide (q, s) {


var bits = q.map { |p|
var bits = q.map { |p|
Line 2,781: Line 2,781:
)
)
say ''
say ''
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,810: Line 2,810:


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>package require TclOO
<syntaxhighlight lang="tcl">package require TclOO


#http://rosettacode.org/wiki/Keyboard_Input/Obtain_a_Y_or_N_response#Tcl
#http://rosettacode.org/wiki/Keyboard_Input/Obtain_a_Y_or_N_response#Tcl
Line 2,849: Line 2,849:
}
}
}
}
}</lang>
}</syntaxhighlight>
Demonstration:
Demonstration:
<lang tcl>DecisionTable create printerDiagnosisTable {
<syntaxhighlight lang="tcl">DecisionTable create printerDiagnosisTable {
"Printer does not print"
"Printer does not print"
"A red light is flashing"
"A red light is flashing"
Line 2,862: Line 2,862:
"Check for paper jam" {0 1 0 1}
"Check for paper jam" {0 1 0 1}
}
}
printerDiagnosisTable consult</lang>
printerDiagnosisTable consult</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 2,875: Line 2,875:
{{trans|Kotlin}}
{{trans|Kotlin}}
{{libheader|Wren-str}}
{{libheader|Wren-str}}
<lang ecmascript>import "io" for Stdin, Stdout
<syntaxhighlight lang="ecmascript">import "io" for Stdin, Stdout
import "/str" for Str
import "/str" for Str


Line 2,928: Line 2,928:
break
break
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}