Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading from text file & storing in array 1

Status
Not open for further replies.

JonnyRed77

Programmer
Nov 28, 2004
2
GB
Hi there. I'm trying to read info from a text file & store it in an array. Each bit of info is seperated by a ",". I tried using the split function to seperate them but will not work. When I go to print out the array, it just prints out my file location as a string rather than printing the file info. Writing to the file is fine, just can't read from it. Any help would be great.

John
-------------------------------------------------

Code:
#!c:/perl/bin/perl
use CGI ':standard';

if (!param()){
	create_form();
}

else {
	process_form();
}

sub create_form {
	print header, start_html( {-title=>'Beta Testers Registration Form', -bgcolor=>'#ffffff'} );
	print start_form( {-action=>'BetaTesters.cgi'} );
	
	print "
	<table width=\"280\" height=\"200\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">
		<tr>
			<td width=\"130\">Surname</td>
			<td><input type=\"text\" name=\"Surname\"></td>
		</tr>
		<tr>
			<td width=\"130\">Forename</td>
			<td><input type=\"text\" name=\"Forename\"></td>
		</tr>
		<tr>
			<td width=\"130\">Extension Number</td>
			<td><input type=\"text\" name=\"ExNumber\"></td>
		</tr>
		<tr>
			<td width=\"130\">E-mail Address</td>
			<td><input type=\"text\" name=\"Email\"></td>
		</tr>
		<tr>
			<td width=\"130\">Department</td>
			<td><select name=\"Department\">
						<option>--- Choose One ---
						<option>Finance
						<option>Sales
						<option>Operations
						<option>Personnel
						<option>Design
				  </select>
			</td>
		</tr>
		<tr>
			<td colspan=\"2\">&nbsp;</td>
		</tr>
		<tr>
			<td colspan=\"2\"><input type=\"submit\" value=\"Submit Details\">
									 <input type=\"reset\" value=\"Clear Form\"></td>
		</tr>
	</table>";
	
	print end_form;
	print end_html;
}

sub process_form {
	$surname = param('Surname');
	$forename = param('Forename');
	$exnumber = param('ExNumber');
	$email = param('Email');
	$department = param('Department');
	
	$FullName = "$surname" . "_" . "$forename";
	$TesterFile = "c:/progra~1/apache~1/apache/bin/list.txt";
	
	if ($email !~ m/@/) {
		print "You entered an invalid e-mail address.";
		}
	
	else {
		open(LIST, ">>$TesterFile") || die "Can't open file: $!";
		
		print LIST "$FullName,$exnumber,$email,$department\n";
		
		close LIST;
	
		open(LIST, "<$TesterFile") || die "Can't read file: $!";
	
		# PROBLEM IS HERE
		@FileContents = split(/,/, $TesterFile);
			
		print header, start_html( {-title=>'Beta Testers Application Results'} );
	
		print "$FileContents[0]";
	
		close LIST;
		print end_html;
	}
	
}

Come visit my website
------------------------------------
 
As audiopro says, you're splitting the filename, not the contents of LIST.

You could use map, e.g.
@contents= map {chomp; split /,/} <LIST>;

This will give you a "1D" array with one piece of info per "row", i.e
0: $FullName
1: $exnumber
2: $email
3: $department
...


or, for a "2D" array with 4 pieces of info per row:
@contents= map {chomp; [ split /,/ ]} <LIST>;
which gives
0: [$FullName, $exnumber, $email, $department]
...



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top