Difference between revisions of "Slice"
Jump to navigation
Jump to search
(Created page with "<c>slice</c> returns a subset of a string or list. This is equivalent to using the square-brackets slice operator seq[from:to], but with ordinary function syntax. === Argume...") |
|||
| (2 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
| − | <c>slice</c> returns a subset of a string or list. This is equivalent to using the square-brackets slice operator seq[from:to], but with ordinary | + | <c>slice</c> returns a subset of a string or list. This is equivalent to using the square-brackets slice operator <c>seq[from:to]</c>, but with ordinary |
function syntax. | function syntax. | ||
| Line 7: | Line 7: | ||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
| − | ! Parameter Name !! | + | ! Parameter Name !! Type !! Meaning |
|- | |- | ||
| ''seq'' || string or list || sequence to get a subsequence of | | ''seq'' || string or list || sequence to get a subsequence of | ||
| Line 15: | Line 15: | ||
| ''to'' || number, optional || 0-based index of first element to *not* include in the result (if negative, count from the end; if omitted, return the rest of the sequence) | | ''to'' || number, optional || 0-based index of first element to *not* include in the result (if negative, count from the end; if omitted, return the rest of the sequence) | ||
|} | |} | ||
| + | |||
| + | == Usage Notes == | ||
| + | |||
| + | The ''from'' and ''to'' indexes are automatically limited to the range of the sequence; they never cause an Index Error. This provides for a useful idiom: to get element ''i'' of a sequence, or an empty value if sequence is too short to have such an element, use <c>seq[i:i+1]</c>. This differs from <c>seq[i]</c> in that the latter will result in an Index Error if ''i'' is out of range. | ||
== Example == | == Example == | ||
| − | <ms>slice("Hello", -2) | + | <ms>slice("Hello", -2) // returns "lo" |
| − | slice( | + | "Hello"[-2:] // equivalent to the above |
| + | |||
| + | slice("x", -2) // returns "x" | ||
| + | "x"[-2:] // equivalent to the above | ||
| + | |||
| + | slice([10,20,30,40,50], 1, 3) // returns [20, 30] | ||
| + | [10,20,30,40,50][1:3] // equivalent to the above</ms> | ||
[[Category:Intrinsic Functions]] | [[Category:Intrinsic Functions]] | ||
[[Category:List Methods]] | [[Category:List Methods]] | ||
[[Category:String Methods]] | [[Category:String Methods]] | ||
Latest revision as of 20:11, 6 May 2024
slice returns a subset of a string or list. This is equivalent to using the square-brackets slice operator seq[from:to], but with ordinary
function syntax.
Arguments
| Parameter Name | Type | Meaning |
|---|---|---|
| seq | string or list | sequence to get a subsequence of |
| from | number, default | 0-based index to the first element to return (if negative, counts from the end) |
| to | number, optional | 0-based index of first element to *not* include in the result (if negative, count from the end; if omitted, return the rest of the sequence) |
Usage Notes
The from and to indexes are automatically limited to the range of the sequence; they never cause an Index Error. This provides for a useful idiom: to get element i of a sequence, or an empty value if sequence is too short to have such an element, use seq[i:i+1]. This differs from seq[i] in that the latter will result in an Index Error if i is out of range.
Example
slice("Hello", -2) // returns "lo"
"Hello"[-2:] // equivalent to the above
slice("x", -2) // returns "x"
"x"[-2:] // equivalent to the above
slice([10,20,30,40,50], 1, 3) // returns [20, 30]
[10,20,30,40,50][1:3] // equivalent to the above