PageResource.com - Web Development Tutorials

Reading Files 2

How to use the data read from the file
In the last section, we saw how to create and read data from a file. Here, we are going to make use of the data which we read into the array.

Remember, our data file is named wrestledata.cgi and looks like this:

The Rock|Cheer|Rock Bottom
Triple H|Boo|Pedigree
Stone Cold|Cheer|Stone Cold Stunner

So far, we have done this in order to open the file, read the data into an array, and close the file:

$data_file="wrestledata.cgi";
open(DAT, $data_file) || die("Could not open file!");
@raw_data=<DAT>;
close(DAT);

Now we will get the data out of the array with a loop and the split method.

Making Use of the Data

To make use of the data, we need a purpose. So, let's say we want to print out a simple sentence for each wrestler in the list. We want to say the name, how the crowd might react, and the favorite move. Something like:

When (wrestler name) is in the ring, the crowd might (reaction) when the (move) is used.

To do this for each wrestler, we can use a loop to cycle through the content of the @raw_data array, grab the variables we want, and use them. This is commonly done with a foreach loop:

foreach $LINE_VAR (@ARRAY)
{
 commands...
}

So, the $LINE_VAR is a variable to represent each line in the array. The @ARRAY will be the name of the array to loop through. For our example, we could use:

foreach $wrestler (@raw_data)
{
 commands...
}

Now we need to do something inside the loop to split each line into variables we can use. Before we invoke the split though, we will want to chomp the \n character off the end of each line:

foreach $wrestler (@raw_data)
{
 chomp($wrestler);
}

Now we are ready to use the split method to create the variables we need each time through the loop. Since we used the pipe symbol as the separator, that is the character we will use to split the data. Notice that the pipe symbol needs to be escaped with a \ character since it is a special character in Perl:

foreach $wrestler (@raw_data)
{
 chomp($wrestler);
 ($w_name,$crowd_re,$fav_move)=split(/\|/,$wrestler);
}

Now we can print the sentence using the variables we created, and it will print the sentence for every wrestler.

foreach $wrestler (@raw_data)
{
 chomp($wrestler);
 ($w_name,$crowd_re,$fav_move)=split(/\|/,$wrestler);
 print "When $w_name is in the ring, the crowd might $crowd_re when the $fav_move is used.\n";
}

That little bit will get us:

When The Rock is in the ring, the crowd might Cheer when the Rock Bottom is used.
When Triple H is in the ring, the crowd might Boo when the Pedigree is used.
When Stone Cold is in the ring, the crowd might Cheer when the Stone Cold Stunner is used.

And there you have it. Of course, you probably want HTML output instead of output for the console. Also, you might want to see the entire script in one piece. So, here is a full script which should give you the same type of output, except it will be an HTML page:

#!/usr/bin/perl

$data_file="wrestledata.cgi";

open(DAT, $data_file) || die("Could not open file!");
@raw_data=<DAT>;
close(DAT);

print "Content-type: text/html\n\n";
print "<html><body>";

foreach $wrestler (@raw_data)
{
 chomp($wrestler);
 ($w_name,$crowd_re,$fav_move)=split(/\|/,$wrestler);
 print "When $w_name is in the ring, the crowd might $crowd_re when the $fav_move is used.";
 print "<br>\n";
}

print "</body></html>";

You can of course include more elaborate HTML markup, I'm a bit lazy about making it look good here. It is just a page with the text, but it could be made into a nicer presentation.

Well, that's all for now, let's go on to: Appending and Writing to Files.


Copyright © The Web Design Resource. All rights reserved. | Contact Us | Privacy Policy