previous main page next

Dokumenthantering VT97:03

These are the exercises and references for the third class of the course Dokumenthanteringen


Perl programming examples

A.1

#!/usr/local/bin/perl
printf "Hello world!\n";

perl -e 'printf "Hello world!\n";'

A.3

@list1 = split(//,$lineOfText); # split $lineOfText in characters and 
                                # put result in @list1
@list2 = split(/\s/);           # split $_ in words and 
                                # put result in @list2
split(/^/,$lineOfText);         # split $lineOfText in lines and
                                # put result in @_

A.4

if ($test<0) {
   printf "test is negative\n"; 
} elsif ($test == 0) {
   printf "test is zero\n"; 
} else  { printf "test is positive\n"; } 

for (1..10) {
   printf $_;
}

for ($i=1; $i<=10; $i++) {
   printf $i;
}

for (@list) {
   printf $_;
}

foreach (keys %list) {
   printf $_;        # print key
   printf $list{$_}; # print value
}

while ($number < 10) {
   $number = $number+1;
}

A.5

while (/tets/) {                    # search for tets in $_
   s/tets/test/;                    # replace tets in $_ by test
   $errorsFound = $errorsFound+1;   # increase counter
}

A.6

while (<STDIN>) {                  # read a line from standard input
   printf STDOUT $_;               # print the line on standard output
   $linesRead = $linesRead+1;      # increase line counter
}
if ($linesRead == 0) {
   printf STDERR "empty file!\n";  # print a warning message
   die;                            # stop the program
}

printf "%5.2f %-20s %d\n",33.97,"William Boney",17;

A.7

#!/usr/local/bin/perl
$i = 0;                 # initialize argument counter to one
$arg = shift(@ARGV);    # put first argument in $arg
while ($arg) {          # repeat until $arg is empty
   $i++;                # new argument: increase counter   
   printf "Argument %d is %s\n",$i,$arg;
   $arg = shift(@ARGV); # put next argument in $arg
}

A.8

#!/usr/local/bin/perl
sub max {                      # start of definition of subroutine max
   $currentMax = @_[0];        # initialize current maximum
   for (@_) {                  # repeat for all arguments
      if ($_ > $currentMax) {  # if the current argument is larger
                               # than the current maximum then
         $currentMax = $_;     # current maximum = current argument
      }
   }
   $currentMax;                # return current maximum
}

A.9

#!/usr/local/bin/perl -w
use Tk;                              # include Tk toolkit
use strict;                          # include strict
my $mainWindow = new MainWindow;     # make a window mainWindow
$mainWindow->title('Test window');   # define the title of the window
$mainWindow->Label(-text=>' Dokumenthantering VT97 ')->pack;
                                     # define a text in the window
MainLoop;                            # start the main loop


Exercises

The results of the exercises marked with * have to be handed in.

  1. In the directory /usr/users/staff/erikt/html/dh97 you will find a Frame file (000103sv.01), the corresponding MIF file (.mif) and the corresponding plain text file (.txt). Compare the Frame file (in FrameMaker: start with frame4 or frame5) with the plain text file. Do you see something strange? The plain text file was created by saving the Frame document as a plain text file in FrameMaker.

    Now examine the MIF file. Find the codes for the lower case variants of the three Swedish vowels. Make a rough estimation of how many percent of the file is format markup (style definition?) and how many percent text and content type markup by finding out after how many percent the actual text starts (use the more command).

    Examine the binary Frame file by loading it in emacs. You will see that the document contains a lot of zeroes and some recognizable text which sometimes is markup code. In UNIX there is a general program for extracting printable strings from a binary file: strings. Apply the program to the binary file. It will show all sequences of four or more printable characters. Do you think that is usable as a Frame to plain text converter?

  2. In the directory /usr/users/staff/erikt/html/dh97 you will find a Word file (.doc), the corresponding plain text file (.txt) and the corresponding RTF file (.rtf). Examine the Word file by loading it in emacs. Compare it with the text file. Notice that the Word file contains old versions of the text which do not appear in the text file. Examine the RTF file as well. You will see that the file contains only one version and little format information. Notice the size difference between the three documents.

  3. * The call perl -e '{ while (<>) { if (/X/) { printf; }}}' prints all lines from STDIN which include an X. Replace the X by different regular expressions which print lines containing: (1) the string bit, (2) the strings bit or bet, (3) exactly two strings separated by white space and (4) at least two copies of at least one character.

  4. * Write a Perl program that performs the same task as the UNIX wc program. Implement at least one wc option. The program only has to be able to process one file.

  5. * Expand the Perl/Tk script with a button that kills the window when pressed.


References


Last update: March 02, 1997. erikt@stp.ling.uu.se