1 00:00:00,250 --> 00:00:05,010 Okay, we have finding a contact by name working. 2 00:00:05,010 --> 00:00:08,780 Now let's go ahead and implement finding by a phone number. 3 00:00:08,780 --> 00:00:11,860 And this is gonna be relatively similar. 4 00:00:20,401 --> 00:00:23,779 We can start by creating an empty results array and 5 00:00:23,779 --> 00:00:28,670 when we do that this variable is gonna be local to just this method. 6 00:00:28,670 --> 00:00:32,280 So no other methods are gonna have access to it unless we pass it. 7 00:00:34,580 --> 00:00:38,820 And now we're gonna be searching so we'll be searching by whatever is sent in, 8 00:00:38,820 --> 00:00:41,450 in this case it's a number like a phone number. 9 00:00:42,470 --> 00:00:46,800 And to make this search a little bit better what we're gonna do is replace any 10 00:00:46,800 --> 00:00:51,230 dashes in the string with nothing. 11 00:00:51,230 --> 00:00:53,290 And we do that using the G sub method. 12 00:00:54,500 --> 00:01:00,130 So we replace a dash with nothing and that gets assigned to a new variable. 13 00:01:01,180 --> 00:01:06,120 So if we were to send in something like 111-111 that would go 14 00:01:06,120 --> 00:01:09,600 into the search as the string 111111. 15 00:01:09,600 --> 00:01:12,303 So now we can do the same thing that we did above, 16 00:01:12,303 --> 00:01:14,270 we can loop through the contacts. 17 00:01:19,029 --> 00:01:22,966 And now we have to loop through the contact phone numbers this time, 18 00:01:22,966 --> 00:01:25,620 to see if the phone number is included. 19 00:01:25,620 --> 00:01:27,300 So we look through the phone number here. 20 00:01:31,550 --> 00:01:35,050 And now just like in the find name method 21 00:01:35,050 --> 00:01:38,660 where we had to down case it before we found it. 22 00:01:38,660 --> 00:01:41,450 We're looking for the same thing, so 23 00:01:41,450 --> 00:01:44,730 this time we have to manipulate it the same way here. 24 00:01:46,660 --> 00:01:52,518 So we'll say if the phone_number.number that when 25 00:01:52,518 --> 00:01:59,167 we replace the dashes with nothing Include our search term. 26 00:02:00,831 --> 00:02:04,670 Then we can append this contact to the results array. 27 00:02:06,090 --> 00:02:09,610 And now all we have to do is print out the results. 28 00:02:11,090 --> 00:02:13,150 And I can just copy this. 29 00:02:15,240 --> 00:02:16,540 And paste it. 30 00:02:22,471 --> 00:02:26,420 And this time I just say phone search results instead of name search results. 31 00:02:29,605 --> 00:02:34,505 So now we can comment this out and we'll say address 32 00:02:34,505 --> 00:02:39,585 book, find by phone number. 33 00:02:42,025 --> 00:02:45,688 Let's just see if this works by doing 222. 34 00:02:45,688 --> 00:02:49,930 So when we run this, it should print out Nick's contact information. 35 00:02:51,830 --> 00:02:53,160 Okay, and there we go. 36 00:02:53,160 --> 00:02:55,910 Nick's contact info is correctly printed out. 37 00:02:58,520 --> 00:03:02,940 Now if we search for just the number two, 38 00:03:02,940 --> 00:03:07,761 since both of us have that as a phone number. 39 00:03:07,761 --> 00:03:11,622 Clear my screen, we should get both myself and Nick returned. 40 00:03:14,161 --> 00:03:16,317 And we do. 41 00:03:17,625 --> 00:03:20,362 So the last thing that we need to do here, 42 00:03:20,362 --> 00:03:24,480 is you'll notice that we are repeating the same logic here. 43 00:03:24,480 --> 00:03:27,150 We're printing out the search results. 44 00:03:27,150 --> 00:03:30,710 Along with a little label for the search. 45 00:03:30,710 --> 00:03:35,318 We're doing this in both the find by name and find by phone number methods. 46 00:03:37,046 --> 00:03:40,289 So let's go ahead and create a method called print results, 47 00:03:45,814 --> 00:03:50,038 Which will take a label for the search. 48 00:03:50,038 --> 00:03:52,408 And an array of results. 49 00:03:56,502 --> 00:03:59,800 And what this is gonna do is de-duplicate our code. 50 00:03:59,800 --> 00:04:03,450 So, if we ever wanna change it we only have to change it in one spot 51 00:04:03,450 --> 00:04:05,360 that we'll call from the individual methods. 52 00:04:07,280 --> 00:04:08,610 So, we'll print out the search. 53 00:04:10,220 --> 00:04:13,370 And since this is the same in each of these. 54 00:04:14,470 --> 00:04:19,685 I'm gonna cut that from there paste it into this method. 55 00:04:25,955 --> 00:04:31,600 And now I can say print_results. 56 00:04:31,600 --> 00:04:37,641 And that will have the label, and then this results array. 57 00:04:41,737 --> 00:04:43,635 And then I can do the same thing here. 58 00:04:54,643 --> 00:04:55,810 So now we're going to clear the screen. 59 00:04:59,040 --> 00:05:00,948 And we can print this both times here. 60 00:05:07,815 --> 00:05:13,172 Now we should see two different searches when we print this out. 61 00:05:15,496 --> 00:05:17,225 Oh that's hard to see, let's do this one more time. 62 00:05:19,936 --> 00:05:22,430 Okay, phone search results two. 63 00:05:26,611 --> 00:05:28,022 Would help if I saved the file. 64 00:05:32,990 --> 00:05:35,275 Now scroll back up here. 65 00:05:35,275 --> 00:05:38,910 Name search results for E, Jason Seifer, Nick Pettit. 66 00:05:40,040 --> 00:05:42,380 Phone search results two. 67 00:05:42,380 --> 00:05:47,610 We have Jason Seifer, Jason Seifer, and Nick Pettit. 68 00:05:47,610 --> 00:05:49,350 Why are I appearing in here twice? 69 00:05:51,630 --> 00:05:56,150 Well that is because my phone number is included twice. 70 00:05:57,540 --> 00:06:00,800 You'll see that we loop through the phone numbers and 71 00:06:00,800 --> 00:06:03,380 two of my phone numbers have the number two. 72 00:06:06,670 --> 00:06:14,110 So if we wanted to we could say append to the results.contact, 73 00:06:14,110 --> 00:06:17,155 unless that contact is already included. 74 00:06:19,255 --> 00:06:24,085 So I'm gonna clear the screen and run this address book one more time. 75 00:06:24,085 --> 00:06:28,428 And you'll notice that that perfectly deduplicates the search results.