LECTURE 17: February 11, 2008 Quiz: WEDNESDAY (Up to lecture 11) HW1 graded HW2 solutions coming soon Today: Q&A The Quiz will be for the whole class period. #s refer to Sample Quiz questions: #6 The computer recognizes $Str= Ò Ò; as something false, as in : if ($Str) {... what is in the round brackets would be false. #9 Let's program it with a while loop this time: Prime with ÒwhileÓ $Num=<> chomp $Num; $IsPrime=1; $Count=2; while ($Count<$Num) { if (($Num%$Count)==0) { $IsPrime=0; } } $Count++; if... for (A ;B; C) { A while (B) {... C } We also could do for #9: $Num=<>; chomp $Num; $HasADivisor=0; for($Count=2;$Count<$Num; $Count++) { if (($Num % $Count)==0) { $HasADivisor=1; } } Might see a question on the quiz regarding nested loops. Array problems: If we do: %H=(1,3,10,12,8,10); print %H; -> 131012810 (can't tell one thing from the other) @A=%H; print @A; ->131012810 (still same thing) print Ò@AÓ; 1 3 10 12 8 10 (now it is readable) print Ò%HÓ; -> %H Union of two arrays: * @A1, @A2 are arrays * Produce @A3 with elements that are in @A1 and @A2 *Not just adding the two arrays together as (@A1, @A2); But need to actually check for duplicates. @A1=(ted, ann,bill); @A2=(ann, george, frank, dean) ->@A3= (ted, ann, bill, george, frank, dean); Start with @A3=@A1 Loop through elements of @A2 -check if already in @A1 -If not, add to @A3 Perl for Union: @A3=@A1; Foreach $Val (@A2) { $Count=0; Foreach $Val2 (@A1) { if ($Val eq $Val2) { $Count++; } } if ($Count==0){ push @A3, $Val } }