Reading File Contents into Perl Arrays

   



Writing to Files in Perl

You can open multiple files in Perl and also write to a file, either by appending to an existing file, creating a new file, or overwriting an existing file. Listing C.11 shows you how to read data from an existing file and then echo every line on the screen as well as writing each line to another file. Notice how the less-than symbol ('<') is for file input and the greater-than symbol ('>) is for file output.

Listing C.11 writeFile1.pl

start example
my($count)       = 0; my($currentLine) = ""; my($fileName)    = "somedata.txt"; my($outputFile)  = "somedata.out"; open(INPUT, "<$fileName") ||       die "Cannot open $fileName: $!\n"; open(OUTPUT, ">$outputFile") ||       die "Cannot open $outputFile: $!\n"; while($currentLine = <INPUT>) {    print OUTPUT "LINE $.:  $currentLine";    print "LINE $.:  $currentLine"; }
end example

Remarks

Listing C.11 shows you a command style in Perl: open all the necessary files at the beginning of the script and handle any possible errors. Note that the input file and the output file differ only by their suffix; you can probably guess what will happen if they specified the same file! Launch the Perl script writeFile1.pl in Listing C.11 from the command line as follows,

perl -w writeFile1.pl

and you will see the following output:

LINE 1:  # this is a comment line LINE 2:   LINE 3:  Jones:Tom:1000 LINE 4:  Smith:Ann:2000 LINE 5:  Smith:Bill:3000

Notice the newly created file called somedata.out which contains the same data that is displayed in the shell.

The ability to open input streams is not limited to text files. For example, if you wanted to list all the Perl scripts in the current directory, you could do something like Listing C.12.

Listing C.12 dir1.pl

start example
my($count)       = 0; my($currentLine) = ""; open(INPUT, "ls *pl|") || die "Cannot list files: $!\n"; while($currentLine = <INPUT>) {    $count++; } print "Found $count Perl scripts\n";
end example

In a Windows NT environment, replace ls *pl with dir/b *pl. In actuality, there are portable commands available for perusing the contents of a directory; this example is intended to illustrate how you can use input streams that are unrelated to text files.

Skipping Comment Lines in Text Files

Sometimes you need to selectively process lines in a text file. One approach involves reading the entire file into an array (which you will see later) and another approach involves processing the contents of a text file on a line-by-line basis. Listing C.13 displays the contents of a Perl script that uses the second approach.

Listing C.13 skipComment1.pl

start example
my($count)    = 0; my($result)      = ""; my($currentLine) = ""; my($fileName)    = "somedata.txt"; open(INPUT, "<$fileName") || die "Cannot open $fileName: $!\n"; while($currentLine = <INPUT>) {    if( ($currentLine =~ /^#/) || ($currentLine =~ /^$/) )    {       next;    }    $count++; } print "File $fileName contains $count lines\n";
end example

Remarks

Listing C.13 contains a loop that uses the continue statement in order to skip blank lines and lines that start with a ‘#' character The key construct in Listing 12 is the following line:

if( ($currentLine =~ /^#/) || ($currentLine =~ /^$/) )

The first part of the if statement checks if the current line starts with a ‘#' character and the second part of the if statement checks if the current line is blank. In both contexts, the '^' and '$' meta characters refer to the start-of-line position and end-of-line position, respectively. You may be familiar with the use of these meta characters in regular expressions or in vi commands. Incidentally, the while loop in Listing C.13 can be rewritten in the following Perlism:

while($_ = <INPUT>) {    if( ($_ =~ /^#/) || ($_ =~ /^$/) )    {       next;    }    $count++; }



   



Fundamentals of SVG Programming. Concepts to Source Code
Fundamentals of SVG Programming: Concepts to Source Code (Graphics Series)
ISBN: 1584502983
EAN: 2147483647
Year: 2003
Pages: 362

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net