Strip comments from a string: Difference between revisions

Add RPL section
(Added XPL0 example.)
(Add RPL section)
Line 1,886:
return cList
</syntaxhighlight>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
'''IF''' DUP 1 1 SUB " " == '''THEN'''
2 OVER SIZE SUB '''TRIMF END'''
'''IF''' DUP DUP SIZE DUP SUB " " == '''THEN'''
1 OVER SIZE 1 - SUB '''TRIMF END'''
≫ ‘'''TRIMF'''’ STO
≪ → string charset
≪ 0 1 charset SIZE '''FOR''' j
'''IF''' string charset j DUP SUB POS
'''THEN''' LAST SWAP DROP charset SIZE 'j' STO '''END'''
'''NEXT'''
≫ ≫ ''''STPOS'''' STO
≪ SWAP
'''IF''' DUP "#;" '''STPOS THEN'''
1 LAST 1 - SUB '''IF''' SWAP '''THEN TRIMF END'''
'''ELSE''' SWAP DROP '''END'''
≫ ''''NOCOM'''' STO
|
'''TRIMF''' ''( "string" -- "trimmed" ) ''
if flag 1 set and 1st char of string is a space
then recursively process tail(string)
if flag 1 set and last char of string is a space
then recursively process head(string)
'''STPOS''' ''( "string" "char_set" -- position ) ''
position = 0, for each character in set
if char in string
update position, request loop exit
'''NOCOM''' ''( "string" mode -- "no_comment" )''
if there is a comment
remove it from string, and spaces too if required
otherwise clean stack
|}
{{in}}
<pre>
" apple, pears # and bananas" 0 NOCOM
" apple, pears # and bananas" 1 NOCOM
" apple, pears ; and bananas" 1 NOCOM
</pre>
{{out}}
<pre>
3: " apple, pears "
2: "apple, pears"
1: "apple, pears"
</pre>
 
=={{header|Ruby}}==
1,151

edits