# HG changeset patch # User Steve Losh # Date 1319602723 14400 # Node ID 1b686fa14d4a052d89d60fecdc2b78ebf3c6be24 # Parent 44918d574627f9ac69106316f1340c4336bb238f Lists. diff -r 44918d574627 -r 1b686fa14d4a chapters/35.markdown --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapters/35.markdown Wed Oct 26 00:18:43 2011 -0400 @@ -0,0 +1,154 @@ +Lists +===== + +We've worked a lot with variables so far, but we haven't talked about aggregates +at all yet! Vim has two main aggregate types, and we'll look at the first now: +lists. + +Vimscript lists are ordered, heterogeneous collections of elements. Run the +following command: + + :echo ['foo', 3, 'bar'] + +Vim displays the list. Lists can of course be nested. Run the following +command: + + :echo ['foo', [3, 'bar']] + +Vim happily displays the list. + +Indexing +-------- + +Vimscript lists are zero-indexed, and you can get at the elements in the usual +way. Run this command: + + :echo [0, [1, 2]][1] + +Vim displays "[1, 2]". You can also index from the end of the list, much like +Python. Try this command: + + :echo [0, [1, 2]][-2] + +Vim displays `0`. The index `-1` refers to the last element in the list, `-2` +is the second-to-last, and so on. + +Slicing +------- + +Vim lists can also be sliced. This will *look* familiar to Python programmers, +but it does *not* always act the same way! Run this command: + + :echo ['a', 'b', 'c', 'd', 'e'][0:2] + +Vim displays "['a', 'b', 'c']" (elements 0, 1 and 2). You can safely exceed the +upper bound as well. Try this command: + + :echo ['a', 'b', 'c', 'd', 'e'][0:100000] + +Vim simply displays the entire list. + +Slice indexes can be negative. Try this command: + + :echo ['a', 'b', 'c', 'd', 'e'][-2:-1] + +Vim displays "['d', 'e']" (elements -2 and -1). + +When slicing lists you can leave off the first index to mean "the beginning" +and/or the last index to mean "the end". Run the following commands: + + :echo ['a', 'b', 'c', 'd', 'e'][:1] + :echo ['a', 'b', 'c', 'd', 'e'][3:] + +Vim displays "['a', 'b']" and "['d', 'e']". + +Like Python, Vimscript allows you to index and slice strings too. Run the +following command: + + :echo "abcd"[0:2] + +Vim displays "abc". + +Concatenation +------------- + +You can combine Vim lists with `+`. Try this command: + + echo ['a', 'b'] + ['c'] + +Vim, unsurprisingly, displays "['a', 'b', 'c']". There's not much else to say +here -- Vimscript lists are surprisingly sane compared to the rest of the +language. + +List Functions +-------------- + +Vim has a number of built-in functions for working with lists. Run these +commands: + + :let foo = ['a'] + :call add(foo, 'b') + :echo foo + +Vim mutates the list `foo` in-place to append `'b'` and displays "['a', 'b']". +Now run this command: + + :echo len(foo) + +Vim displays "2", the length of the list. Try these commands: + + :echo get(foo, 0, 'default') + :echo get(foo, 100, 'default') + +Vim displays "a" and "default". The `get` function will get the item at the +given index from the given list, or return the given default value if the index +is out of range in the list. + +Run this command: + + :echo index(foo, 'b') + :echo index(foo, 'nope') + +Vim displays "1" and "-1". The `index` function returns the first index of the +given item in the given list, or `-1` if the item is not in the list. + +Now run this command: + + :echo join(foo) + :echo join(foo, '---') + :echo join([1, 2, 3], '') + +Vim displays "a b" and "a---b". `join` will join the items in the given list +together into a string, separated by the given separator string (or a space if +none is given), coercing each item to a string if necessary/possible. + +Run the following commands: + + :call reverse(foo) + :echo foo + :call reverse(foo) + :echo foo + +Vim displays "['b', 'a']" and then "['a', 'b']". `reverse` reverses the given +list *in place*. + +Exercises +--------- + +Read `:help List`. All of it. Notice the capital "L". + +Read `:help add()`. + +Read `:help len()`. + +Read `:help get()`. + +Read `:help index()`. + +Read `:help join()`. + +Read `:help reverse()`. + +Skim `:help functions` to find some other list-related functions I haven't +mentioned yet. Run `:match Keyword /\clist/` to case-insensitively highlight +the word "list" to make it easier to find what you're looking for. diff -r 44918d574627 -r 1b686fa14d4a outline.org --- a/outline.org Tue Oct 25 23:24:02 2011 -0400 +++ b/outline.org Wed Oct 26 00:18:43 2011 -0400 @@ -32,7 +32,7 @@ ** DONE execute normal! ** DONE basic regexes ** DONE Case Study: GrepMotion -** TODO lists +** DONE lists ** TODO looping ** TODO dictionaries ** TODO advanced regexes diff -r 44918d574627 -r 1b686fa14d4a proofreader-volunteers.txt --- a/proofreader-volunteers.txt Tue Oct 25 23:24:02 2011 -0400 +++ b/proofreader-volunteers.txt Wed Oct 26 00:18:43 2011 -0400 @@ -4,3 +4,4 @@ @robhudson @cemicolon @execat +@alesplin