Previous Table of Contents Next


The int() Function

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 don’t, 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”; }

Let’s 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.)

Rounding Numbers

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.

Integer Arithmetic

Perl actually stores all numbers as floating point. If you’re looking to speed up numeric processing, read about the integer pragma in Chapter 8, Session 1.

Quiz 4

1.  int() is a(n) _______, eq is an _______, and while ($i < 10) {$i ++} is a _______.
a.  operator, expression, loop
b.  function, operator, statement
c.  statement, operator, function
d.  function, expression, loop
2.  Assume $num is 10. Which statement prints the following?
100 81 64 49 36 25 16 9 4 1
a.  
while ($num > 0) { print $num * $num, ‘ ’; }
b.  
while ($num != 0) { print “$num * $num ”; $num--; }
c.  
while ($num >= 1) { print $num * $num, ‘ ’; $num--; }
d.  
while ($num > 0) { $num--; print $num * $num, ‘ ’; }
3.  Where’s the error in this program?
#!/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++;
}
a.  The condition of the while loop
b.  The chomp() statement
c.  One of the print() statements
d.  The condition of the if statement
4.  Suppose you want a program that behaves in the following way.
        % Type a two-digit number: 26
RESULT 26 has:
        2 in the tens place, and
        6 in the ones place.

Which line should be in the program?
a.  
print $num/10, “ in the tens place, and \n”, $num%10,
                 ‘ in the ones place.’;
b.  
print $num%10, ’“in the tens place, and \n”, $num/10,
                 ‘ in the ones place.’;
c.  
print int($num/10), “in the tens place, and \n”, $num%10,
                 ‘ in the ones place.’;
d.  
print “$num/10 in the tens place, and \n $num%10 in the ones
                 place.”;

Exercise 4

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.

Review

Humphrey: I’m completely lost. I’ve never programmed before and I’ve used UNIX only a little bit. All I want to do is write some programs for my Web page. But that’s 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! They’re pretty similar, right?

Ingrid: In some ways, yes. So far, what we’ve 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 aren’t that similar after all.

Humphrey: I’ve 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: I’ve 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? Wouldn’t you need a separate if statement for each one? That would make the program huge!

Ingrid: That’s one way to do it. But you could also use a single array to contain an entire collection of movies. We’ll learn about arrays next.


Previous Table of Contents Next