Abbreviations, simple: Difference between revisions

→‎{{header|REXX}}: ooRexx conformance and readabie
(→‎{{header|REXX}}: ooRexx conformance and readabie)
 
(8 intermediate revisions by 5 users not shown)
Line 94:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V command_table_text =
|‘add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3
compress 4 copy 2 count 3 Coverlay 3 cursor 3 delete 3 Cdelete 2 down 1 duplicate
Line 148:
 
print(‘user words: ’user_words)
print(‘full words: ’full_words)</langsyntaxhighlight>
 
{{out}}
Line 158:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program abbrSimple64.s */
Line 587:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
<pre>
Enter command (or <ctrl-c> to stop) : riG
Line 614:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Characters.Handling;
with Ada.Containers.Vectors;
with Ada.Strings.Fixed;
Line 743:
Put_Match ("poweRin");
Put_Match ("");
end Abbreviations_Simple;</langsyntaxhighlight>
 
{{out}}
Line 762:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<langsyntaxhighlight lang="algol68"># "Simple" abbreviations #
 
# returns the next word from text, updating pos #
Line 891:
 
# task test cases #
test expand( "riG rePEAT copies put mo rest types fup. 6 poweRin" )</langsyntaxhighlight>
{{out}}
<pre>
Line 907:
<pre> Ok correction le 17/11/2020 16H </pre>
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program abbrSimple.s */
Line 1,317:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
<pre>
Enter command (or <ctrl-c> to stop) : riG
Line 1,343:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">table := "
(
add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3
Line 1,376:
}
return Trim(result, " ")
}</langsyntaxhighlight>
{{out}}
<pre>RIGHT REPEAT *error* PUT MOVE RESTORE *error* *error* *error* POWERINPUT</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
Line 1,530:
free_command_list(commands);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 1,539:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <cctype>
#include <iostream>
Line 1,655:
std::cout << "output: " << output << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 1,666:
NOTE: Type Hints have been added here to indicate the types of function arguments and to make the program faster. They are not strictly necessary, since Clojure is a dynamically typed language (i.e., variable types are determined at runtime).
 
