Locked lesson.
About this lesson
We'll learn to output the contents of a file into an array.
Exercise files
Download this lesson’s related exercise files.
Open a File into an Array - Solution.docx60.9 KB Open a File into an Array.docx
60.8 KB
Quick reference
Open a File into an Array
Once you open a file, you can do all sorts of things with it's content.
When to use
Use this whenver you want to stuff the contents of a file into an array.
Instructions
First, we need to create an empty array:
my_array = [ ]
Then you need to loop etc:
# Open the file
my_file = File.open("stuff.txt")
# Create an empty array
my_array = []
# Loop and push stuff into array
while ! my_file.eof?
stuff = my_file.gets.chomp
my_array << stuff
end
# Output the array to the screen
puts my_array
# Close the file
my_file.close
Hints & tips
- To create an empty array: my_array = [ ]
- To add items to an array: my_array << "Bob"
- 00:04 In the last video, we opened a file, and
- 00:06 then we puts-ed the stuff from the file out to the screen.
- 00:08 So that's useful, but what if we want to do stuff with the things in that file?
- 00:14 How can we do that?
- 00:16 Well, one way, I told you in the last video,
- 00:18 there's lots of different ways you can do stuff with files.
- 00:21 You can do this while loop, or you could do all kinds of other things.
- 00:24 We just puts it out to the screen, we could also put that stuff into an array,
- 00:27 and then do stuff with the array later on.
- 00:30 So let's come up here to our stuff.txt file, and
- 00:32 I'm just going to get rid of this Customer List heading.
- 00:35 So let's just say we've got a list of names here, so I'm going to save this and
- 00:39 close it, and we get rid of this puts my_file thing,
- 00:41 that just output this thing onto the screen.
- 00:44 So we don't need that anymore.
- 00:45 So what do we need to do?
- 00:46 Well, first off, we need to create an empty array.
- 00:49 And to do that, it's just like creating any array, you just name the array,
- 00:53 I'm just going to call it my_array = and then you just put your brackets,
- 00:57 and then you leave them blank.
- 00:59 So there's nothing in this array.
- 01:01 Now, I don't think we talked about it when we talked about arrays, but
- 01:05 in order to put things into an array later on, you just name the array, my_array.
- 01:10 And then you do these two little arrows, and then Bob,
- 01:14 whatever you want to put in there.
- 01:16 These arrows kind of shove this into your array.
- 01:20 That's sort of how I think of it,
- 01:22 the arrows kind of push this Bob into the array, so that's how you do that.
- 01:26 So in order to put our stuff from the file into the array,
- 01:30 all we have to really do is modify this line.
- 01:33 So let's go my_array, and then what do we want to put in there?
- 01:37 Well, we're taking all this stuff from our file, and
- 01:40 we're gets-ing it line by line and chomping each thing.
- 01:43 And we're stuffing it in this stuff variable name,
- 01:45 which is not a great variable name.
- 01:47 So what we want to do is we want to put this stuff into this array, so
- 01:51 we just type stuff.
- 01:52 So if we save this and run it, well first, what do you think's going to happen?
- 01:56 And the answer is nothing, because we've put the stuff into our array, but
- 02:00 we haven't actually done anything with the array.
- 02:03 So that's an easy thing to fix, all we have to do is my_array, and then, well,
- 02:08 let's just puts it and see what happens, first of all.
- 02:11 And if you think back to our video on arrays, whenever we puts an array,
- 02:15 it just prints out everything in the array.
- 02:18 So if we run this again, here we get John, Tim, Bob, Mary, and Bluto, which is John,
- 02:22 Tim, Bob, Mary, and Bluto, so that looks good.
- 02:25 Now we can also pull any specific thing we want out of the array.
- 02:29 So if we want, let's say we want Bob, and Bob is going to be the 0,
- 02:33 1, 2, the second thing in our array.
- 02:35 So we just type in 2, save this, run it again, boom, Bob, so that's pretty simple.
- 02:40 It's just that easy to stuff our stuff into an array.
- 02:43 You could put it into a hash if you wanted to.
- 02:46 Remember back to the hash video, we know how to stuff things into hashes,
- 02:50 after the fact, you can do that, too.
- 02:52 So lots of different things you can do,
- 02:54 lots of different options whenever you're opening a file.
- 02:58 It really just sort of depends on the kind of data that's in the file,
- 03:00 it's going to determine what you want to do with it.
- 03:02 If you want to just print it out to the screen,
- 03:05 if you want to stuff it into an array and use each line for certain things,
- 03:09 whatever you want, you have lots of options.
- 03:12 So that's all for this video.
- 03:14 In the next video, we're going to look at open file modes.
- 03:17 Whenever you open a file, there's lots of different modes you can use,
- 03:20 depending on what you want to do with it, and
- 03:21 that's what we going to talk about in the next video.
Lesson notes are only available for subscribers.