untitled

(ff) #1
10.1 Text Transformations 217

Summary


  • Programs are commonly organized into three parts: introduction, body,
    and conclusion.

  • While programs run, they store data in variables. As the program runs
    the data stored in each variable will change. The simplest kind of variable
    in Perl is a scalar, which holds a single string or number.

  • The simplest way to transform data is to transform one line at a time. In
    such a program:

    1. The introduction prints the title and sets variables to initial values.

    2. The body reads each line, extracts data from it and prints the data in
      the required format.

    3. The conclusion computes summary information and prints it.



  • Perl has many ways to abbreviate commonly used operations.

  • Perl has two kinds of variables for holding collections of data items:

    1. An array holds a sequence of data items. Arrays are also called lists.

    2. A hash maps keys to associated information.




•Theforeachstatement is used for performing operations on each data
item in a collection.

10.1.2 Multidimensional Arrays


The transformation technique we have used so far has been to transform one
line of a file at a time. This works well for files that have one record on each
line and each record has nearly the same structure as any other record. When
the input file is not so well arranged, it may be better to process the file in
some other order. In Perl it is very easy to read an entire file all at once as in
program 10.5.
Thewhilestatement of the programs in subsection 10.1.1 has been re-
placed by an assignment of the input operator<>. When the first statement
is finished, thelinesarray will have all of the lines of the input file.
Thescalarfunction converts a variable of any kind to a scalar. This is a
general-purpose function whose meaning depends on the particular variable
being converted. In the case of arrays,scalargives the size of the array. As
Free download pdf