O'Reilly logo

Learning Perl, Second Edition by Randal L. Schwartz, Tom Christiansen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

3.8. Exercises

See Appendix A for answers.

  1. Write a program that reads a list of strings on separate lines and prints out the list in reverse order. If you're reading the list from the terminal, you'll probably need to delimit the end of the list by pressing your end-of-file character, probably CTRL-D under UNIX or Plan 9; often CTRL-Z elsewhere.

  2. Write a program that reads a number and then a list of strings (all on separate lines), and then prints one of the lines from the list as selected by the number.

  3. Write a program that reads a list of strings and then selects and prints a random string from the list. To select a random element of @somearray, put

    srand;

    at the beginning of your program (this initializes the random-number generator), and then use

    rand(@somearray)

    where you need a random value between zero and one less than the length of @somearray.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required