Previous Table of Contents Next


Exercise 8

Difficulty: Medium

Consider the array (2, 7, 4). Without explicitly accessing individual array elements with brackets or splice() or shift() or pop(), find one simple Perl expression that extracts the 2, one that extracts the 4, and one that yields 3, the number of elements in the array.

Review

Humphrey: I understand loops a lot better now. But I’m not sure what to make of these contexts and default variables.

Ingrid: Me neither. I’ve never seen anything like them in any programming language.

Humphrey: They seem dangerous. Programming languages shouldn’t be like real languages. They should enforce a uniform style and syntax so there’s only one way to solve a problem. That way, it’s easy to understand programs other people write.

Ingrid: You won’t like Perl, then, since it gives you several ways to do just about everything.

Chapter Exercise

Easy

Modify tickets5 so it asks users to name movies. If a user types the name of a movie that isn’t in @movies, your new program should append it.

Hint: Use foreach, eq, and push().

Medium

Modify tickets3 so it prompts the user to name several movies and then prints them in reverse order.

Hint: Use a for loop. (Don’t use the reverse() function from Chapter 4, Session 6. That’s cheating.)

Moderate

Modify tickets5 so it accepts a rating from the command line and then prints all the movies that have a lower rating.

Difficult

Write a program that does the following:

1.  Prompts the user for an integer width.
2.  Prompts the user for an integer height.
3.  Prints a rectangle of stars with the given width and height.

Make sure that any input is OK; your program should truncate any decimals and reprompt the user if the input is too small or too large.

Hint: Use the x operator and two for loops, one nested inside the other.


Previous Table of Contents Next