text
stringlengths
0
897
Q> If you do not have courage, you may not have the opportunity to use any of your other virtues.
Q>— Samuel L. Jackson (actor and producer)
A> **Chapter Objectives**
A> - Learn how to extract a section of a list or a string.
A> - See the many ways in which this "slicing" can be useful.
A> - Learn to write a longer script as a file, so you can run it again and again.
We saw in the last chapter how you could access any element of a list or string by putting its 0-based index in square brackets. We're going to build on that in some really cool ways, so let's start with a quick review. Fire up command-line MiniScript, and try the following at the REPL:
```
s = "Howdy world!"
s[0]
```
That should print "H", since the character in `s` with 0 letters to its left is "H". What would `s[3]` print? What about `s[-1]`, recalling that negative indexes count from the end of the string? Check each of these in the REPL to make sure you're right.
D> If you're not at a computer, following along with these examples in the REPL, they are unlikely to stick. Don't waste your time! No runner ever got fast by watching other people run.
Now the coolness begins.
## Slicing
In MiniScript, as well as some other languages (e.g. Python), you can index into a string or list with *two* indexes instead of just one. When you do, you get back the subset that starts at the first index, and goes up to (but not including) the second one. This operation is known as *slicing*, and what you get back ...
slice
: a substring or sublist defined by start and end indexes in a larger string or list
slicing
: the operation of getting a slice: `seq[a:b]`, where `seq` is a string or list, and `a` and `b` are numbers
You get a slice by putting the two indexes together within the square brackets, separated by a colon. Try it:
```
s[0:3]
```
This should print "How", which is the substring starting at `s[0]`, and going up to but not including `s[3]`. Now see if you can get the slice "world", starting with "w" and going up to but not including "!". Go on. I'll wait.
D> At times like this, the REPL is your dearest friend. Even when you are an experienced programmer with decades of skillful code-craft under your belt, when you need to quickly confirm your understanding of some language feature like slicing, there is no better way than launching a REPL and trying a few quick example...
Did you come up with `s[6:11]`? That gets the job done! But to get that you probably either had to try several times, or count carefully from the beginning of the string. There is another way, using the trick that negative indices count from the end of the string. `s[-1]` is the exclamation point of our string, so ...
```
s[6:-1]
```
This returns the substring starting at character 6, and going up to but not including the last character of the string. You can use a negative number with either index, or both, as needed.
In addition to a numeric index (whether negative or not), you can also *omit* either index. If you leave out the first index, the slice starts at the beginning of the string; if you leave out the second index, the slice extends to the end of the string. These are very commonly needed, so this is a convenient shortcut...
```
s[0:5]
```
but you can also write:
```
s[:5]
```
which you might read out loud as "s (up to 5)". These are equivalent, though the second form is slightly shorter, and makes your intent slightly more clear. The benefit is a bit greater when you omit the second index. Suppose we want to get the slice "world!" from our "Howdy world!" string... but we are too lazy to ...
```
s[6:s.len]
```
but it's shorter, easier, and faster to do
```
s[6:]
```
which you might read as "s (from 6 to the end)".
These sorts of string operations come up in programming a lot more than you might think. Suppose you had a filename with a three-letter extension, which is something like ".txt" or ".png" or ".jpg". You might want to get the name without the extension, to display a nice name to the user; or you might want to get the ...
{caption:"Getting the name and file extension from a file name"}
```miniscript
filename = "My Recipes.txt"
name = filename[:-4]
ext = filename[-3:]
```
Enter that in the REPL, and then enter `name` and `ext` by themselves to see what values they have. `filename[:-4]` gets the original string up to (but not including) the 4th character from the end, or in other words, it strips off the last four characters. Conversely, `filename[-3:]` gets the last three characters o...
All our examples so far have been with strings, but slicing works with lists, too. For this next batch of examples, let's work with a list of the first ten prime numbers.
```
p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
```
Now, because we know this list is already sorted, you can use `p[0]` to find the smallest number, and `p[-1]` to find the largest. (Try it!) That much you knew yesterday. But now you can get, say, the *three* smallest prime numbers:
```
p[:3]
```
Similarly, suppose you wanted the list of all primes *except* for the first and last. You only want the ones in the middle. Again with slicing, this is easy: