tin tức

Bài tập Perl

22/02/2012 - Xem: 2549

Bài tập thực hành – Dùng trong môn perl programing Aiti-aptech

Perl Exercises

  1. Create and run a Perl program that prints “Hello, World” on the monitor.
  2. Write a program that prints the decimal number 32. However, in the print command, specify the value of 32 using hexadecimal notation.
  3. Create program that uses the tab character in three literals to align text.
  4. Write a program that prints using metaded new lines in a single-quoted literal.
  5. Convert the number 56,500,000 into scientific notation.
  6. Write a program that prints an array that uses the range operator. The left value should be AA and the right value should be BB. What happens and why?
  7. Write a program that prints its own source code
  8. Create a array called @months. It should have 12 elements in it with the names of the month represented as strings.
  9. Create a string that interpolates that value of the variable $numberOfBooks.
  10. Using the range operator (..) create an array with the following elements: 1, 5, 6, 7, 10, 11, 12.
  11. Using the array created in the last exercise, create a print command to display the last element.
  12. Create an associative array that holds a list of five music artists and a rating for them. Use “good,” “bad,” and “indifferent” as the ratings.
  13. Using the array created in the last exercise, create a print command to display the last element.
  14. Assign a value to $firstVar using both division and subtraction.
  15. Using the post-decrement operator, subtract one from $firstVar.
  16. Write a program that assigns values to $firstVar and $secondVar and uses the >= operator to test their relationship to each other. Print the resulting value.
  17. Use the **= assignment operator to assign a value to $firstVar.
  18. Use the ternary operator to decide between to different value.
  19. Write a program that assigns values to $firstVar and $secondVar and uses the <=> operator to test their relationship to each other. Print the resulting value.
  20. Use the concatenation operator to join the following values together: “A” x 4 and “B” x 3.
  21. Use the exponentiation operator to find the value of 2 to the 5th power.
  22. Write an assignment statement that uses the && and || and ! operators.
  23. Write a program that prints the value of the fifth bit from the right in a scalar variable.
  24. Write a program that uses a bitwise assignment to set the fifth bit from the right in a scalar variable.
  25. Write a program that shows the difference in operator precedence between the % operator and the && operator.
  26. Create a function that prints its own parameter list.
  27. Create a program that uses three functions to demonstrate function call nesting.
  28. Use the chop() function in a program. Print both the returned character and the string that was passed as a parameter.
  29. Run the following program to see how many levels of recursion your system configuration supports.
    firstSub();
    
    sub firstSub{
     print("$count\n");
     $count++;
     firstSub();
    }

  30. Write a function that uses the substr() and uc() functions to change the tenth through twenthieth characters to uppercase.
  31. Write a function that uses the keys() function to print out the values of an associative array.
  32. Create a program that uses a private function to subtract two numbers and multiply the result by four.
  33. Write a program that shows what the shift() and unshift() functions do.
  34. Write a program that shows what the push() and pop() functions do.
  35. Write a simple expression that uses the exponentiation operator.
  36. Write a complex expression that uses three operators and one function.
  37. Write a Perl program that uses a statement block inside a function call.
  38. Use the statement block from the previous exercise to create local variables.
  39. Write a Perl program that shows if the expression clause of a while modified statement will be evaluated when the condition is false.
  40. Use the while loop in a program to count from 1 to 100 in steps of 5.
  41. Use the for loop in a program to print each number from 55 to 1.
  42. Use an until loop, the next statement and the modulus operator to loop from 0 to 100 and print out “AAA” every 16th iteration.
  43. Use the foreach loop to determine the smallest element in an array.
  44. Use a for loop to iterate over an array and multiply each element by 3.
  45. Use a do..until loop and the each() function to iterate over an associative array looking for an value equal to “AAA”. When the element is found, the loop should be ended.
  46. Write a program that will print the dereferenced value of $ref in the following line of code.
    $ref = \\\45;

  47. Write a function that removes the first element from each array passed to it. The return value of the function should be the number of elements removed from all arrays.
  48. Add error checking to the function written in exercise 4 so that the undef value is returned if one of the parameters is not an array.
  49. Write a program based on Listing 8.7 that adds a data member indicating which weekdays a salesman may call the customer with an id of MRD-300. Use the following as an example:
    "Best days to call" => ["Monday", "Thursday" ]

  50. Write a program to open a file and display each line along with its line number.
  51. Write a program that prints to four files at once.
  52. Write a program that gets the file statistics for /usr/bin/perl and displays its size in bytes.
  53. Write a program that uses the sysread() function. The program should first test the file for existence and determine the file size. Then the file size should be passed to the sysread() function as one of its parameters.
  54. Write a program that reads from the file handle in the following line of code. Read all of the input into an array and then sort and print the array.
     open(FILE, "dir *.pl |");

  55. Using the binary mode, write a program that reads the /usr/bin/perl and print any characters that are greater than or equal to “A” and less than or equal to “Z”.
  56. Write a program that reads a file with two fields. The first field is a customer ID and the second field is the customer name. Use the ! character as a separator between the fields. Store the information into a hash with the customer id as the key and the customer name as the value.
  57. Write a program that reads a file into a array, then displays 20 lines at time.
  58. Write a pattern that matches either “top” or “topgun”.
  59. Write a program that accepts input from STDIN and changes all instances of the letter a into the letter b.
  60. Write a pattern that stores the first character to follow a tab into pattern memory.
  61. Write a pattern that matches the letter g between 3 and 7 times.
  62. Write a program that finds repeated words in an input file and prints the repeated word and the line number on which it was found.
  63. Create a character class for octal numbers.
  64. Write a program that uses the translation operator to remove repeated instances of the tab character and then replaces the tab character with a space character.
  65. Write a pattern that matches either “top” or “topgun” using a zero-width positive look-ahead assertion.

(i-php.net)

Liên kết

Tin được quan tâm nhất

Quản cáo