Section for Week 1, Thursday, 1/14/99 STRINGS ------- Thus far we've seen the standard "hello\n" string appear often. What can we do natively w/ strings in perl? Quite a lot: 1) Within a double-quote string, we have the following constructs: \n Newline \r Return \t Tab \f form-feed \b backspace \a bell \e escape \007 Octal (begin w/ zero, give three digits) \x7f Hex (begin w/ x) \c? Control-? (so \cX == ^X) \\ Backslash \" Double quote \l lowercase next letter \L Lowercase to \E \u uppercase next letter \U uppercase to \E \Q \-quote every non-alphanumeric to \E \E Ends \L \U \Q The case constructs come in useful when variable interpolating: "\L$string\E". Operators: Concatenation: "hello " . "world" == "hello world" Comparison: eq, ne, lt, gt, le, ge Repetition: "Ami" x 3 == "AmiAmiAmi" (3+2) x 4 == 5 x 4 == "5" x 4 == "5555" (auto string context) Functions: chop() --> take the last character off a string (must be a variable!) $x="Hello World"; chop($x); # now $x == "Hello Worl"; chomp() --> only remove a newline from the end of a string $a = ; chomp($a); Equiv: chomp($a = ); Exercise: Write a program that reads a number and a string and prints out the string the number of times indicated by the number on separate lines. Answer: In C, probably iterate (for loop) printing each string every iteration. Compare Perl solution: print "What is the string? "; $s=; print "What is the number? "; $number=; print $s x $number;