String interpolation (included): Difference between revisions

Content added Content deleted
Line 628: Line 628:
=={{header|APL}}==
=={{header|APL}}==
<lang apl>
<lang apl>
s ← 'Mary had a _ lamb' ⋄ s[s⍳'_'] ← ⊂'little' ⋄ s ← ∊s
s ← 'Mary had a lamb' ⋄ s[s⍳''] ← ⊂'little' ⋄ s ← ∊s
s
s
Mary had a little lamb
Mary had a little lamb
Line 637: Line 637:
∇r ← s sInterp sv
∇r ← s sInterp sv
⍝⍝ Interpolate items in sv into s (string field substitution)
⍝⍝ Interpolate items in sv into s (string field substitution)
⍝ s: string - format string, '_' (underscore) interpolation points
⍝ s: string - format string, '' used for interpolation points
⍝ sv: vector - vector of items to interpolate into s
⍝ sv: vector - vector of items to interpolate into s
⍝ r: interpolated string
⍝ r: interpolated string
s[('_'=s)/⍳⍴s] ← ⊃¨(⍕¨sv)
s[(''=s)/⍳⍴s] ← ⊃¨(⍕¨sv)
r ← ∊s
r ← ∊s
'Mary had a _ lamb, its fleece was _ as _.' sInterp 'little' 'black' 'night'
'Mary had a lamb, its fleece was as .' sInterp 'little' 'black' 'night'
Mary had a little lamb, its fleece was black as night.
Mary had a little lamb, its fleece was black as night.
'Mary had a _ lamb, its fleece was _ as _.' sInterp 'little' 'large' 42
'Mary had a lamb, its fleece was as .' sInterp 'little' 'large' 42
Mary had a little lamb, its fleece was large as 42.
Mary had a little lamb, its fleece was large as 42.
</lang>
</lang>