<langsyntaxhighlight lang="clojure">
(defn words
"Split string into words"
Line 1,733:
;; Print solution for given test case
(println (solution "riG rePEAT copies put mo rest types fup. 6 poweRin"))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,739:
 
=={{header|Crystal}}==
<langsyntaxhighlight lang="crystal">COMMAND_TABLE =
"add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3
compress 4 copy 2 count 3 Coverlay 3 cursor 3 delete 3 Cdelete 2 down 1 duplicate
Line 1,790:
puts "Output:"
puts parse_user_input(user_input, cmds)
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,801:
 
=={{header|D}}==
<langsyntaxhighlight Dlang="d">class Abbreviations
{
import std.array: split, join;
Line 1,864:
writeln("Output: ", new Abbreviations(table).expand(input));
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,876:
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Abraviation_simple;
 
Line 1,982:
 
Readln;
end.</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: arrays assocs combinators formatting fry grouping.extras
kernel literals math math.parser multiline sequences
splitting.extras unicode ;
Line 2,048:
: main ( -- ) input "" [ show-commands ] bi@ ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 2,063:
Needs the FMS-SI (single inheritance) library code located here:
http://soton.mpeforth.com/flag/fms/index.html
<langsyntaxhighlight lang="forth">include FMS-SI.f
include FMS-SILib.f
 
Line 2,118:
run RIGHT REPEAT *error* PUT MOVE RESTORE *error* *error* *error* POWERINPUT ok
 
</syntaxhighlight>
</lang>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">_window = 1
begin enum 1
_userStringFld
_validateBtn
_resultsStringFld
end enum
 
void local fn BuildWindow
window _window, @"Abbreviations, simple", (0,0,600,268)
WindowSetContentMinSize( _window, fn CGSizeMake( 200, 268 ) )
WindowSetContentMaxSize( _window, fn CGSizeMake( 10000, 268 ) )
textfield _userStringFld,, @"riG rePEAT copies put mo rest types fup. 6 poweRin", (20,152,560,96)
TextFieldSetPlaceholderString( _userStringFld, @"Enter commands" )
ViewSetAutoresizingMask( _userStringFld, NSViewWidthSizable )
button _validateBtn,,, @"Validate", (259,117,83,32)
ViewSetAutoresizingMask( _validateBtn, NSViewMinXMargin + NSViewMaxXMargin )
textfield _resultsStringFld,,, (20,20,560,96)
TextFieldSetEditable( _resultsStringFld, NO )
TextFieldSetSelectable( _resultsStringFld, YES )
ViewSetAutoresizingMask( _resultsStringFld, NSViewWidthSizable )
end fn
 
local fn Commands as CFArrayRef
CFStringRef cmd, string
long abbrLen
CFMutableArrayRef commands = fn MutableArrayWithCapacity(0)
ScannerRef scanner
string = @" add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3"
string = fn StringByAppendingString( string, @" compress 4 copy 2 count 3 Coverlay 3 cursor 3 delete 3 Cdelete 2 down 1 duplicate" )
string = fn StringByAppendingString( string, @" 3 xEdit 1 expand 3 extract 3 find 1 Nfind 2 Nfindup 6 NfUP 3 Cfind 2 findUP 3 fUP 2" )
string = fn StringByAppendingString( string, @" forward 2 get help 1 hexType 4 input 1 powerInput 3 join 1 split 2 spltJOIN load" )
string = fn StringByAppendingString( string, @" locate 1 Clocate 2 lowerCase 3 upperCase 3 Lprefix 2 macro merge 2 modify 3 move 2" )
string = fn StringByAppendingString( string, @" msg next 1 overlay 1 parse preserve 4 purge 3 put putD query 1 quit read recover 3" )
string = fn StringByAppendingString( string, @" refresh renum 3 repeat 3 replace 1 Creplace 2 reset 3 restore 4 rgtLEFT right 2 left" )
string = fn StringByAppendingString( string, @" 2 save set shift 2 si sort sos stack 3 status 4 top transfer 3 type 1 up 1" )
scanner = fn ScannerWithString( string )
while ( fn ScannerIsAtEnd( scanner ) == NO )
if ( fn ScannerScanUpToCharactersFromSet( scanner, fn CharacterSetWhitespaceAndNewlineSet, @cmd ) )
abbrLen = 0
fn ScannerScanInteger( scanner, @abbrLen )
MutableArrayAddObject( commands, @{@"cmd":cmd,@"len":@(abbrLen)} )
end if
wend
end fn = commands
 
void local fn Validate
CFArrayRef commands, words
CFStringRef userString, result, wd, cmd
long wordCount, i, wordLen, abbrLen
CFMutableStringRef results
CFDictionaryRef dict
BOOL found
commands = fn Commands
userString = fn ControlStringValue( _userStringFld )
words = fn StringComponentsSeparatedByCharactersInSet( userString, fn CharacterSetWhitespaceAndNewlineSet )
results = fn MutableStringWithCapacity( 0 )
wordCount = len( words )
for i = 0 to wordCount - 1
found = NO
result = @"*error* "
wd = words[i]
wordLen = len( wd )
if ( wordLen )
for dict in commands
cmd = dict[@"cmd"]
abbrLen = fn NumberIntegerValue(dict[@"len"])
if ( abbrLen != 0 and wordLen >= abbrLen )
found = fn StringHasPrefix( lcase( cmd ), lcase( wd ) )
else
found = fn StringIsEqual( lcase( cmd ), lcase( wd ) )
end if
if ( found )
result = fn StringWithFormat( @"%@ ",ucase( cmd ) )
break
end if
next
MutableStringAppendString( results, result )
end if
next
ControlSetStringValue( _resultsStringFld, results )
end fn
 
 
void local fn DoDialog( ev as long, tag as long )
select ( ev )
case _btnClick : fn Validate
end select
end fn
 
 
editmenu 1
fn BuildWindow
 
on dialog fn DoDialog
 
HandleEvents</syntaxhighlight>
 
{{out}}
<pre>
user string: riG rePEAT copies put mo rest types fup. 6 poweRin
results string: RIGHT REPEAT *error* PUT MOVE RESTORE *error* *error* *error* POWERINPUT
</pre>
 
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 2,212 ⟶ 2,330:
printResults(words, results)
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,222 ⟶ 2,340:
=={{header|Haskell}}==
{{Trans|Python}}
<langsyntaxhighlight lang="haskell">import Data.List (find, isPrefixOf)
import Data.Char (isDigit, toUpper)
import Data.Maybe (maybe)
Line 2,267 ⟶ 2,385:
unAbbrev
"riG rePEAT copies put mo rest types fup. 6 poweRin"
print $ unAbbrev ""</langsyntaxhighlight>
{{Out}}
<pre>"RIGHT REPEAT *error PUT MOVE RESTORE *error *error *error POWERINPUT"
Line 2,273 ⟶ 2,391:
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j">ctable=:|:(({.;~{:@(_,".)@;@}.);.1~ _2<nc) cut {{)n
add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3
compress 4 copy 2 count 3 Coverlay 3 cursor 3 delete 3 Cdelete 2 down 1 duplicate
Line 2,291 ⟶ 2,409:
end.
'*error*'
}}L:0</langsyntaxhighlight>
 
