| Previous | Table of Contents | Next |
You could also have tested for evenness with the int() function. int() lops off everything after the decimal point and returns the whole (integral) part.
int(3.14159)
is 3
int(5.00000)
is 5
int(-2.71828)
is -2.
Because odd numbers yield a fractional part when divided by 2 (e.g., 7 / 2 is 3.5) and even numbers dont, you can determine evenness with the int() function: if int($num / 2) is equal to $num / 2, you know that $num must be even. Listing 1-12 is a short program that demonstrates this property.
Listing 1-12 oddeven: Using the int() function
#!/usr/bin/perl -w
print Enter a number: ;
chomp($num = <>); # equivalent to $num = <>; chop $num;
if (int($num/2) != ($num/2)) { # If $num is odd,
print $num is odd.\n; # say so!
}
else { print $num is even.\n; }
Lets try oddeven with a few different inputs.
% oddeven
RESULT: Enter a number: 5
5 is odd.
% oddeven
RESULT: Enter a number: 248
248 is even.
% oddeven
RESULT: Enter a number: 4.8
4.8 is odd.
because 2 != 2.4. (oddeven classifies all fractional numbers as odd.)
You can round a number to the nearest integer using int().
$nearest_integer = int($number + 0.5)
works for positive numbers, and
$nearest_integer = ($number > 0) ? int($number + 0.5) : int($number - 0.5)
works for both positive and negative numbers.
$money = int(100 * $money) / 100
truncates $money after the hundredths place, so 16.6666 becomes 16.66.
Check the printf() and sprintf() functions (Chapter 6, Session 4) for another way to round numbers to a particular number of digits. The POSIX module (Chapter 8, Session 8) includes floor() and ceil() functions.
Perl actually stores all numbers as floating point. If youre looking to speed up numeric processing, read about the integer pragma in Chapter 8, Session 1.
100 81 64 49 36 25 16 9 4 1
while ($num > 0) { print $num * $num, ; }
while ($num != 0) { print $num * $num ; $num--; }
while ($num >= 1) { print $num * $num, ; $num--; }
while ($num > 0) { $num--; print $num * $num, ; }
#!/usr/bin/perl
$i = 1;
while ($i < 4) {
print Please enter integer number $i: ;
chomp($num = <>);
if (($num % 2) = 1) { print $num is odd.\n; }
else { print $num is even.\n; }
$i++;
}
% Type a two-digit number: 26
RESULT 26 has:
2 in the tens place, and
6 in the ones place.
print $num/10, in the tens place, and \n, $num%10,
in the ones place.;
print $num%10, in the tens place, and \n, $num/10,
in the ones place.;
print int($num/10), in the tens place, and \n, $num%10,
in the ones place.;
print $num/10 in the tens place, and \n $num%10 in the ones
place.;
Difficulty: Medium
Modify tickets3 so that if the user types something besides yes or no, the program asks the user (politely!) again. A while loop will prove handy.
Humphrey: Im completely lost. Ive never programmed before and Ive used UNIX only a little bit. All I want to do is write some programs for my Web page. But thats not covered until Chapter 12.
Ingrid: I know how you feel. I felt overwhelmed when I learned C two years ago. Writing the first few programs is always the hardest part.
Humphrey: You know C? Then learning Perl should be easy for you! Theyre pretty similar, right?
Ingrid: In some ways, yes. So far, what weve learned is similar to C, except for these single and double quotes. And chop() and chomp(). And that strange way to grab user input with angle brackets. And of course, Perl is interpreted, whereas C is compiled. Come to think of it, Perl and C arent that similar after all.
Humphrey: Ive heard Perl described, on separate occasions, as a systems administration language, a text-processing language, and a Web programming language. Can Perl do everything C can?
Ingrid: Yes. You could call C a lower-level language because you have to muddle with memory addresses and explicitly allocate and deallocate memory, but nearly everything you can do in one, you can do in the other. Some programs will run faster in C, but all programs can be written faster in Perl.
Humphrey: Ive got a question about the tickets programs. Right now they only know about one movie. What if we wanted to offer a whole slew of movies? Wouldnt you need a separate if statement for each one? That would make the program huge!
Ingrid: Thats one way to do it. But you could also use a single array to contain an entire collection of movies. Well learn about arrays next.
| Previous | Table of Contents | Next |