#!/usr/bin/env perl -w #What's wrong here? #Assume some piece of code has filled @array with many #(but an unknown number) of integers. We want to #compute the average of all positive numbers, and stop #when a number equals -999. Assume there is at least one #positive integer before -999. # for $num (@array){ if ($num < 0){ if ($num == -999){ last; } else { redo; } } else { $sum+=$num; $count++; } } $avg = $sum/$count; print "The average of positive integers before -999 is " .$avg."\n"; #--------------------------------------------------------------------------------- #!/usr/bin/env perl -w use strict; my %numbers; my (@small, @big); my $num; foreach (1..10){ $numbers{$_} = $_ * 10; } @small = keys %numbers; @big = values %numbers; for $num (@small){ print "$num "; } print "\n"; for (my $i = 0; $i< @big; $i++){ print "$big[$i] "; } print "\n"; #----------------------------------------------------------------- #!/usr/bin/env perl -w $i = 0; do { $i++ } while ($i>0 && $i<5); print "\$i = $i\n"; $j = 0; $j++ while ($j>0 && $j<5); print "\$j = $j\n"; #---------------------------------------------------------- #!/usr/bin/env perl -w #what's wrong with this code? #This program is supposed to print the position of each space character #within the string. It doesn't work. Why not? $string = "This is a string seperated by spaces\n"; $i = 0; while ($i != -1){ $i = index $string, " ", $i; print "A space was found at position $i\n"; unless $i == -1; } #-----------------------------------------------------------------