Flatten a list: Difference between revisions
Content added Content deleted
(RPL: add section) |
(Grouping BASIC dialects) |
||
Line 488: | Line 488: | ||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header| |
=={{header|BASIC}}== |
||
==={{header|BaCon}}=== |
|||
BaCon has the concept of delimited strings, which may contain delimited strings within delimited strings etc. Such nested delimited strings must be surrounded by (escaped) double quotes in order to avoid their delimiter messing up operations on higher level delimited strings. However, from functional point of view, a delimited string is the same as a regular list. The special function FLATTEN$ can actually flatten out lists within lists. The last SORT$ in the program below makes sure no empty items remain in the list. |
BaCon has the concept of delimited strings, which may contain delimited strings within delimited strings etc. Such nested delimited strings must be surrounded by (escaped) double quotes in order to avoid their delimiter messing up operations on higher level delimited strings. However, from functional point of view, a delimited string is the same as a regular list. The special function FLATTEN$ can actually flatten out lists within lists. The last SORT$ in the program below makes sure no empty items remain in the list. |
||
<syntaxhighlight lang="qbasic">OPTION COLLAPSE TRUE |
<syntaxhighlight lang="qbasic">OPTION COLLAPSE TRUE |
||
Line 502: | Line 503: | ||
PRINT SORT$(lst$, ",")</syntaxhighlight> |
PRINT SORT$(lst$, ",")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>"1",2,"\"3,4\",5","\"\\"\\"\"","\"\\"6\\"\"",7,8,"" |
|||
<pre> |
|||
1,2,3,4,5,6,7,8</pre> |
|||
1,2,3,4,5,6,7,8 |
|||
</pre> |
|||
==={{header|BASIC256}}=== |
|||
=={{header|BASIC256}}== |
|||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
<syntaxhighlight lang="basic256">sComma = "": sFlatter = "" |
<syntaxhighlight lang="basic256">sComma = "": sFlatter = "" |
||
Line 523: | Line 521: | ||
End</syntaxhighlight> |
End</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Igual que la entrada de FreeBASIC.</pre> |
|||
<pre> |
|||
Igual que la entrada de FreeBASIC. |
|||
</pre> |
|||
==={{header|FreeBASIC}}=== |
|||
{{trans|Gambas}} |
|||
<syntaxhighlight lang="freebasic">Dim As String sComma, sString, sFlatter |
|||
Dim As Short siCount |
|||
sString = "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" |
|||
For siCount = 1 To Len(sString) |
|||
If Instr("[] ,", Mid(sString, siCount, 1)) = 0 Then |
|||
sFlatter += sComma + Mid(sString, siCount, 1) |
|||
sComma = ", " |
|||
End If |
|||
Next siCount |
|||
Print "["; sFlatter; "]" |
|||
Sleep</syntaxhighlight> |
|||
{{out}} |
|||
<pre>[1, 2, 3, 4, 5, 6, 7, 8]</pre> |
|||
==={{header|FutureBasic}}=== |
|||
Definitely old school. |
|||
<syntaxhighlight lang="futurebasic"> |
|||
local fn FlattenList( list as Str255 ) as Str255 |
|||
long i |
|||
Str255 flatStr, commaStr |
|||
flatStr = "" |
|||
for i = 1 to len$(list) |
|||
if ( instr$( 0, "[] ,", mid$( list, i, 1 ) ) === 0 ) |
|||
flatStr += commaStr + mid$( list, i, 1 ) |
|||
commaStr = ", " |
|||
end if |
|||
next |
|||
end fn = flatStr |
|||
window 1, @"Flatten a list", ( 0, 0, 350, 150 ) |
|||
print "["; fn FlattenList( "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" ); "]" |
|||
HandleEvents</syntaxhighlight> |
|||
{{output}} |
|||
<pre>[1, 2, 3, 4, 5, 6, 7, 8]</pre> |
|||
Modern and a little outside the box. |
|||
<syntaxhighlight lang="futurebasic"> |
|||
void local fn FlattenAList |
|||
CFStringRef listStr = @"[[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]" |
|||
CFArrayRef listArr = fn StringComponentsSeparatedByCharactersInSet( listStr, fn CharacterSetWithCharactersInString( @"\"[ ]," ) ) |
|||
CFMutableArrayRef mutArr = fn MutableArrayWithArray( listArr ) |
|||
MutableArrayRemoveObject( mutArr, @"" ) |
|||
CFStringRef flatStr = fn ArrayComponentsJoinedByString( mutArr, @", " ) |
|||
printf @"[%@]", flatStr |
|||
end fn |
|||
fn FlattenAList |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre>[1, 2, 3, 4, 5, 6, 7, 8]</pre> |
|||
==={{header|Gambas}}=== |
|||
'''[https://gambas-playground.proko.eu/?gist=1c0157ce2b7eab99ba4e784e183ba474 Click this link to run this code]''' |
|||
<syntaxhighlight lang="gambas">'Code 'borrowed' from Run BASIC |
|||
Public Sub Main() |
|||
Dim sComma, sString, sFlatter As String |
|||
Dim siCount As Short |
|||
sString = "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" |
|||
For siCount = 1 To Len(sString) |
|||
If InStr("[] ,", Mid$(sString, siCount, 1)) = 0 Then |
|||
sFlatter = sFlatter & sComma & Mid(sString, siCount, 1) |
|||
sComma = "," |
|||
End If |
|||
Next |
|||
Print "["; sFlatter; "]" |
|||
End</syntaxhighlight> |
|||
Output: |
|||
<pre>[1,2,3,4,5,6,7,8]</pre> |
|||
==={{header|PureBasic}}=== |
|||
<syntaxhighlight lang="purebasic">Structure RCList |
|||
Value.i |
|||
List A.RCList() |
|||
EndStructure |
|||
Procedure Flatten(List A.RCList()) |
|||
ResetList(A()) |
|||
While NextElement(A()) |
|||
With A() |
|||
If \Value |
|||
Continue |
|||
Else |
|||
ResetList(\A()) |
|||
While NextElement(\A()) |
|||
If \A()\Value: A()\Value=\A()\Value: EndIf |
|||
Wend |
|||
EndIf |
|||
While ListSize(\A()): DeleteElement(\A()): Wend |
|||
If Not \Value: DeleteElement(A()): EndIf |
|||
EndWith |
|||
Wend |
|||
EndProcedure</syntaxhighlight> |
|||
Set up the MD-List & test the Flattening procedure. |
|||
<syntaxhighlight lang="purebasic">;- Set up two lists, one multi dimensional and one 1-D. |
|||
NewList A.RCList() |
|||
;- Create a deep list |
|||
With A() |
|||
AddElement(A()): AddElement(\A()): AddElement(\A()): \A()\Value=1 |
|||
AddElement(A()): A()\Value=2 |
|||
AddElement(A()): AddElement(\A()): \A()\Value=3 |
|||
AddElement(\A()): \A()\Value=4 |
|||
AddElement(A()): AddElement(\A()): \A()\Value=5 |
|||
AddElement(A()): AddElement(\A()): AddElement(\A()): AddElement(\A()) |
|||
AddElement(A()): AddElement(\A()): AddElement(\A()): \A()\Value=6 |
|||
AddElement(A()): A()\Value=7 |
|||
AddElement(A()): A()\Value=8 |
|||
AddElement(A()): AddElement(\A()): AddElement(\A()) |
|||
EndWith |
|||
Flatten(A()) |
|||
;- Present the result |
|||
If OpenConsole() |
|||
Print("Flatten: [") |
|||
ForEach A() |
|||
Print(Str(A()\Value)) |
|||
If ListIndex(A())<(ListSize(A())-1) |
|||
Print(", ") |
|||
Else |
|||
PrintN("]") |
|||
EndIf |
|||
Next |
|||
Print(#CRLF$+"Press ENTER to quit"): Input() |
|||
EndIf</syntaxhighlight><pre>Flatten: [1, 2, 4, 5, 6, 7, 8]</pre> |
|||
==={{header|QBasic}}=== |
|||
{{works with|QBasic|1.1}} |
|||
{{works with|QuickBasic|4.5}} |
|||
<syntaxhighlight lang="qbasic">sString$ = "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" |
|||
FOR siCount = 1 TO LEN(sString$) |
|||
IF INSTR("[] ,", MID$(sString$, siCount, 1)) = 0 THEN |
|||
sFlatter$ = sFlatter$ + sComma$ + MID$(sString$, siCount, 1) |
|||
sComma$ = ", " |
|||
END IF |
|||
NEXT siCount |
|||
PRINT "["; sFlatter$; "]" |
|||
END</syntaxhighlight> |
|||
==={{header|Run BASIC}}=== |
|||
{{incorrect|Run BASIC| The task is not in string translation but in list translation.}} |
|||
<syntaxhighlight lang="runbasic">n$ = "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" |
|||
for i = 1 to len(n$) |
|||
if instr("[] ,",mid$(n$,i,1)) = 0 then |
|||
flatten$ = flatten$ + c$ + mid$(n$,i,1) |
|||
c$ = "," |
|||
end if |
|||
next i |
|||
print "[";flatten$;"]"</syntaxhighlight> |
|||
{{out}} |
|||
<pre>[1,2,3,4,5,6,7,8]</pre> |
|||
==={{header|TI-89 BASIC}}=== |
|||
There is no nesting of lists or other data structures in TI-89 BASIC, short of using variable names as pointers. |
|||
==={{header|True BASIC}}=== |
|||
<syntaxhighlight lang="qbasic">LET sstring$ = "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" |
|||
FOR sicount = 1 TO LEN(sstring$) |
|||
IF pos("[] ,",(sstring$)[sicount:sicount+1-1]) = 0 THEN |
|||
LET sflatter$ = sflatter$ & scomma$ & (sstring$)[sicount:sicount+1-1] |
|||
LET scomma$ = ", " |
|||
END IF |
|||
NEXT sicount |
|||
PRINT "["; sflatter$; "]" |
|||
END</syntaxhighlight> |
|||
==={{header|XBasic}}=== |
|||
{{works with|Windows XBasic}} |
|||
<syntaxhighlight lang="xbasic">PROGRAM "Flatten a list" |
|||
DECLARE FUNCTION Entry () |
|||
FUNCTION Entry () |
|||
n$ = "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" |
|||
FOR i = 1 TO LEN(n$) |
|||
IF INSTR("[] ,",MID$(n$,i,1)) = 0 THEN |
|||
flatten$ = flatten$ + c$ + MID$(n$,i,1) |
|||
c$ = ", " |
|||
END IF |
|||
NEXT i |
|||
PRINT "[";flatten$;"]" |
|||
END FUNCTION |
|||
END PROGRAM</syntaxhighlight> |
|||
{{out}} |
|||
<pre>[1, 2, 3, 4, 5, 6, 7, 8]</pre> |
|||
==={{header|Yabasic}}=== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="yabasic">sString$ = "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" |
|||
For siCount = 1 To Len(sString$) |
|||
If Instr("[] ,", Mid$(sString$, siCount, 1)) = 0 Then |
|||
sFlatter$ = sFlatter$ + sComma$ + Mid$(sString$, siCount, 1) |
|||
sComma$ = ", " |
|||
End If |
|||
Next siCount |
|||
Print "[", sFlatter$, "]" |
|||
End</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Igual que la entrada de FreeBASIC.</pre> |
|||
==={{header|ZX Spectrum Basic}}=== |
|||
{{incorrect|ZX Spectrum Basic| The task is not in string translation but in list translation.}} |
|||
<syntaxhighlight lang="zxbasic">10 LET f$="[" |
|||
20 LET n$="[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" |
|||
30 FOR i=2 TO (LEN n$)-1 |
|||
40 IF n$(i)>"/" AND n$(i)<":" THEN LET f$=f$+n$(i): GO TO 60 |
|||
50 IF n$(i)="," AND f$(LEN f$)<>"," THEN LET f$=f$+"," |
|||
60 NEXT i |
|||
70 LET f$=f$+"]": PRINT f$</syntaxhighlight> |
|||
=={{header|BQN}}== |
=={{header|BQN}}== |
||
Line 1,636: | Line 1,859: | ||
All of this relies on the list being presented as a flat text, which text is then manipulated directly. If the list was manifested in a data structure of some kind with links and suchlike, then tree-traversal of that structure would be needed to reach the leaf entries. |
All of this relies on the list being presented as a flat text, which text is then manipulated directly. If the list was manifested in a data structure of some kind with links and suchlike, then tree-traversal of that structure would be needed to reach the leaf entries. |
||
=={{header|FreeBASIC}}== |
|||
{{trans|Gambas}} |
|||
<syntaxhighlight lang="freebasic">Dim As String sComma, sString, sFlatter |
|||
Dim As Short siCount |
|||
sString = "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" |
|||
For siCount = 1 To Len(sString) |
|||
If Instr("[] ,", Mid(sString, siCount, 1)) = 0 Then |
|||
sFlatter += sComma + Mid(sString, siCount, 1) |
|||
sComma = ", " |
|||
End If |
|||
Next siCount |
|||
Print "["; sFlatter; "]" |
|||
Sleep</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[1, 2, 3, 4, 5, 6, 7, 8] |
|||
</pre> |
|||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
Line 1,665: | Line 1,865: | ||
println[flatten[a]] |
println[flatten[a]] |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|FutureBasic}}== |
|||
Definitely old school. |
|||
<syntaxhighlight lang="futurebasic"> |
|||
local fn FlattenList( list as Str255 ) as Str255 |
|||
long i |
|||
Str255 flatStr, commaStr |
|||
flatStr = "" |
|||
for i = 1 to len$(list) |
|||
if ( instr$( 0, "[] ,", mid$( list, i, 1 ) ) == 0 ) |
|||
flatStr += commaStr + mid$( list, i, 1 ) |
|||
commaStr = ", " |
|||
end if |
|||
next |
|||
end fn = flatStr |
|||
window 1, @"Flatten a list", ( 0, 0, 350, 150 ) |
|||
print "["; fn FlattenList( "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" ); "]" |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
[1, 2, 3, 4, 5, 6, 7, 8] |
|||
</pre> |
|||
Modern and a little outside the box. |
|||
<syntaxhighlight lang="futurebasic"> |
|||
void local fn FlattenAList |
|||
CFStringRef listStr = @"[[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []]" |
|||
CFArrayRef listArr = fn StringComponentsSeparatedByCharactersInSet( listStr, fn CharacterSetWithCharactersInString( @"\"[ ]," ) ) |
|||
CFMutableArrayRef mutArr = fn MutableArrayWithArray( listArr ) |
|||
MutableArrayRemoveObject( mutArr, @"" ) |
|||
CFStringRef flatStr = fn ArrayComponentsJoinedByString( mutArr, @", " ) |
|||
printf @"[%@]", flatStr |
|||
end fn |
|||
fn FlattenAList |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
[1, 2, 3, 4, 5, 6, 7, 8] |
|||
</pre> |
|||
=={{header|Gambas}}== |
|||
'''[https://gambas-playground.proko.eu/?gist=1c0157ce2b7eab99ba4e784e183ba474 Click this link to run this code]''' |
|||
<syntaxhighlight lang="gambas">'Code 'borrowed' from Run BASIC |
|||
Public Sub Main() |
|||
Dim sComma, sString, sFlatter As String |
|||
Dim siCount As Short |
|||
sString = "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" |
|||
For siCount = 1 To Len(sString) |
|||
If InStr("[] ,", Mid$(sString, siCount, 1)) = 0 Then |
|||
sFlatter = sFlatter & sComma & Mid(sString, siCount, 1) |
|||
sComma = "," |
|||
End If |
|||
Next |
|||
Print "["; sFlatter; "]" |
|||
End</syntaxhighlight> |
|||
Output: |
|||
<pre> |
|||
[1,2,3,4,5,6,7,8] |
|||
</pre> |
|||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
Line 3,008: | Line 3,132: | ||
flatten(NonList, T, [NonList|T]). |
flatten(NonList, T, [NonList|T]). |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|PureBasic}}== |
|||
<syntaxhighlight lang="purebasic">Structure RCList |
|||
Value.i |
|||
List A.RCList() |
|||
EndStructure |
|||
Procedure Flatten(List A.RCList()) |
|||
ResetList(A()) |
|||
While NextElement(A()) |
|||
With A() |
|||
If \Value |
|||
Continue |
|||
Else |
|||
ResetList(\A()) |
|||
While NextElement(\A()) |
|||
If \A()\Value: A()\Value=\A()\Value: EndIf |
|||
Wend |
|||
EndIf |
|||
While ListSize(\A()): DeleteElement(\A()): Wend |
|||
If Not \Value: DeleteElement(A()): EndIf |
|||
EndWith |
|||
Wend |
|||
EndProcedure</syntaxhighlight> |
|||
Set up the MD-List & test the Flattening procedure. |
|||
<syntaxhighlight lang="purebasic">;- Set up two lists, one multi dimensional and one 1-D. |
|||
NewList A.RCList() |
|||
;- Create a deep list |
|||
With A() |
|||
AddElement(A()): AddElement(\A()): AddElement(\A()): \A()\Value=1 |
|||
AddElement(A()): A()\Value=2 |
|||
AddElement(A()): AddElement(\A()): \A()\Value=3 |
|||
AddElement(\A()): \A()\Value=4 |
|||
AddElement(A()): AddElement(\A()): \A()\Value=5 |
|||
AddElement(A()): AddElement(\A()): AddElement(\A()): AddElement(\A()) |
|||
AddElement(A()): AddElement(\A()): AddElement(\A()): \A()\Value=6 |
|||
AddElement(A()): A()\Value=7 |
|||
AddElement(A()): A()\Value=8 |
|||
AddElement(A()): AddElement(\A()): AddElement(\A()) |
|||
EndWith |
|||
Flatten(A()) |
|||
;- Present the result |
|||
If OpenConsole() |
|||
Print("Flatten: [") |
|||
ForEach A() |
|||
Print(Str(A()\Value)) |
|||
If ListIndex(A())<(ListSize(A())-1) |
|||
Print(", ") |
|||
Else |
|||
PrintN("]") |
|||
EndIf |
|||
Next |
|||
Print(#CRLF$+"Press ENTER to quit"): Input() |
|||
EndIf</syntaxhighlight><pre>Flatten: [1, 2, 4, 5, 6, 7, 8]</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Recursive=== |
===Recursive=== |
||
<syntaxhighlight lang="python">>>> def flatten(lst): |
<syntaxhighlight lang="python">>>> def flatten(lst): |
||
return sum( ([x] if not isinstance(x, list) else flatten(x) |
return sum( ([x] if not isinstance(x, list) else flatten(x) |
||
Line 3,079: | Line 3,144: | ||
===Recursive, generative and working with any type of iterable object=== |
===Recursive, generative and working with any type of iterable object=== |
||
<syntaxhighlight lang="python">>>> def flatten(itr): |
<syntaxhighlight lang="python">>>> def flatten(itr): |
||
>>> for x in itr: |
>>> for x in itr: |
||
Line 3,107: | Line 3,171: | ||
===Non-recursive=== |
===Non-recursive=== |
||
Function flat is iterative and flattens the list in-place. It follows the Python idiom of returning None when acting in-place: |
Function flat is iterative and flattens the list in-place. It follows the Python idiom of returning None when acting in-place: |
||
<syntaxhighlight lang="python">>>> def flat(lst): |
<syntaxhighlight lang="python">>>> def flat(lst): |
||
Line 3,333: | Line 3,396: | ||
We repeatedly apply <tt>raze</tt> until the return value converges to a fixed value. |
We repeatedly apply <tt>raze</tt> until the return value converges to a fixed value. |
||
<syntaxhighlight lang="q">(raze/) ((1); 2; ((3;4); 5); ((())); (((6))); 7; 8; ())</syntaxhighlight> |
<syntaxhighlight lang="q">(raze/) ((1); 2; ((3;4); 5); ((())); (((6))); 7; 8; ())</syntaxhighlight> |
||
=={{header|QBasic}}== |
|||
{{works with|QBasic|1.1}} |
|||
{{works with|QuickBasic|4.5}} |
|||
<syntaxhighlight lang="qbasic">sString$ = "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" |
|||
FOR siCount = 1 TO LEN(sString$) |
|||
IF INSTR("[] ,", MID$(sString$, siCount, 1)) = 0 THEN |
|||
sFlatter$ = sFlatter$ + sComma$ + MID$(sString$, siCount, 1) |
|||
sComma$ = ", " |
|||
END IF |
|||
NEXT siCount |
|||
PRINT "["; sFlatter$; "]" |
|||
END</syntaxhighlight> |
|||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
Line 3,501: | Line 3,549: | ||
# => [1, 2, [3, 4], 5, [[]], [[6]], 7, 8] |
# => [1, 2, [3, 4], 5, [[]], [[6]], 7, 8] |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Run BASIC}}== |
|||
{{incorrect|Run BASIC| The task is not in string translation but in list translation.}} |
|||
<syntaxhighlight lang="runbasic">n$ = "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" |
|||
for i = 1 to len(n$) |
|||
if instr("[] ,",mid$(n$,i,1)) = 0 then |
|||
flatten$ = flatten$ + c$ + mid$(n$,i,1) |
|||
c$ = "," |
|||
end if |
|||
next i |
|||
print "[";flatten$;"]"</syntaxhighlight> |
|||
{{out}} |
|||
<pre>[1,2,3,4,5,6,7,8]</pre> |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Line 3,988: | Line 4,023: | ||
puts [flatten {{1} 2 {{3 4} 5} {{{}}} {{{6}}} 7 8 {}}] |
puts [flatten {{1} 2 {{3 4} 5} {{{}}} {{{6}}} 7 8 {}}] |
||
# ===> 1 2 3 4 5 6 7 8</syntaxhighlight> |
# ===> 1 2 3 4 5 6 7 8</syntaxhighlight> |
||
=={{header|TI-89 BASIC}}== |
|||
There is no nesting of lists or other data structures in TI-89 BASIC, short of using variable names as pointers. |
|||
=={{header|Trith}}== |
=={{header|Trith}}== |
||
Line 3,996: | Line 4,028: | ||
{{omit from|UNIX Shell}} |
{{omit from|UNIX Shell}} |
||
=={{header|True BASIC}}== |
|||
<syntaxhighlight lang="qbasic">LET sstring$ = "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" |
|||
FOR sicount = 1 TO LEN(sstring$) |
|||
IF pos("[] ,",(sstring$)[sicount:sicount+1-1]) = 0 THEN |
|||
LET sflatter$ = sflatter$ & scomma$ & (sstring$)[sicount:sicount+1-1] |
|||
LET scomma$ = ", " |
|||
END IF |
|||
NEXT sicount |
|||
PRINT "["; sflatter$; "]" |
|||
END</syntaxhighlight> |
|||
=={{header|TXR}}== |
=={{header|TXR}}== |
||
Line 4,149: | Line 4,170: | ||
[1, 2, 3, 4, 5, 6, 7, 8] |
[1, 2, 3, 4, 5, 6, 7, 8] |
||
</pre> |
</pre> |
||
=={{header|XBasic}}== |
|||
{{works with|Windows XBasic}} |
|||
<syntaxhighlight lang="xbasic">PROGRAM "Flatten a list" |
|||
DECLARE FUNCTION Entry () |
|||
FUNCTION Entry () |
|||
n$ = "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" |
|||
FOR i = 1 TO LEN(n$) |
|||
IF INSTR("[] ,",MID$(n$,i,1)) = 0 THEN |
|||
flatten$ = flatten$ + c$ + MID$(n$,i,1) |
|||
c$ = ", " |
|||
END IF |
|||
NEXT i |
|||
PRINT "[";flatten$;"]" |
|||
END FUNCTION |
|||
END PROGRAM</syntaxhighlight> |
|||
{{out}} |
|||
<pre>[1, 2, 3, 4, 5, 6, 7, 8]</pre> |
|||
=={{header|Yabasic}}== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="yabasic">sString$ = "[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" |
|||
For siCount = 1 To Len(sString$) |
|||
If Instr("[] ,", Mid$(sString$, siCount, 1)) = 0 Then |
|||
sFlatter$ = sFlatter$ + sComma$ + Mid$(sString$, siCount, 1) |
|||
sComma$ = ", " |
|||
End If |
|||
Next siCount |
|||
Print "[", sFlatter$, "]" |
|||
End</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Igual que la entrada de FreeBASIC. |
|||
</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Line 4,198: | Line 4,178: | ||
//-->L(1,2,3,4,5,6,7,8)</syntaxhighlight> |
//-->L(1,2,3,4,5,6,7,8)</syntaxhighlight> |
||
This works by recursively writing the contents of lists to a new list. If a list is recursive or cyclic, it will blow the stack and throw an exception. |
This works by recursively writing the contents of lists to a new list. If a list is recursive or cyclic, it will blow the stack and throw an exception. |
||
=={{header|ZX Spectrum Basic}}== |
|||
{{incorrect|ZX Spectrum Basic| The task is not in string translation but in list translation.}} |
|||
<syntaxhighlight lang="zxbasic">10 LET f$="[" |
|||
20 LET n$="[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8 []]" |
|||
30 FOR i=2 TO (LEN n$)-1 |
|||
40 IF n$(i)>"/" AND n$(i)<":" THEN LET f$=f$+n$(i): GO TO 60 |
|||
50 IF n$(i)="," AND f$(LEN f$)<>"," THEN LET f$=f$+"," |
|||
60 NEXT i |
|||
70 LET f$=f$+"]": PRINT f$</syntaxhighlight> |