Accessing Items in Arrays4:42 with Jason Seifer
Now that we've seen how to create arrays and add items to them, we're going to learn how to work with existing arrays and access what's inside them.
Index: The numbered position of an item in an array. Arrays all start with the first item having an index of 0.
Starting with the following array:
array = ["milk", "eggs", "bread", "ice cream", "pie", "potatoes"]
Access "milk", which is at the first position in the array (index 0), and assign it to a variable named "item":
item = array
Access the second item in the array (index 1):
Print out the first item in the array using the first method:
Print out the last item in the array, using the negative index and also the "last" method:
puts array[-1] puts array.last
The fetch method can be used with an index to return that item in an array:
puts array.fetch(2) # => "bread"
If a second argument is provided to the fetch method and there is no corresponding item in the array, the second argument will be used as a default:
puts array.fetch(20, "cake") # => "cake"
Return the number of items in an array:
Return the number of items in the array matching what you send in:
array.count("eggs") # => 1
To find out if an array contains a particular item, use the include? method with the argument of the desired item:
array.include?("eggs") # => true
So far, we've created arrays and added items to them. 0:00 What happens if we wanna get just certain pieces of data back out of the array? 0:05 We do that by asking the array for just a certain part of itself. 0:10 An item's place in an array is called the index. 0:16 And just to be complicated, the index starts with the number zero. 0:20 Let's say that we have our grocery list array with three items in it, milk, eggs, 0:25 and bread. 0:30 Milk would be at index zero, eggs at index one, and bread at index two. 0:32 Once we know how indexes work, we have a lot of flexibility in how we ask for 0:39 things in an array. 0:43 Let's go ahead and see how that works now using WorkSpaces. 0:45 So here I am inside of a Ruby WorkSpace. 0:49 So let's go ahead and 0:53 bring the console up and practice working with arrays inside of irb. 0:54 Let's go ahead and create a grocery list array. 1:00 We're gonna say that that has milk, eggs, bread, ice cream, 1:03 pie, and potatoes. 1:09 Now when we look at this array, 1:13 we can see that it has six different items inside of it. 1:14 If wanted to access the first item inside of the array, we do that 1:18 by typing the name of the array and then an open bracket and the index. 1:23 Now remember, indexes start from zero, so 1:29 if we want the first item, we have to type the number zero, which returns milk. 1:33 Likewise, if we wanted the second item, we would use an open bracket, type 1:40 the number one, and then a close bracket, and that will return the second item. 1:45 Now in addition to using brackets, we can also use the method 1:51 at and then pass that the argument of the index. 1:56 And it will return the item at that index inside of the array. 2:01 So we could say item is the grocery list at index one. 2:09 And now our item will still have the grocery list item at index one. 2:13 And if we look at our grocery list, it still contains all of the different items. 2:20 Now I'm going to clear my screen here, and let's just look at our grocery list so 2:25 we know what we're looking at. 2:29 There's some special methods for accessing the first and 2:31 last items in the array, and those methods are first and last. 2:36 Another way of accessing the last item in the array, 2:41 is typing out the variable name [-1]. 2:47 Which goes in the reverse order of the index. 2:52 We can also use -2 and so on. 2:57 So I just cleared my screen here, and now that we know how to access different ideas 3:01 in the array we can do cool things like insert items at a given position. 3:05 For example, if I wanted to insert oatmeal into this array and 3:12 I wanted to insert it after bread, I could say grocery_list.insert, 3:17 give it the index and then, the item to insert there. 3:22 And now, we've added oatmeal into the grocery list array at the index of two. 3:28 We could also ask our grocery list how many items are inside of it. 3:34 By using the length method. 3:40 An alias for that is count. 3:43 We can do something else kind of cool by asking for 3:47 the count, of a certain item in the array. 3:50 So if we want to see how many times eggs appears inside of this array. 3:54 We get that one time. 3:58 So if we add it in there twice, you'll see it appears in there twice. 4:03 And then if we ask how many times eggs appears in this array, 4:09 we get the number 2. 4:14 We could also use the method, include, to see if something is included in the array. 4:17 So does this array include the string eggs, and it does. 4:24 But if we ask if it includes water It does not. 4:33 Try working with array indexes now on your own using WorkSpaces. 4:37
You need to sign up for Treehouse in order to download course files.Sign up