well split!! (Solved)

[email protected] Wed, 7 Mar 2001 18:27:03 -0500
Newsgroups perl.macperl.webcgi
Message-ID <p05010401b6cc68263f90@[64.194.142.225]>
To all -- many thanks:

With all your help, and some experimenting on my part, I finally 
discovered the solution to my problem. The following is my 
understanding of what's happening -- if I am wrong, please tell me.

First, the data file I wanted to display contained numerous records. 
Each record had seven fields separated by <tabs> and ending with a 
<return>.

Second, when the program opened the data file, I wanted it to 
"load-in" the fields from each record into variables and then fill a 
table using those variables.

Third, I then wanted the routine to continue from record to record 
until it had finished with all records while concurrently making a 
table listing all the data found in the data file.

Now, what most everyone told me was to fill the variables via the 
"my(...)=split(/\t)" statement. Okay, that worked fine for the first 
record, but it promptly quit after reading the first record. So, I 
knew I needed something to loop the "reading data" process. I 
originally used a "for (split)" statement to do this, but it wasn't 
correct -- however, I wasn't too far from being right, as it turns 
out.

What the routine needed was a way to grab a record and then grab the 
fields within the record.

So, in the following code, it first grabs a record by looking for a 
return, namely via the "for (split(/\r/))" statement. This dumps the 
contents of the first record into $_ (implied). Then to grab the 
fields, I used what everyone recommended, which was the 
"my(...)=split(/\t)" statement. This actually just further evaluates 
the $_ (implied) again, but this time with respect to the field data 
contained within the record. (incidentally, the "my" portion of the 
"my(...)=split(/\t)" statement isn't needed).

With all that said, the following code works for me -- can anyone see 
anything wrong with it?

#--- start of code ---

#!/usr/bin/perl

	print "Content-type: text/html\n\n";
	print "<HTML><HEAD><TITLE>$title</TITLE>";

	print <<DB; # create table with title
	<table border="1" cellpadding="5" cellspacing="2" width="600">
	<TR>
	<TH>ID</TH>
	<TH>Category</TH>
	<TH>Variable Name</TH>
	<TH>Price</TH>
	<TH>Name</TH>
	<TH>Description</TH>
	<TH>Item Number</TH>
	</TR>
DB

$datafile = "Database/outlet.data";
open DATA, "$datafile" or die "Can't open $datafile: $!";
	while (<DATA>)
   		{
		next if /^$/;  # skip empty lines
		chomp;         # remove the "line ending"
		for (split(/\r/)) # find returns
			{
			# find tabs
			my ($database_id, $category, $variable_name, 
$price, $name, $description, $item_number) = split(/\t/);

			print <<ROW; # place variables into table
				<TR>
				<TD>$database_id</TD>
				<TD>$category</TD>
				<TD>$variable_name</TD>
				<TD>\$$price</TD>
				<TD>$name</TD>
				<TD>$description</TD>
				<TD>$item_number</TD>
				</TR>
ROW
			}
		}

close DATA;

print "	</table>";

__END__

#---- end of code ----

tedd
-- 
http://sperling.com/