Range expansion: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
(Added solution for Action!) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(9 intermediate revisions by 7 users not shown) | |||
Line 17:
=={{header|11l}}==
{{trans|Python}}
<
[Int] lst
L(r) txt.split(‘,’)
Line 27:
R lst
print(rangeexpand(‘-6,-3--1,3-5,7-11,14,15,17-20’))</
=={{header|8th}}==
<
\ range, inclusive:
: n:gen-range \ low hi -- a
Line 90:
n:range-expand
\ print the expanded list:
. cr bye</
{{out}}
<pre>
Line 97:
=={{header|Action!}}==
<
BYTE i
Line 168:
RangeExtraction("-6,-3--1,3-5,7-11,14,15,17-20",res,@size)
PrintArray(res,size)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Range_expansion.png Screenshot from Atari 8-bit computer]
Line 178:
The function Expand takes a string and returns a corresponding array of integers.
Upon syntax errors Constraint_Error is propagated:
<
procedure Test_Range_Expansion is
type Sequence is array (Positive range <>) of Integer;
Line 243:
begin
Put (Expand ("-6,-3--1,3-5,7-11,14,15,17-20"));
end Test_Range_Expansion;</
{{out}}
<pre>
Line 251:
=={{header|Aime}}==
{{incorrect|Aime|Needs "a comma separated list" without the trailing comma}}
<
file().b_affix("-6,-3--1,3-5,7-11,14,15,17-20").news(l, 0, 0, ",");
Line 270:
}
o_("\n");</
or:
<
list l;
Line 285:
}
o_("\n");</
{{out}}
<pre>-6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20,</pre>
Line 293:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny] - string parsing and formatting code tested with 2.6.win32}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d]}}
<
MODE RANGE = STRUCT(INT lwb, upb);
Line 462:
print( ( TOSTRING range expand( TORANGE "-6,-3--1,3-5,7-11,14,15,17-20" ), newline ) )
)
</syntaxhighlight>
{{out}}
<pre>
Line 470:
=={{header|APL}}==
{{works with|Dyalog APL}}
<
aplnum←{⍎('¯',⎕D)[('-',⎕D)⍳⍵]}
∊{ 0::('Invalid range: ''',⍵,'''')⎕SIGNAL 11
Line 478:
(s+(⍳e-s-1))-1
}¨(⍵≠',')⊆⍵
}</
{{out}}
Line 496:
===Functional===
{{Trans|JavaScript}} (Functional ES5 version)
<
-- and these integer lists are concatenated together into a single list
Line 651:
item 1 of tuple
end if
end tupleRange</
{{Out}}
<
----
===Idiomatic===
<
-- Split the expression at the commas, if any.
set astid to AppleScript's text item delimiters
Line 688:
-- Demo code:
set rangeExpression to "-6,-3--1,3-5,7-11,14,15,17-20"
return rangeExpansion(rangeExpression)</
{{output}}
<
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">expandRange: function [rng][
flatten @ to :block
join.with:" " map split.by:"," rng 'x ->
replace replace replace x
{/^\-(\d+)/} "(neg $1)" {/\-\-(\d+)/}
"-(neg $1)" "-" ".."
]
print expandRange {-6,-3--1,3-5,7-11,14,15,17-20}</syntaxhighlight>
{{out}}
<pre>-6 -3 -2 -1 3 4 5 7 8 9 10 11 14 15 17 18 19 20</pre>
=={{header|AutoHotkey}}==
<
expand( range ) {
Line 702 ⟶ 718:
ret .= "," (A_Index-1) + f1
return SubStr(ret, 2)
}</
=={{header|AWK}}==
<
BEGIN { FS=","; }
Line 727 ⟶ 743:
}
return;
} </
<pre>
Line 736 ⟶ 752:
=={{header|BBC BASIC}}==
<
END
Line 758 ⟶ 774:
ENDIF
UNTIL i% = 0
= r$</
{{out}}
<pre>
Line 765 ⟶ 781:
=={{header|Bracmat}}==
<
= a b L
. @( !arg
Line 780 ⟶ 796:
)
& out$(str$(expandRanges$"-6,-3--1,3-5,7-11,14,15,17-20"))
</syntaxhighlight>
{{out}}
<pre>-6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20</pre>
Line 786 ⟶ 802:
=={{header|C}}==
Recursive descent parser.
<
#include <stdlib.h>
#include <ctype.h>
Line 863 ⟶ 879:
return 0;
}</
{{out}}
<pre>-6 -3 -2 -1 3 4 5 7 8 9 10 11 14 15 17 18 19 20
Line 872 ⟶ 888:
=={{header|C sharp|C#}}==
{{works with|C sharp|3.0}}
<
using System.Collections.Generic;
using System.Linq;
Line 901 ⟶ 917:
Console.WriteLine(string.Join(", ", values));
}
}</
{{works with|C sharp|3.5+}}
<
using System.Collections.Generic;
using System.Linq;
Line 934 ⟶ 950:
}
}
}</
=={{header|C++}}==
<
#include <sstream>
#include <iterator>
Line 1,010 ⟶ 1,026:
else
std::cout << "an error occured.";
}</
{{out}}
-6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20
Line 1,016 ⟶ 1,032:
=={{header|Clojure}}==
There is a split method in clojure.contrib, but I don't know if it is able to skip first character to so that <code>(split "-8--8") => (-8 -8)</code>.
<
(defn skipFirst [[x & xs :as s]]
(cond (empty? s) [nil nil]
Line 1,039 ⟶ 1,055:
> (rangeexpand "-6,-3--1,3-5,7-11,14,15,17-20")
(-6 -3 -2 -1 3 4 5 7 8 9 10 11 14 15 17 18 19 20)</
=={{header|COBOL}}==
{{works with|GNU Cobol|2.0}}
<
IDENTIFICATION DIVISION.
PROGRAM-ID. expand-range.
Line 1,135 ⟶ 1,151:
END-IF
.
END PROGRAM display-edited-num.</
Setup:
Line 1,149 ⟶ 1,165:
=={{header|Common Lisp}}==
<
(loop
with prevnum = nil
Line 1,171 ⟶ 1,187:
CL-USER> (expand-ranges "-6,-3--1,3-5,7-11,14,15,17-20")
(-6 -3 -2 -1 3 4 5 7 8 9 10 11 14 15 17 18 19 20)</
=={{header|Cowgol}}==
<
# Callback interface
Line 1,267 ⟶ 1,283:
# Try it on a couple of wrong ones
PrintExpansion("-6-3--1,3-5,7-11,14,15,17-20"); # misformatted range
PrintExpansion("-6,-3--1,5-3,7-11,14,15,17-20"); # numbers not in order</
{{out}}
Line 1,280 ⟶ 1,296:
=={{header|Crystal}}==
{{trans|Ruby}}
<
range.split(',').flat_map do |part|
match = /^(-?\d+)-(-?\d+)$/.match(part)
Line 1,291 ⟶ 1,307:
end
puts range_expand("-6,-3--1,3-5,7-11,14,15,17-20")</
{{out}}
<pre>[-6, -3, -2, -1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]</pre>
=={{header|D}}==
<
enum rangeEx = (string s) /*pure*/ => s.matchAll(`(-?\d+)-?(-?\d+)?,?`)
Line 1,303 ⟶ 1,319:
void main() {
"-6,-3--1,3-5,7-11,14,15,17-20".rangeEx.writeln;
}</
{{out}}
<pre>[-6, -3, -2, -1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]</pre>
Line 1,309 ⟶ 1,325:
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program Range_expansion;
Line 1,379 ⟶ 1,395:
end;
readln;
end.</
=={{header|DWScript}}==
<
function ExpandRanges(ranges : String) : array of Integer;
begin
Line 1,399 ⟶ 1,415:
var expanded := ExpandRanges('-6,-3--1,3-5,7-11,14,15,17-20');
PrintLn(JSON.Stringify(expanded));
</syntaxhighlight>
{{out}}
<pre>[-6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20]</pre>
Line 1,407 ⟶ 1,423:
{{trans|Go}}
<
let input = "-6,-3--1,3-5,7-11,14,15,17-20"
print("range: \(input)")
var r = []
var last = 0
for part in input.
var i = part[1..].
if i == -1 {
var n = Integer(part)
if r.
return print("duplicate value: \(n)") when last == n
return print("values not ordered: \(last) > \(n)") when last > n
}
r.
last = n
} else {
Line 1,426 ⟶ 1,442:
var n2 = Integer(part[(i + 2)..])
return print("invalid range: \(part)") when n2 < n1 + 2
if r.
return print("duplicate value: \(n1)") when last == n1
return print("values not ordered: \(last) > \(n1)") when last > n1
}
for i in n1..n2 {
r.
}
last = n2
Line 1,442 ⟶ 1,458:
}
main()</
{{out}}
Line 1,450 ⟶ 1,466:
=={{header|EchoLisp}}==
<
;; parsing [spaces][-]digit(s)-[-]digit(s)[spaces]
(define R (make-regexp "^ *(\-?\\d+)\-(\-?\\d+) *$" ))
Line 1,476 ⟶ 1,492:
(ranges task)
→ (-6 -3 -2 -1 3 4 5 7 8 9 10 11 14 15 17 18 19 20)
</syntaxhighlight>
=={{header|Elixir}}==
{{trans|Ruby}}
<
def expansion(range) do
Enum.flat_map(String.split(range, ","), fn part ->
Line 1,491 ⟶ 1,507:
end
IO.inspect RC.expansion("-6,-3--1,3-5,7-11,14,15,17-20")</
{{out}}
Line 1,499 ⟶ 1,515:
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
-module( range ).
Line 1,514 ⟶ 1,530:
expansion_individual( {ok, [N], []} ) -> N;
expansion_individual( {ok, [Start], "-" ++ Stop_string} ) -> lists:seq( Start, erlang:list_to_integer(Stop_string) ).
</syntaxhighlight>
{{out}}
Line 1,523 ⟶ 1,539:
=={{header|F_Sharp|F#}}==
<
// simplify regex matching with an active pattern
Line 1,545 ⟶ 1,561:
|> List.collect parseRange
printfn "%A" (expand "-6,-3--1,3-5,7-11,14,15,17-20")</
{{out}}
<pre>[-6; -3; -2; -1; 3; 4; 5; 7; 8; 9; 10; 11; 14; 15; 17; 18; 19; 20]</pre>
Line 1,551 ⟶ 1,567:
=={{header|Factor}}==
<code>R/ (?<=\d)-/ re-split</code> says: ''split only on hyphens immediately preceded by a digit.''
<
sequences sequences.extras splitting ;
Line 1,560 ⟶ 1,576:
] map-concat ;
"-6,-3--1,3-5,7-11,14,15,17-20" expand .</
{{out}}
<pre>
Line 1,567 ⟶ 1,583:
=={{header|Forth}}==
<
0. 2swap
over c@ [char] - = if
Line 1,591 ⟶ 1,607:
repeat 2drop ;
s" -6,-3--1,3-5,7-11,14,15,17-20" expand</
=={{header|Fortran}}==
Line 1,602 ⟶ 1,618:
A frustrating problem with many modern computer languages is the absence of a "shortcut" evaluation praxis for logical expressions; in Fortran's case the modern standard is that there is no standard. So a test <code>I<=LEN(TEXT) & TEXT(I:I)''etc.''</code> can't be relied upon to dodge out-of-bounds errors, and a flabby two-statement sequence is required instead. Similarly, few Fortran compilers allow for a function being evaluated via a WRITE statement to itself succeed in using a WRITE statement internally, though some do if one usage is free-format and the other formatted. If necessary, subroutine SPLOT could be re-written to convert an integer to a digit string without a WRITE statement, even for negative integers. And some compilers have difficulty with the use of the function name as a variable within the function so that it is safest to develop the result in an ordinary variable and then remember to assign its value to the function name just before exit.
A single number is made internally into a two-number range sequence, which values are used as the bounds for a DO-loop to generate the numbers for output. Despite the '''The range syntax is to be used only for, and for every range that expands to more than two values''', I see no reason for this restriction (say because otherwise some fancy method would be stymied, except I can think of no such fancier method) and I have no desire to interpose some sort of error message, a tedious business that requires a wider interface between a routine and its caller. Similarly, if a range of 40-30 were to appear, why not take it at face value? <
CONTAINS !The key function.
CHARACTER*200 FUNCTION ERANGE(TEXT) !Expands integer ranges in a list.
Line 1,701 ⟶ 1,717:
SOME = ERANGE(SOME)
WRITE (6,*) SOME !If ERANGE(SOME) then the function usually can't write output also.
END</
Output: <pre> -6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20</pre>
Line 1,708 ⟶ 1,724:
=={{header|FreeBASIC}}==
<
Sub split (s As Const String, sepList As Const String, result() As String)
Line 1,782 ⟶ 1,798:
Print
Print "Press any key to quit"
Sleep</
{{out}}
Line 1,791 ⟶ 1,807:
=={{header|Go}}==
A version rather strict with input
<
import (
Line 1,854 ⟶ 1,870:
}
fmt.Println("expanded:", r)
}</
=={{header|Groovy}}==
Line 1,864 ⟶ 1,880:
# express as a string
# unwrap the list delimiters
<
Eval.me('['+compressed.replaceAll(~/(\d)-/, '$1..')+']').flatten().toString()[1..-2]
}</
Test:
<
println (expandRanges(s))</
{{out}}
<pre>-6, -3, -2, -1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20</pre>
Line 1,875 ⟶ 1,891:
=={{header|Haskell}}==
Given either of the below implementations of <code>expandRange</code>:
<
[-6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20]</
===With conventional list processing===
<
expandRange = concatMap f . split ','
where f str@(c : cs) | '-' `elem` cs = [read (c : a) .. read b]
Line 1,887 ⟶ 1,903:
split delim [] = []
split delim l = a : split delim (dropWhile (== delim) b)
where (a, b) = break (== delim) l</
===With a parser===
<
import Text.Parsec
Line 1,915 ⟶ 1,931:
main :: IO ()
main = print $ expandRange "-6,-3--1,3-5,7-11,14,15,17-20"</
{{Out}}
<pre>Just [-6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20]</pre>
=={{header|Icon}} and {{header|Unicon}}==
<
s := "-6,-3--1,3-5,7-11,14,15,17-20"
write("Input string := ",s)
Line 1,944 ⟶ 1,960:
every (s := "[ ") ||:= !L || " "
return s || "]"
end</
{{out}}
<pre>Input string := -6,-3--1,3-5,7-11,14,15,17-20
Line 1,950 ⟶ 1,966:
=={{header|J}}==
<
thru=: <. + i.@(+*)@-~
num=: _&".
normaliz=: rplc&(',-';',_';'--';'-_')@,~&','
subranges=:<@(thru/)@(num;._2)@,&'-';._1
rngexp=: ;@subranges@normaliz</
{{out|Example}}
<
_6 _3 _2 _1 3 4 5 7 8 9 10 11 14 15 17 18 19 20</
Notes:
Line 1,975 ⟶ 1,991:
=={{header|Java}}==
<
class RangeExpander implements Iterator<Integer>, Iterable<Integer> {
Line 2,039 ⟶ 2,055:
}
}
}</
=={{header|JavaScript}}==
Line 2,045 ⟶ 2,061:
===Imperative (Spidermonkey)===
<
function main() {
Line 2,079 ⟶ 2,095:
main();
</syntaxhighlight>
{{out}}
Line 2,088 ⟶ 2,104:
====ES5====
<
'use strict';
Line 2,126 ⟶ 2,142:
return expansion(strTest);
})('-6,-3--1,3-5,7-11,14,15,17-20');</
{{Out}}
<
====ES6====
<syntaxhighlight lang="javascript">(() => {
"use strict";
// ----------------- RANGE EXPANSION -----------------
// rangeExpansion :: String -> [Int]
const rangeExpansion = rangeString =>
// A list of
// comma-delimited string which may include
// (rising) hyphenated ranges.
rangeString.split(",")
.flatMap(x => {
const ns = x.split("-")
.reduce((a, s, i, xs) =>
Boolean(s) ? (
0 < i ? a.concat(
parseInt(
xs[i - 1].length ? (
s
) : `-${s}`,
10
)
) : [Number(s)]
) : a,
[]
);
return 2 === ns.length ? (
) :
});
// ---------------------- TEST -----------------------
// main :: IO ()
const main = () =>
rangeExpansion("-6,-3--1,3-5,7-11,14,15,17-20");
// --------------------- GENERIC ---------------------
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = m =>
}, (_, i) => m + i);
// uncurry :: (a -> b -> c) -> ((a, b) -> c)
const uncurry = f =>
// A function over a pair, derived
// from a curried function.
(...args) => {
const [x, y] = Boolean(args.length % 2) ? (
args[0]
) : args;
return
};
// MAIN ---
return JSON.stringify(main());
})();</syntaxhighlight>
{{Out}}
<
=={{header|jq}}==
{{works with|jq|with regex support}}
<
def number: "-?[0-9]+";
def expand: [range(.[0]; .[1] + 1)];
Line 2,188 ⟶ 2,229:
else sub( "(?<x>\(number))-(?<y>\(number))"; "\(.x):\(.y)")
| split(":") | map(tonumber) | expand
end));</
'''Example''':
<
{{out}}$ jq -c -n -f Range_expansion.jq
[-6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20]
Line 2,196 ⟶ 2,237:
=={{header|Jsish}}==
{{trans|Javascript}}
<
"use strict";
Line 2,235 ⟶ 2,276:
rangeExpand('-6,-3--1,3-5,7-11,14,15,17-20') ==> [ -6, -3, -2, -1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20 ]
=!EXPECTEND!=
*/</
{{out}}
Line 2,242 ⟶ 2,283:
=={{header|Julia}}==
<
conv(s)= colon(map(x->parse(Int,x),match(r"^(-?\d+)-(-?\d+)$", s).captures)...)
expand(s) = mapreduce(x -> isa(x,Number)? Int(x) : conv(x), vcat, slurp(s))</
{{out}}
<pre>julia> show(expand("-6,-3--1,3-5,7-11,14,15,17-20"))
Line 2,252 ⟶ 2,293:
=={{header|K}}==
<
pos : {:[3=l:#p:&"-"=x;0,p@1;2=l;p;0=*p;,0;0,p]}
conv: 0${(x;1_ y)}/'{(pos x)_ x}'
expd: {,/@[x;&2=#:'x;{(*x)+!1+,/-':x}]}
rnge: {expd@conv grp x}</
{{out|Example}}
<
-6 -3 -2 -1 3 4 5 7 8 9 10 11 14 15 17 18 19 20</
=={{header|Kotlin}}==
<
fun expandRange(s: String): MutableList<Int> {
Line 2,296 ⟶ 2,337:
val s = "-6,-3--1,3-5,7-11,14,15,17-20"
println(expandRange(s))
}</
{{out}}
Line 2,304 ⟶ 2,345:
=={{header|Lasso}}==
<
local(parts) = regexp(`^(-?\d+)-(-?\d+)$`)
Line 2,316 ⟶ 2,357:
}
range_expand(`-6,-3--1,3-5,7-11,14,15,17-20`)</
{{out}}
Line 2,322 ⟶ 2,363:
=={{header|Liberty BASIC}}==
<
end
Line 2,344 ⟶ 2,385:
ItemCount = ItemCount + 1
wend
end function</
{{out}}
<pre>-6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20</pre>
=={{header|Lingo}}==
<
on expandRange (str)
res = ""
Line 2,369 ⟶ 2,410:
delete the last char of res
return res
end</
<
-- "-6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20"</
=={{header|LiveCode}}==
<
local tRange, tBegin, tEnd, tstep
if stepping is empty or stepping is 0 then
Line 2,411 ⟶ 2,452:
end repeat
return z
end expandRange</
Test
<
-6 -3 -2 -1 3 4 5 7 8 9 10 11 14 15 17 18 19 20 </
=={{header|Lua}}==
<
local t = {}
for n = i, j, i<j and 1 or -1 do
Line 2,446 ⟶ 2,487:
local ranges = "-6,-3--1,3-5,7-11,14,15,17-20"
print(table.concat(expand_ranges(ranges), ', '))</
Due to the way Lua's <code>tonumber</code> function works and the way the string pattern to parse ranges is written, whitespace is allowed around commas and the dash separating the range start and end (but not between the plus/minus sign and the number).
Line 2,456 ⟶ 2,497:
=={{header|Maple}}==
<syntaxhighlight lang="maple">
ExpandRanges := proc( s :: string )
uses StringTools;
Line 2,494 ⟶ 2,535:
map( DoOne, map( Trim, Split( s, "," ) ) )
end proc:
</syntaxhighlight>
Running this on the example input we get the following.
<syntaxhighlight lang="maple">
> rng := "-6,-3--1,3-5,7-11,14,15,17-20":
> ExpandRanges( rng );
[-6, -3, -2, -1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]
</syntaxhighlight>
Here is an additional example which my first attempt got wrong.
<syntaxhighlight lang="maple">
> rng := "-6,-3-1,3-5,7-11,14,15,17-20":
> ExpandRanges( rng );
[-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]
</syntaxhighlight>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
step1 = StringSplit[StringReplacePart[rng,"S",StringPosition[ rng,DigitCharacter~~"-"] /. {x_,y_} -> {y,y}],","];
Flatten@ToExpression/@Quiet@StringReplace[step1,x__~~"S"~~y__->"Range["<>x<>","<>y<>"]"] ]</
{{out|Example}}
<pre>rangeexpand["-6,-3--1,3-5,7-11,14,15,17-20"]
Line 2,517 ⟶ 2,558:
=={{header|MATLAB}} / {{header|Octave}}==
<
% Range expansion
if nargin < 1;
Line 2,529 ⟶ 2,570:
S(ixr)=':';
S=['[',S,']'];
L=eval(S);</
Usage:
<pre>
Line 2,539 ⟶ 2,580:
=={{header|MiniScript}}==
<
numstr = chars.pull
while chars and chars[0] != "," and chars[0] != "-"
Line 2,562 ⟶ 2,603:
end function
print expandRange("-6,-3--1,3-5,7-11,14,15,17-20")</
{{out}}
<pre>[-6, -3, -2, -1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]</pre>
=={{header|MUMPS}}==
<
NEW Y,I,J,X1,H SET Y=""
FOR I=1:1:$LENGTH(X,",") DO
Line 2,576 ⟶ 2,617:
.IF '(H<0) FOR J=+$EXTRACT(X1,1,(H-1)):1:+$EXTRACT(X1,(H+1),$LENGTH(X1)) SET Y=$SELECT($LENGTH(Y)=0:J,1:Y_","_J)
KILL I,J,X1,H
QUIT Y</
{{out|Example}}
<pre>USER>SET U="-6,-3--1,3-5,7-11,14,15,17-20"
Line 2,585 ⟶ 2,626:
=={{header|NetRexx}}==
Translation of: [[Range_expansion#Version_2_somewhat_simplified_.21.3F.21|Rexx Version 2]]
<
* 09.08.2012 Walter Pachl derived from my Rexx version
* Changes: translate(old,' ',',') -> old.translate(' ',',')
Line 2,619 ⟶ 2,660:
End
Say 'new='new /*show the expanded list */
</syntaxhighlight>
{{out}}
<pre>
Line 2,627 ⟶ 2,668:
=={{header|Nim}}==
<
proc expandRange(input: string): string =
Line 2,650 ⟶ 2,691:
return output.join(",")
echo("-6,-3--1,3-5,7-11,14,15,17-20".expandRange)</
{{out}}
Line 2,657 ⟶ 2,698:
=={{header|Oberon-2}}==
Oxford Oberon-2
<
MODULE LIVector;
IMPORT SYSTEM;
Line 2,894 ⟶ 2,935:
END ExpandRange.
</syntaxhighlight>
{{out}}
<pre>
Line 2,901 ⟶ 2,942:
=={{header|OCaml}}==
<
let range a b =
Line 2,923 ⟶ 2,964:
let exp = range_expand rng in
List.iter (Printf.printf " %d") exp;
print_newline ()</
=={{header|Oforth}}==
<
| i n |
s asInteger dup ifNotNull: [ res add return ] drop
Line 2,935 ⟶ 2,976:
: rangeExpand ( s -- [ n ] )
ArrayBuffer new s wordsWith( ',' ) apply( #[ over addRange ] ) ;</
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">
list = '-6,-3--1,3-5,7-11,14,15,17-20'
expanded = expandRanges(list)
Line 2,969 ⟶ 3,010:
end
return expanded
</syntaxhighlight>
{{out}}
<pre>
Line 2,977 ⟶ 3,018:
=={{header|Oz}}==
<
fun {Expand RangeDesc}
{Flatten
Line 3,006 ⟶ 3,047:
in
{System.showInfo
{Value.toVirtualString {Expand "-6,-3--1,3-5,7-11,14,15,17-20"} 100 100}}</
{{out|Sample output}}
<
=={{header|Perl}}==
One-liner:
<
map { /^(.*\d)-(.+)$/ ? $1..$2 : $_ } split /,/, shift
}
# Test and display
print join(',', rangex('-6,-3--1,3-5,7-11,14,15,17-20')), "\n";</
{{out}}
<pre>-6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20</pre>
Alternative:
<
(my $range = shift) =~ s/(?<=\d)-/../g;
eval $range;
}</
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">range_expansion</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">range</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: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">range</span><span style="color: #0000FF;">,</span><span style="color: #008000;">','</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</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: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<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>
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'-'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">si</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">to_number</span><span style="color: #0000FF;">(</span><span style="color: #000000;">si</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">else</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">startrange</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">to_number</span><span style="color: #0000FF;">(</span><span style="color: #000000;">si</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]),</span>
<span style="color: #000000;">endofrange</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">to_number</span><span style="color: #0000FF;">(</span><span style="color: #000000;">si</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$])</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">startrange</span> <span style="color: #008080;">to</span> <span style="color: #000000;">endofrange</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)</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: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">range_expansion</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"-6,-3-1,3-5,7-11,14,15,17-20"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">range_expansion</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"-6,-3--1,3-5,7-11,14,15,17-20"</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 3,057 ⟶ 3,101:
=={{header|Phixmonti}}==
Require Phixmonti 1.1
<
def append
Line 3,080 ⟶ 3,124:
endfor
r
pstack</
Other solution
<
def append
Line 3,105 ⟶ 3,149:
endfor
r
pstack</
A bit more understandable
<
def append
Line 3,145 ⟶ 3,189:
endfor
r
pstack</
=={{header|PHP}}==
{{trans|Python}}
<
$lst = array();
foreach (explode(',', $str) as $e) {
Line 3,160 ⟶ 3,204:
}
return $lst;
}</
=={{header|PicoLisp}}==
<
(make
(for S (split (chop Str) ",")
Line 3,171 ⟶ 3,215:
(format (head @ S))
(format (tail (- -1 @) S)) ) )
(link (format S)) ) ) ) )</
{{out}}
<pre>: (rangeexpand "-6,-3--1,3-5,7-11,14,15,17-20")
Line 3,177 ⟶ 3,221:
=={{header|PL/I}}==
<
procedure options (main);
Line 3,227 ⟶ 3,271:
delimiter = ',';
end;
end range_expansion;</
{{out}}
<pre>
Line 3,234 ⟶ 3,278:
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function range-expansion($array) {
function expansion($arr) {
Line 3,257 ⟶ 3,301:
}
range-expansion "-6,-3--1,3-5,7-11,14,15,17-20"
</syntaxhighlight>
<b>Output:</b>
<pre>
Line 3,265 ⟶ 3,309:
===Alternate Half-Assed Regex Version===
Ten times faster (only minimally tested).
<syntaxhighlight lang="powershell">
function Expand-Range
{
Line 3,308 ⟶ 3,352:
$output
}
</syntaxhighlight>
<syntaxhighlight lang="powershell">
(Expand-Range "-6,-3--1,3-5,7-11,14,15,17-20") -join ", "
</syntaxhighlight>
{{Out}}
<pre>
Line 3,322 ⟶ 3,366:
The code uses three predicates '''extract_Range/2''', '''study_Range/2''' and '''pack_Range/2'''.<BR>
Every predicate works in both directions arg1 towards arg2 and arg2 towards arg1, so that '''Range expansion''' and '''Range extraction''' work with the same predicates but in reverse order.
<
L = '-6,-3--1,3-5,7-11,14,15,17-20',
writeln(L),
Line 3,402 ⟶ 3,446:
run(Var1,LRest,[Deb, Fin], RRest).
run(Val,[Other|RRest], [Val, Val],[Other|RRest]).</
{{out}}
<pre> ?- range_expand.
Line 3,410 ⟶ 3,454:
=={{header|PureBasic}}==
<
Protected rangesCount = CountString(txt, ",") + 1
Protected subTxt.s, r, rangeMarker, rangeStart, rangeFinish, rangeIncrement, i
Line 3,455 ⟶ 3,499:
Input()
CloseConsole()
EndIf</
{{out}}
<pre>[ -6 -3 -2 -1 3 4 5 7 8 9 10 11 14 15 17 18 19 20 ]</pre>
Line 3,461 ⟶ 3,505:
=={{header|Python}}==
===Procedural===
<
lst = []
for r in txt.split(','):
Line 3,471 ⟶ 3,515:
return lst
print(rangeexpand('-6,-3--1,3-5,7-11,14,15,17-20'))</
another variant, using [[regular expressions]] to parse the ranges,
<
def rangeexpand(txt):
Line 3,484 ⟶ 3,528:
else:
lst.append(int(start))
return lst</
===Functional===
As a fold/catamorphism:
{{Works with|Python|3.7}}
<
from functools import (reduce)
Line 3,590 ⟶ 3,634:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>Range expansion:
Line 3,599 ⟶ 3,643:
=={{header|R}}==
<syntaxhighlight lang="r">
rangeExpand <- function(text) {
lst <- gsub("(\\d)-", "\\1:", unlist(strsplit(text, ",")))
Line 3,607 ⟶ 3,651:
rangeExpand("-6,-3--1,3-5,7-11,14,15,17-20")
[1] -6 -3 -2 -1 3 4 5 7 8 9 10 11 14 15 17 18 19 20
</syntaxhighlight>
=={{header|Racket}}==
<
#lang racket
Line 3,624 ⟶ 3,668:
(range-expand "-6,-3--1,3-5,7-11,14,15,17-20")
</syntaxhighlight>
{{out}}
<pre>
Line 3,635 ⟶ 3,679:
rather than 3 followed by - followed by -4,
a readtable is installed that makes - a delimiter.
<
#lang racket
Line 3,662 ⟶ 3,706:
(range-expand (open-input-string "-6,-3--1,3-5,7-11,14,15,17-20"))
</syntaxhighlight>
Note that one can use the full number syntax in this alternative version:
<pre>
Line 3,673 ⟶ 3,717:
{{works with|Rakudo|2016.07}}
<syntaxhighlight lang="raku"
my token number { '-'? \d+ }
my token range { (<&number>) '-' (<&number>) }
Line 3,683 ⟶ 3,727:
}
say range-expand('-6,-3--1,3-5,7-11,14,15,17-20').join(', ');</
{{out}}
Line 3,691 ⟶ 3,735:
Alternatively, using a grammar:
<syntaxhighlight lang="raku"
token TOP { <term>* % ',' { make $<term>.map(*.made) } }
token term { [<range>|<num>] { make ($<num> // $<range>).made } }
Line 3,698 ⟶ 3,742:
}
say RangeList.parse('-6,-3--1,3-5,7-11,14,15,17-20').made.flat.join(', ');</
{{out}}
Line 3,705 ⟶ 3,749:
=={{header|Raven}}==
Based loosely on Ruby
<
# "-22" split by "-" is [ "", "22" ] so check if
# first list item is "" -> a negative number
Line 3,737 ⟶ 3,781:
"\n" print
'-6,-3--1,3-5,7-11,14,15,17-20' range_expand</
{{out}}
<pre>-6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20</pre>
Line 3,744 ⟶ 3,788:
===version 1===
Extra imbedded blanks were added to the old list (which are ignored) to make the ''over/under'' comparison easier (in the output).
<
old= '-6,-3--1, 3-5, 7-11, 14,15,17-20'; a=translate(old,,',')
new= /*translate [↑] commas (,) ───► blanks*/
Line 3,756 ⟶ 3,800:
new=translate( strip(new), ',', " ") /*remove the first blank, add commas. */
say 'old list: ' old /*show the old list of numbers/ranges.*/
say 'new list: ' new /* " " new " " numbers. */</
'''output'''
<pre>
Line 3,764 ⟶ 3,808:
===Version 2 somewhat simplified !?!===
<
* 09.08.2012 Walter Pachl
**********************************************************************/
Line 3,792 ⟶ 3,836:
End
End
Say 'new='new /*show the expanded list */</
{{out}}
<pre>
Line 3,800 ⟶ 3,844:
=={{header|Ring}}==
<
# Project : Range expansion
Line 3,842 ⟶ 3,886:
see svect
see "]" + nl
</syntaxhighlight>
Output:
<pre>
Line 3,849 ⟶ 3,893:
=={{header|Ruby}}==
<
rng.split(',').flat_map do |part|
if part =~ /^(-?\d+)-(-?\d+)$/
Line 3,859 ⟶ 3,903:
end
p range_expand('-6,-3--1,3-5,7-11,14,15,17-20')</
{{out}}
<pre>[-6, -3, -2, -1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]</pre>
=={{header|Run BASIC}}==
<
end
Line 3,888 ⟶ 3,932:
if i <> 0 then goto [loop]
rangeExpand$ = range$
end function</
{{out}}
<pre>-6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20</pre>
Line 3,895 ⟶ 3,939:
Rust doesn't have regex in standard library yet.
<
// Precondition: range doesn't contain multibyte UTF-8 characters
Line 3,921 ⟶ 3,965:
println!("{:?}", range_expand("-6,-3--1,3-5,7-11,14,15,17-20"));
}
</syntaxhighlight>
{{out}}
Line 3,929 ⟶ 3,973:
=={{header|S-lang}}==
<
foreach s (strchop(r_expres, ',', 0))
Line 3,947 ⟶ 3,991:
list_append(r_expan, s);
}
print(strjoin(list_to_array(r_expan), ", "));</
=={{header|Scala}}==
<
str split "," flatMap { (s) =>
val r = """(-?\d+)(?:-(-?\d+))?""".r
val r(a,b) = s
if (b == null) Seq(a.toInt) else a.toInt to b.toInt
}</
{{out}}
Line 3,964 ⟶ 4,008:
=={{header|Scheme}}==
<
(lambda (str char skip count)
(let ((len (string-length str)))
Line 3,998 ⟶ 4,042:
(display ","))))))
(split str #\, 0 0))
(newline)))</
{{out}}
<pre>
Line 4,008 ⟶ 4,052:
The library [http://seed7.sourceforge.net/libraries/scanstri.htm scanstri.s7i] defines the function [http://seed7.sourceforge.net/libraries/scanstri.htm#getInteger%28inout_string%29 getInteger] to extract substrings with integer literals (optional sign followed by a sequence of digits) from a string.
The integer literals are converted to the type [http://seed7.sourceforge.net/libraries/integer.htm integer] with the [http://seed7.sourceforge.net/libraries/integer.htm#%28attr_integer%29parse%28in_string%29 parse] operator.
<
include "scanstri.s7i";
Line 4,042 ⟶ 4,086:
end for;
writeln;
end func;</
{{out}}
<pre>
Line 4,049 ⟶ 4,093:
=={{header|Sidef}}==
<
str.split(',').map { |r|
var m = r.match(/^
Line 4,060 ⟶ 4,104:
}
say rangex('-6,-3--1,3-5,7-11,14,15,17-20').join(',')</
{{out}}
<pre>-6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20</pre>
=={{header|SNOBOL4}}==
<
define('range(n1,n2)') :(range_end)
range range = range n1 ','; n1 = lt(n1,n2) n1 + 1 :s(range)
Line 4,079 ⟶ 4,123:
* # Test and display
output = rangex('-6,-3--1,3-5,7-11,14,15,17-20')
end</
{{out}}
<pre>-6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20</pre>
=={{header|SQL}}==
{{works with|ORACLE 19c}}
This is not a particularly efficient solution, but it gets the job done.
<syntaxhighlight lang="sql">
/*
This code is an implementation of "Range expansion" in SQL ORACLE 19c
p_list_of_sets -- input string
delimeter by default ","
*/
with
function range_expansion(p_list_of_sets in varchar2)
return varchar2 is
--
v_list_of_sets varchar2(32767) := p_list_of_sets;
v_output varchar2(32767) ;
v_set_1 varchar2(2000) ;
v_set_1_min pls_integer;
v_set_1_max pls_integer;
--
function sort_set(p_in_str varchar2)
return varchar2 is
v_out varchar2(32767) := p_in_str;
begin
--
with out_tab as
(select to_number(regexp_substr(str, '[^,]+', 1, rownum, 'c', 0) default null on conversion error) elem
from
(select p_in_str as str
from dual
)
connect by level <= regexp_count(str, '[^,]+')
)
select trim(both ',' from min(elem)||','||max(elem)) end
into v_out
from out_tab;
--
return v_out;
end;
--
function sort_output(p_in_str varchar2)
return varchar2 is
v_out varchar2(32767) := p_in_str;
begin
--
with out_tab as
(select distinct to_number(regexp_substr(str, '[^,]+', 1, rownum, 'c', 0) default null on conversion error) elem
from
(select p_in_str as str
from dual
)
connect by level <= regexp_count(str, '[^,]+')
)
select listagg(elem, ',') within group(order by elem) end
into v_out
from out_tab
where elem is not null;
--
return v_out;
end;
--
begin
--cleaning
v_list_of_sets := replace(v_list_of_sets, ' ', '') ;
v_list_of_sets := replace(v_list_of_sets, '+', '') ;
v_list_of_sets := replace(v_list_of_sets, ',', '|') ;
v_list_of_sets := regexp_replace(v_list_of_sets, '(\d{1,})-(\d{1,})', '\1,\2', 1, 0) ;
v_list_of_sets := regexp_replace(v_list_of_sets, '(\d{1,})--(\d{1,})', '\1,-\2', 1, 0) ;
--
<<loop_through_sets>>
while regexp_count(v_list_of_sets, '[^|]+') > 0
loop
v_set_1 := regexp_substr(v_list_of_sets, '[^|]+', 1, 1) ;
v_list_of_sets := regexp_replace(v_list_of_sets, v_set_1, sort_set(v_set_1), 1, 1) ;
v_set_1 := sort_set(v_set_1) ;
--
continue loop_through_sets when v_set_1 is null;
--
v_set_1_min := least(to_number(regexp_substr(v_set_1, '[^,]+', 1, 1))
,to_number(regexp_substr(v_set_1, '[^,]+', 1, 2))
) ;
v_set_1_max := greatest(to_number(regexp_substr(v_set_1, '[^,]+', 1, 1))
,to_number(regexp_substr(v_set_1, '[^,]+', 1, 2))
) ;
--
<<loop_for>>
for i in v_set_1_min..v_set_1_max
loop
--
v_output := v_output||','||i;
--
end loop loop_for;
--
v_list_of_sets := regexp_replace(v_list_of_sets,v_set_1,'',1,1);
--
end loop loop_through_sets;
--
v_output := sort_output(v_output);
--
return trim(v_output);
end;
--Test
select '-- Test ' as output from dual
union all
select lpad(' ', 65) || ' ==> ' || range_expansion(' ') as output from dual
union all
select lpad('-0,+0,-2 ,-1--2,3 ,-3, 2,-2', 65) || ' ==> ' || range_expansion('-0,+0,-2 ,-1--2,3 ,-3, 2,-2') as output from dual
union all
select lpad('0,-1,+2,-2', 65) || ' ==> ' || range_expansion('0,-1,2,-2') as output from dual
union all
select lpad('-D,-w23--1,+14q,15,17-20,3-5,7-11, +0, 2q, +4, 3,0 ,-0,-2 , -3', 65) || ' ==> ' || range_expansion('-D,-w23--1,+14q,15,17-20,3-5,7-11, +0, 2q, +4, 3,0 ,-0,-2 , -3') as output from dual
union all
select lpad('-6,-3--1,14,15,17-20,3-5,7-11', 65) || ' ==> ' || range_expansion('-6,-3--1,14,15,17-20,3-5,11-7') as output from dual
union all
--Test RosettaCode
select '-- Test RosettaCode' as output from dual
union all
select lpad('-6,-3--1,3-5,7-11,14,15,17-20', 65) || ' ==> ' || range_expansion('-6,-3--1,3-5,7-11,14,15,17-20') as output from dual
;
</syntaxhighlight>
{{out}}
<pre>
-- Test
==>
-0,+0,-2 ,-1--2,3 ,-3, 2,-2 ==> -3,-2,-1,0,2,3
0,-1,+2,-2 ==> -2,-1,0,2
-D,-w23--1,+14q,15,17-20,3-5,7-11, +0, 2q, +4, 3,0 ,-0,-2 , -3 ==> -3,-2,-1,0,3,4,5,7,8,9,10,11,15,17,18,19,20
-6,-3--1,14,15,17-20,3-5,7-11 ==> -6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20
-- Test RosettaCode
-6,-3--1,3-5,7-11,14,15,17-20 ==> -6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20
</pre>
=={{header|Tailspin}}==
<
composer expand
[<element>*]
Line 4,092 ⟶ 4,270:
'-6,-3--1,3-5,7-11,14,15,17-20' -> expand -> !OUT::write
</syntaxhighlight>
{{out}}
<pre>
Line 4,099 ⟶ 4,277:
=={{header|Tcl}}==
<
set result {}
foreach term [split $desc ","] {
Line 4,112 ⟶ 4,290:
}
puts [rangeExpand "-6,-3--1,3-5,7-11,14,15,17-20"]</
{{out}}
<pre>-6 -3 -2 -1 3 4 5 7 8 9 10 11 14 15 17 18 19 20</pre>
=={{header|TUSCRIPT}}==
<
rangednrs="-6,-3--1,3-5,7-11,14,15,17-20"
expandnrs=SPLIT (rangednrs,":,:")
Line 4,138 ⟶ 4,316:
expandnrs= JOIN (expandnrs,",")
PRINT expandnrs</
{{out}}
<pre>
Line 4,163 ⟶ 4,341:
Code:
<
@(define entry (e))@\
@(local n1 n2)@\
Line 4,205 ⟶ 4,383:
your junk: @{trailing-junk}
@(end)
@(end)</
{{out|Run}}
Line 4,229 ⟶ 4,407:
=={{header|UNIX Shell}}==
{{works with|bash}}
<
range_expand () (
Line 4,247 ⟶ 4,425:
)
range_expand "-6,-3--1,3-5,7-11,14,15,17-20"</
{{out}}
<pre>-6 -3 -2 -1 3 4 5 7 8 9 10 11 14 15 17 18 19 20</pre>
=={{header|Ursala}}==
<
#import int
Line 4,259 ⟶ 4,437:
#cast %zL
t = rex '-6,-3--1,3-5,7-11,14,15,17-20'</
{{out}}
<pre><-6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20></pre>
=={{header|VBA}}==
<
' return a list with the numbers expressed in AString
Dim Splits() As String
Line 4,309 ⟶ 4,487:
Next
Debug.Print
End Sub</
{{out}}
<pre>
Line 4,319 ⟶ 4,497:
=={{header|Wren}}==
{{trans|Kotlin}}
<
var list = []
var items = s.split(",")
Line 4,347 ⟶ 4,525:
var s = "-6,-3--1,3-5,7-11,14,15,17-20"
System.print(expandRange.call(s))</
{{out}}
Line 4,355 ⟶ 4,533:
=={{header|XPL0}}==
See Range Extraction for explanations.
<syntaxhighlight lang "XPL0">proc Expand(Range);
char
int
[Text(8, Range);
loop [Lo:= IntIn(8);
case ChIn(8) of
^-: [Hi:= IntIn(8);
for N:= Lo to Hi do
[IntOut(0, N);
if N < Hi then Text(0, ", ");
BackUp;
if ChIn(8) = $1A \EOF\ then quit;
^,: IntOut(0, Lo)
other \EOF\ [IntOut(0, Lo); quit];
];
];
Expand("-6,-3--1,3-5,7-11,14,15,17-20")
</syntaxhighlight>
{{out}}
<pre>
-6, -3, -2, -1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20</pre>
=={{header|Yabasic}}==
<
sub RangeExpand$(s$)
Line 4,426 ⟶ 4,584:
return left$(r$, len(r$) - 1)
end sub
</syntaxhighlight>
=={{header|zkl}}==
<
fcn(s,re){
if (re.search(s)){
Line 4,439 ⟶ 4,597:
s.split(",").pump(List, _.fp1(RegExp(0'|(.*\d+)-(.*\d+)|)))
.flatten().concat(",");
}</
<
rangex(ns).println();
ns="-6,-3--1,3-5,7-11,14,15,17-20";
rangex(ns).println();</
{{out}}
|