print a number between 1 and 10:”; “Enter $selection = The second instruction contains <STDIN> which stands for standard input which is synonymous with the keyboard. The program actually stops at this instruction and waits for the user (i.e. you) to type something in and hit enter. The main thing you have to remember here is that, all you type goes into the variable INCLUDING the enter key (i.e. \n). Every variable read this way will have the \n on the end. This is quite often a pain and generally one wants to remove it. It can easily be done using the chomp command. The following examples should demonstrate its usage. print your name:”; “Enter $name = chomp($name); # removes the \n from the end. your age:”; “Enter $age = <STDIN>; chomp($age); # you need to remove the \n from both numbers and strings. Homework Problems 5.1 Give the output of the following program EXACTLY. $a=23; print answer is $a\n”; “The answer is $a\n’;#note the ‘The print have called this principle,\n by which each slight variation,\n”; “I print useful, is preserved, by the term of Natural Selection.\n”; #by Charles Darwin “if 5.2 Give the output of the following program exactly. Note the variable is not chomped. What would happen if it were? print a number :”; “Enter $num = <STDIN>; another number :”; “Enter $val = <STDIN>; chomp($val); print second number was $val and the first number was $num. Interesting huh!”; “The Mathematical operators Perl has a large number of built-in operators for performing operations on numbers. Perl considers all numbers as real ( i.e. floating point numbers). As mentioned previously a variable can be initialized by these values. Here $PI and $cube10 are initialized. $PI=3.1415926535;# a real number with decimal $cube10 = 1000; # an integer
print “Enter a number between 1 and 10:”; $selection = The second instruction contains <STDIN> which stands for standard input which is synonymous with the keyboard. The program actually stops at this instruction and waits for the user (i.e. you) to type something in and hit enter. The main thing you have to remember here is that, all you type goes into the variable INCLUDING the enter key (i.e. \n). Every variable read this way will have the \n on the end. This is quite often a pain and generally one wants to remove it. It can easily be done using the chomp command. The following examples should demonstrate its usage. print “Enter your name:”; $name = chomp($name); # removes the \n from the end. “Enter your age:”; $age = <STDIN>; chomp($age); # you need to remove the \n from both numbers and strings. Homework Problems 5.1 Give the output of the following program EXACTLY. $a=23; print “The answer is $a\n”; ‘The answer is $a\n’;#note the print “I have called this principle,\n by which each slight variation,\n”; print “if useful, is preserved, by the term of Natural Selection.\n”; #by Charles Darwin 5.2 Give the output of the following program exactly. Note the variable is not chomped. What would happen if it were? print “Enter a number :”; $num = <STDIN>; “Enter another number :”; $val = <STDIN>; chomp($val); print “The second number was $val and the first number was $num. Interesting huh!”; Mathematical operators Perl has a large number of built-in operators for performing operations on numbers. Perl considers all numbers as real ( i.e. floating point numbers). As mentioned previously a variable can be initialized by these values. Here $PI and $cube10 are initialized. $PI=3.1415926535;# a real number with decimal $cube10 = 1000; # an integer
Perl-for-science-Majors61.docx