next up previous contents
Next: Using the -n and Up: Example uses of command-line Previous: Example uses of command-line

Using the -0 Option

The -0 option will let you change the record separator. This is useful if your records are separated by something other than a newline. Let's use the example of input records separated by a dash instead of a newline. First, you need to find out the octal value of the dash character. The easy way to do this is to covert from the decimal value, which will be displayed if you run the following command line.

perl -e "print ord('-');"

This program will display 45. Converting 4510 into octal results in 558.

Next, you'll need an input file to practice with the following data held in a test file:

Veterinarian-Orthopedist-Dentist-

A program that reads the above data file using the diamond operators is now developed:

The Perl code is:

#!perl -0055

open(FILE, "<test.dat");

@lines = <FILE>;

close(FILE);

foreach (@lines) {

    print("$_\n");
}

Hint Instead of using the command-line option, you could also say $/ = "-";. Using the command line is a better option if the line ending changes from input file to input file.

This program will display:

Veterinarian-
Orthopedist-
Dentist-

Notice that the end-of-line indicator is left as part of the record. This behavior also happens when the newline is used as the end-of-line indicator. You can use chop() or chomp() to remove the dash, if needed.


next up previous contents
Next: Using the -n and Up: Example uses of command-line Previous: Example uses of command-line
dave@cs.cf.ac.uk