#!/usr/bin/env perl -w $foo = "foo bar\nBaz too\n"; @arr = $foo =~ /(\w+) (\w+)\n.+(\w{3})/; print "@arr\n"; #Does this expression succeed? If so, what is contained in @arr? #------------------------------------------------------------- #!/usr/bin/env perl -w #In each case, predict what the output will be. #Then uncomment each line that's commented out #and comment the previous line. What will the #output be now? #Example 1 $string = "Perl is superlative to other languages"; @matches= $string =~ /Perl/g; #@matches = $string =~ /Perl/ig; print "@matches\n"; #Example 2 $phrase = "Do not go gently into that dark night\nRage, Rage\nAgainst the dying of the light"; @firsts = $phrase =~ /^(\w+)/g; #@firsts = $phrase =~ /^(\w+)/gm; print "Firsts: @firsts\n"; @lasts = $phrase =~ /(\w+)$/g; #@lasts = $phrase =~ /(\w+)$/gm; print "Lasts: @lasts\n"; #Example 3 $longline = "123\nHello\n432\nGoodbye\n"; @nums = $longline =~ /(\d+)./g; #@nums = $longline =~ /(\d+)./sg; print "@nums\n"; #Example 4 $string = "ab de fd ac rd ad"; $var = "aa"; for (1..10){ print "$var: "; print "$1 found in $string" if $string =~ /($var)/; # print "$1 found in $string" if $string =~ /($var)/o; print "\n"; $var++; } #------------------------------------------------------------------ #!/usr/bin/env perl -w @foods = ("Apples", "Bananas", "Brocholi", "Beets", "Berries", "Coconuts"); foreach (@foods){ $first = $1 if ?(B.*)?; $last = $1 if /(B.*)/; } print "First B word: $first\nLast B word: $last\n"; #------------------------------------------------------------------------------- #!/usr/bin/env perl -w $sentence = "oOPS! sOMEONE OBVIOULSLY LEFT HIS cAPS lOCK KEY ON! oH nO!" #Which, if any, of these expressions would fix this? #A) # $sentence =~ s/[a-z]/[A-Z]/; #B) # $sentence =~ tr/a-zA-Z/A-Za-z/; #C) # $sentence =~ s/[a-z]/[A-Z]/g; #D) # $sentence =~ s/[a-zA-Z]/[A-Za-z]/g; #E) # $sentence =~ tr/a-z/A-Z/; #F) # $sentence =~ tr/A-Z/a-z/; #G) # $sentence =~ tr/A-Z/a-z/; # $sentence =~ tr/a-z/A-Z/; # (ie, both statements, one after another)