How to Use the Foreach Statement in Perl

The Perl foreach function, or statement, is used to loop through arrays and hashes. Foreach takes the array as the parameter and loops though each of its values.

Instructions

    • 1

      Specify the foreach keyword followed by the array or hash to loop over, with the statements to be executed each time through the loop following within curly brackets.

      @groceryList = ("Apples","Milk","Cheese", "Eggs", "Steak");
      foreach (@groceryList)
      {
        print $_;
      }

    • 2

      Use "$_" to refer to the value that the loop is currently on, as in the previous step.

Related Searches:

Resources

Comments

You May Also Like

Related Ads

Featured