################################################################ # PERL CLASS 3 # # Examples from class # ################################################################ while ($line = ) #a while loop to print Hello, followed by the line from the screen { chomp ($line); print "Hello, $line\n"; } print "Goodbye\n"; #print goodbye at end of the loop @animals = qw(lion tiger turtle elephant); #creates an array print "@animals\n"; #prints the array @newanimals = @animals; #copies array into a new array print "@newanimals\n"; #prints the new array ($first) = @animals; #gets the first element of an array print "$first\n"; ($first, @rest) = @animals; #assigns $first to the first element and @rest to the rest of the array print "@rest\n"; #print the rest of the array $newanimals[2] = "camel"; #changes the third element in the array to "camel" print "@newanimals\n"; #prints the array after changing it push @newanimals, "zebra"; #adds a new element to the end of the array print "@newanimals\n"; #prints the array after changing it while ($line = ) { chomp $line; push @newanimals, $line; #adds animals from the display to the array } print "@newanimals\n"; #prints the array after adding new elements @friends = qw (john mike sandy misho); #creates an array print "@friends\n"; @friends2 = reverse @friends; #creates a new array be reversing an existing array print "@friends2\n"; foreach $x (@friends2) #access each element of the array { print "$x\n"; #then print it on a separate line } for ($i = 0; $i < @friends2; ++$i) #same thing as the previous one { print $friends2[$i]; }