Perl Programming - Using the foreach loop on arrays and lists
Get the entire Perl Programming course for 20% off: http://stoneriverelearning.com/courses/perl-programming-for-beginners-online-course?product_id=38639&coupon_code=YOUTUBE20
Welcome back, guys! In this lecture, we’re going to be using the foreach loop on arrays and list. Again, this is section 4, lecture 7. Let’s jump right into it.
What is a foreach loop? Well, the foreach loop is just a control structure that repeats a block of code for each element inside an array. Again, it’s just another type of iteration loop just like the if control structure and the while control structure but the difference between this for loop is that it repeats the block of code for each value or element inside our array. It executes one time through the loop for each value. So for each element or value in the array, it executes the block of code within the curly braces.
For example, how we set up the structure, we have to define an array first. We start off with our @ character followed by the array name. I just called it names. I’m going to assign it some value, so using our qw operator, I pass in three strings which is Victor Jaddian Davis separated by spaces. Of course, we use our foreach control structure, but the f is lowercase. After we declare our foreach keyword, we put a variable name. So we declare another variable. Inside our parentheses this is where we pass in our array with our values. So again, it’s going to loop through this curly braces each time that it finds a value or element in our array. It’s going to print Victor Jaddian Davis, all three of them inside our loop.
Let me show you some examples. Again, we start by declaring our array and assigning it some values. I’ll declare my array variable. This time I’ll call it names equal then the list of values. So I’ll use my qw followed by two forward slashes, a space just for nice formatting. I’ll type Victor. I’ll out Jaddian and Forte. Now I want to use my foreach structure. I’ll use the keyword forech. I’ll declare a variable to save our value and our elements of our array. I’ll just call it name followed by parentheses. The array we’re going to use of course is called names, so I’ll copy this variable. I’ll paste it into our parentheses. Open and close curly braces. I just want to print the names inside. How I’ll do this I’ll use the name value so I’ll paste that in there. Let’s just say I’ll put it on each new line. If we save that, what I’m telling Perl to do is for each element inside our names array, I want to print the value each time it finds an element. Again, we have 1 2 3. Three elements and three values. So we’re going to print each name. If I go up to the menu Run, click Run Script, that’s exactly what we have. Victor Jaddian Forte.
That’s pretty much in a nutshell how we use the foreach loop. It loops through in an entire array for each value that it finds or each element it finds in our array. Let’s say if you want to add more value, I’ll call it Jackson Harris 05 40. I’ll save that. Again, we click Run, Run Script. It prints all of our values with a new line character. Again, we use the keyword foreach to begin our foreach loop. We declare a variable to assign our value with or to associate our value with inside our elements or our elements inside of our array, and we just want to print out the values. That’s pretty much it in a nutshell for foreach loop. If you guys have any questions, let me know. Feel free to ask questions and I’ll see you guys in our next lecture.