Task:
<langsyntaxhighlight Jlang="j"> ;:inv findmatch cut ' riG rePEAT copies put mo rest types fup. 6 poweRin'
RIGHT REPEAT *error* PUT MOVE RESTORE *error* *error* *error* POWERINPUT</langsyntaxhighlight>
 
=={{header|Java}}==
{{Trans|C++}}
<langsyntaxhighlight lang="java">import java.util.*;
 
public class Abbreviations {
Line 2,390 ⟶ 2,508:
private List<Command> commands = new ArrayList<>();
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,401 ⟶ 2,519:
{{Trans|Haskell}}
{{Trans|Python}}
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 2,643 ⟶ 2,761:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>Abbreviation tests:
Line 2,651 ⟶ 2,769:
'' ->
''</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq, and with fq.'''
 
''' Adapted from [[#Wren|Wren]]
<syntaxhighlight lang=jq>
def table:
"add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3 " +
"compress 4 copy 2 count 3 Coverlay 3 cursor 3 delete 3 Cdelete 2 down 1 duplicate " +
"3 xEdit 1 expand 3 extract 3 find 1 Nfind 2 Nfindup 6 NfUP 3 Cfind 2 findUP 3 fUP 2 " +
"forward 2 get help 1 hexType 4 input 1 powerInput 3 join 1 split 2 spltJOIN load " +
"locate 1 Clocate 2 lowerCase 3 upperCase 3 Lprefix 2 macro merge 2 modify 3 move 2 " +
"msg next 1 overlay 1 parse preserve 4 purge 3 put putD query 1 quit read recover 3 " +
"refresh renum 3 repeat 3 replace 1 Creplace 2 reset 3 restore 4 rgtLEFT right 2 left " +
"2 save set shift 2 si sort sos stack 3 status 4 top transfer 3 type 1 up 1"
;
 
# Input: {commands, minLens}
# Output: array of expansions or error markers corresponding to $words
def validate($words):
.commands as $commands
| .minLens as $minLens
| [ $words[] as $word
| ($word|length) as $wlen
| first( range(0; $commands|length) as $i
| $commands[$i]
| select($minLens[$i] != 0 and $wlen >= $minLens[$i] and $wlen <= length)
| ascii_upcase
| select(startswith(($word|ascii_upcase))) )
// "*error*" ];
 
 
# Output: {commands, minLens} corresponding to the $table string
def commands($table):
[$table|splits(" *")] as $split_table
| ($split_table|length) as $slen
| {commands:[], minLens:[], i:0}
| until(.found;
.commands += [ $split_table[.i] ]
| ($split_table[.i]|length) as $len
| if (.i == $slen - 1)
then .minLens += [$len]
| .found = true
else .
end
| .i += 1
| ($split_table[.i] | try (tonumber) // null) as $num
| if ($num != null)
then .minLens += [ if ($num < $len) then $num else $len end ]
| .i += 1
| if (.i == $slen) then .found = true else . end
else .minLens += [$len]
end );
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def task($sentence):
[$sentence | splits(" *")] as $words
| commands(table)
| validate($words)
| $words, .
| map(lpad(10))
| join(" ") ;
 
task("riG rePEAT copies put mo rest types fup. 6 poweRin")
</syntaxhighlight>
{{output}}
'''Invocation''' jq -rnf abbreviations-simple.jq
</pre>
riG rePEAT copies put mo rest types fup. 6 poweRin
RIGHT REPEAT *error* PUT MOVE RESTORE *error* *error* *error* POWERINPUT
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">
const commandtable = """
add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3
Line 2,699 ⟶ 2,890:
 
teststring("riG rePEAT copies put mo rest types fup. 6 poweRin")
</langsyntaxhighlight> {{output}} <pre>
riG rePEAT copies put mo rest types fup. 6 poweRin
RIGHT REPEAT *error* PUT MOVE RESTORE *error* *error* *error* POWERINPUT
Line 2,705 ⟶ 2,896:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="kotlin">import java.util.Locale
 
private const val table = "" +
Line 2,758 ⟶ 2,949:
println("full words: ${results.joinToString(" ")}")
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,767 ⟶ 2,958:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">abbr = {
define = function(self, cmdstr)
local cmd
Line 2,803 ⟶ 2,994:
local input = "riG rePEAT copies put mo rest types fup. 6 poweRin"
print("Input:", input)
print("Output:", abbr:expand(input))</langsyntaxhighlight>
{{out}}
<pre>Input: riG rePEAT copies put mo rest types fup. 6 poweRin
Line 2,812 ⟶ 3,003:
Object Queue is a list (using a hash table) which can hold same keys (strings or numbers or mix of them). Always the Exist(aQueuePointer, "word") set the internal index to the last key with name "word" if return true. We read the item in that index using Eval$(aQueuePointer) for string value, or Eval(aQueuePointer) for aritmetic value. We can't delete keys from anywhere, but only from the last entries using Drop statement to drop a number of keys. Here we didn't use drop, we have only a table of words.
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Abbreviations_Simple {
Function Lex {
Line 2,869 ⟶ 3,060:
}
Abbreviations_Simple
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,879 ⟶ 3,070:
Here we use just a string to hold the words and the Instr() function to search for each abbreviation.
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Abbreviations_Simple_2 {
Function Lex$ {
Line 2,946 ⟶ 3,137:
}
Abbreviations_Simple_2
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,955 ⟶ 3,146:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[ct, FunctionMatchQ, ValidFunctionQ, ProcessString]
ct = "add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3
compress 4 copy 2 count 3 Coverlay 3 cursor 3 delete 3 Cdelete 2 down 1 duplicate
Line 2,990 ⟶ 3,181:
StringRiffle[ValidFunctionQ /@ parts, " "]
]
ProcessString["riG rePEAT copies put mo rest types fup. 6 poweRin"]</langsyntaxhighlight>
{{out}}
<pre>"RIGHT REPEAT *error* PUT MOVE RESTORE *error* *error* *error* POWERINPUT"</pre>
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">c = "add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3" +
" compress 4 copy 2 count 3 Coverlay 3 cursor 3 delete 3 Cdelete 2 down 1 duplicate" +
" 3 xEdit 1 expand 3 extract 3 find 1 Nfind 2 Nfindup 6 NfUP 3 Cfind 2 findUP 3 fUP 2" +
Line 3,036 ⟶ 3,227:
output.push check(word)
end for
print output.join</langsyntaxhighlight>
 
{{out}}
Line 3,044 ⟶ 3,235:
{{Trans|Python}}
Adapted from Python version with several modifications.
<syntaxhighlight lang="nim">
<lang Nim>
import parseutils
import strutils
Line 3,120 ⟶ 3,311:
echo ""
break
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,132 ⟶ 3,323:
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">open String
 
let table_as_string =
Line 3,146 ⟶ 3,337:
replace 1 Creplace 2 reset 3 restore 4 rgtLEFT right 2 left 2 \
save set shift 2 si sort sos stack 3 status 4 top \
transfer 3 type 1 up 1"</langsyntaxhighlight>
 
The interesting part below is the '''compare''' function.
Line 3,154 ⟶ 3,345:
but for the latter, we need to ensure that the word corresponds to the abbreviation
(the abbreviation is a substring of the word and the abbreviation length is sufficient).
<langsyntaxhighlight lang="ocaml">module Entry = struct
type t = { word : string ; min : int }
let compare e1 e2 =
Line 3,164 ⟶ 3,355:
end
 
module Table = Set.Make(Entry)</langsyntaxhighlight>
 
The few functions below are used to build the table from the string at the beginning.
<langsyntaxhighlight lang="ocaml">let clean_strings strs =
List.filter (fun w -> w <> "" && w <> " ") strs
 
Line 3,187 ⟶ 3,378:
split_on_char ' ' table_as_string
|> clean_strings
|> split</langsyntaxhighlight>
 
Finally, here is the function looking for a word :
<langsyntaxhighlight lang="ocaml">let abbrev (table:Table.t) (w:string) : string =
let w = uppercase_ascii w in
try
Line 3,205 ⟶ 3,396:
let inputs = ["riG";"rePEAT";"copies";"put";"mo";"rest";"types";"fup.";"6";"poweRin"] in
check table inputs;
exit 0</langsyntaxhighlight>
 
{{out}}
Line 3,213 ⟶ 3,404:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">@c = (uc join ' ', qw<
add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3
compress 4 copy 2 count 3 Coverlay 3 cursor 3 delete 3 Cdelete 2 down 1 duplicate
Line 3,241 ⟶ 3,432:
}
 
print "$inp\n$out\n";</langsyntaxhighlight>
{{out}}
<pre>Input: riG rePEAT copies put mo rest types fup. 6 poweRin
Line 3,248 ⟶ 3,439:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">abbrtxt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3
Line 3,297 ⟶ 3,488:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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;">"\n"</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 3,306 ⟶ 3,497:
====Procedural====
{{works with|Python|3.6}}
<langsyntaxhighlight lang="python">
 
command_table_text = """add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3
Line 3,368 ⟶ 3,559:
print("user words:", user_words)
print("full words:", full_words)
</syntaxhighlight>
</lang>
{{out}}
<pre>input: ""
Line 3,379 ⟶ 3,570:
====Composition of pure functions====
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Simple abbreviations'''
 
from functools import reduce
Line 3,549 ⟶ 3,740:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Simple abbreviations:
Line 3,559 ⟶ 3,750:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
(require srfi/13)
 
Line 3,600 ⟶ 3,791:
(validate-string "riG rePEAT copies put mo rest types fup. 6 poweRin")
"RIGHT REPEAT *error* PUT MOVE RESTORE *error* *error* *error* POWERINPUT")
(check-equal? (validate-string "") ""))</langsyntaxhighlight>
{{out}}
<pre>input: ""
Line 3,614 ⟶ 3,805:
Demonstrate that inputting an empty string returns an empty string in addition to the required test input.
 
<syntaxhighlight lang="raku" perl6line><
add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3
compress 4 copy 2 count 3 Coverlay 3 cursor 3 delete 3 Cdelete 2 down 1 duplicate
Line 3,637 ⟶ 3,828:
put ' Input: ', $str;
put 'Output: ', join ' ', $str.words.map: &abbr-simple;
}</langsyntaxhighlight>
{{out}}
<pre> Input: riG rePEAT copies put mo rest types fup. 6 poweRin
Line 3,645 ⟶ 3,836:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program validates a user "word" against a "command table" with abbreviations.*/
parseParse argArg uw /*obtain optional arguments from the CL*/
ifIf uw='' thenThen uw= 'riG rePEAT copies put mo rest types fup. 6 poweRin'
saySay 'user words: ' uw
 
@keyws= 'add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3',
'compress 4 copy 2 count 3 Coverlay 3 cursor 3 delete 3 Cdelete 2 down 1 duplicate',
'3 xEdit 1 expand 3 extract 3 find 1 Nfind 2 Nfindup 6 NfUP 3 Cfind 2 findUP 3 fUP 2',
'forward 2 get help 1 hexType 4 input 1 powerInput 3 join 1 split 2 spltJOIN load',
'locate 1 Clocate 2 lowerCase 3 upperCase 3 Lprefix 2 macro merge 2 modify 3 move 2',
'msg next 1 overlay 1 parseParse preserve 4 purge 3 put putD query 1 quit read recover 3',
'refresh renum 3 repeat 3 replace 1 Creplace 2 reset 3 restore 4 rgtLEFT right 2 left',
'2 save set shift 2 si sort sos stack 3 status 4 top transfer 3 type 1 up 1'
 
saySay 'full words: ' validate(uw) /*display the result(s) to the terminal*/
exitExit /*stick a fork in it, we're all done. */
/*--------------------------------------------------------------------------------------*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
validate: Procedure Expose keyws
validate: procedure expose @; arg x; upper @ /*ARG capitalizes all the X words. */
keyws=translate(keyws)
$= /*initialize the return string to null.*/
Arg userwords do j=1 to words(x); _=word(x, j) /*obtain a word from the X list./*ARG capitalizes all the userwords. */
res='' do k=1 to words(@); a=word(@, k) /*get a legitmate command name from @ /*initialize the Return string to null.*/
Do j=1 to words(userwords) L=word(@, k+1) /* loop over userwords /*··· and maybe get it's abbrev length.*/
uword=word(userwords,j) if datatype(L, 'W') then k=k + 1/*obtain a word from the userword /*yuppers, it's an abbrev lengthlist.*/
Do k=1 to words(keyws) /* loop over keywords else L=length(a) /*nope, it can't be abbreviated.*/
kw=word(keyws,k) if abbrev(a, _, L) then do; $=$ a; iterate/*get j;a legitmate endcommand name /*is validfrom abbrev?keyws.*/
L=word(keyws,k+1) end /*k··· and maybe get its abbrev length.*/
If $=$ datatype(L,'*error*W') Then /* it's a number - /*processedan theabbrev wholelength. list, not valid. */
k=k + 1 end /*j skip it for next kw */
Else return strip($) /*elide theotherwise superfluous leading blank. */</lang>
L=length(kw) /* it can't be abbreviated. */
If abbrev(kw,uword,L) Then Do /* is valid abbreviation */
res=res kw /* add to result string */
Iterate j /* proceed with next userword */
End
End
res=res '*error*' /*processed the whole list, not valid */
End
Return strip(res) /* elide superfluous leading blank. */
</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 3,681 ⟶ 3,882:
 
=={{header|Ruby}}==
<langsyntaxhighlight Rubylang="ruby">str = "add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3
compress 4 copy 2 count 3 Coverlay 3 cursor 3 delete 3 Cdelete 2 down 1 duplicate
3 xEdit 1 expand 3 extract 3 find 1 Nfind 2 Nfindup 6 NfUP 3 Cfind 2 findUP 3 fUP 2
Line 3,703 ⟶ 3,904:
 
puts ar.join(" ")
</syntaxhighlight>
</lang>
{{out}}
<pre>RIGHT REPEAT *error* PUT MOVE RESTORE *error* *error* *error* POWERINPUT
Line 3,711 ⟶ 3,912:
{{works with|Rust|1.35.0}}
cargo clippy and cargo fmt run against it
<langsyntaxhighlight lang="rust">use std::collections::HashMap;
 
// The plan here is to build a hashmap of all the commands keyed on the minimum number of
Line 3,821 ⟶ 4,022:
let correct_output = "RIGHT REPEAT *error* PUT MOVE RESTORE *error* *error* *error* POWERINPUT";
assert_eq!(output_text, correct_output)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,829 ⟶ 4,030:
 
=={{header|Scala}}==
<syntaxhighlight lang="scala">
<lang Scala>
object Main extends App {
implicit class StrOps(i: String) {
Line 3,875 ⟶ 4,076:
println(resultLine)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,883 ⟶ 4,084:
=={{header|SNOBOL4}}==
{{works with|SNOBOL4, SPITBOL for Linux}}
<syntaxhighlight lang="snobol4">
<lang SNOBOL4>
* Program: abbr_simple.sbl
* To run: sbl abbr_simple.sbl
Line 3,945 ⟶ 4,146:
 
END
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,957 ⟶ 4,158:
=={{header|Tcl}}==
 
<langsyntaxhighlight lang="tcl">proc appendCmd {word} {
# Procedure to append the correct command from the global list ::cmds
# for the word given as parameter to the global list ::result.
Line 3,995 ⟶ 4,196:
 
puts "user words: $words"
puts $result</langsyntaxhighlight>
{{out}}<pre>./abbreviations_simple.tcl
user words: riG rePEAT copies put mo rest types fup. 6 poweRin
Line 4,002 ⟶ 4,203:
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Private Function ValidateUserWords(userstring As String) As String
Dim s As String
Dim user_words() As String
Line 4,060 ⟶ 4,261:
Debug.Print "full words:", ValidateUserWords(guserstring)
End Sub
</syntaxhighlight>
</lang>
{{out}}<pre>user words: riG rePEAT copies put mo rest types fup. 6 poweRin
full words: RIGHT REPEAT *error* PUT MOVE RESTORE *error* *error* *error* POWERINPUT
Line 4,066 ⟶ 4,267:
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
option explicit
 
Line 4,156 ⟶ 4,357:
test
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,163 ⟶ 4,364:
</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">import encoding.utf8
import strconv
fn read_table(table string) ([]string, []int) {
Line 4,239 ⟶ 4,440:
print_results(words, results)
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 4,251 ⟶ 4,452:
{{libheader|Wren-str}}
Based on an older version of the Go entry.
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
import "./str" for Str
 
var table =
Line 4,319 ⟶ 4,520:
}
System.write("\nfull words: ")
System.print(results.join(" "))</langsyntaxhighlight>
 
{{out}}
Line 4,328 ⟶ 4,529:
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">c$ = "add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3"
c$ = c$ + " compress 4 copy 2 count 3 Coverlay 3 cursor 3 delete 3 Cdelete 2 down 1 duplicate"
c$ = c$ + " 3 xEdit 1 expand 3 extract 3 find 1 Nfind 2 Nfindup 6 NfUP 3 Cfind 2 findUP 3 fUP 2"
Line 4,376 ⟶ 4,577:
next
 
print r$</langsyntaxhighlight>
 
{{out}}
Line 4,384 ⟶ 4,585:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">commands:=Data(0,String, // "add\01\0alter\0..."
#<<<
"add 1 alter 3 backup 2 bottom 1 Cappend 2 change 1 Schange Cinsert 2 Clast 3
Line 4,417 ⟶ 4,618:
n+=c.len();
}
}).concat(" ").println();</langsyntaxhighlight>
{{out}}
<pre>
2,289

edits