#!/usr/bin/perl -w ####################################################### # class2_10_foreach1 # # playing with foreach - part 1 # # modified by Sharon Tuttle from "Learning Perl", # by Schartz and Phoenix # # last modified: 4-11-03 ####################################################### print "give beginning of range: "; chomp($left_val = ); print "give ending of range: "; chomp($right_val = ); # swap range ends if user got 'em backwards! if ($left_val > $right_val) { ($right_val, $left_val) = ($left_val, $right_val); } @myArr = ($left_val..$right_val); # print each value in @myArr on its own line print "\@myArr contains:\n"; foreach $val ( @myArr ) { print "$val\n"; } # print each value in a literal list on its own line print "\nSome names: \n"; foreach $val ( qw/ George Harold Jerome /) { print "$val\n"; } # use UNIX command ls to generate the files in the # current working directory --- and print THEM # one per line print "\nMan! Look at all this junk:\n"; foreach $file ( `ls` ) { print "$file"; # guess what? ls returns LINES of results... } # question: if I modify loop control variable, am I changing # the underlying array/list, OR am I changing a copy of that # latest element? foreach $val ( @myArr ) { $val += 100; } print "\n\@myArr: @myArr\n"; # end of class2_10_foreach1