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!

PERL: Getting input from a txt file into an array

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:
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";
	
	open(LIST, ">>$TesterFile") || die "Can't open file: $!";
	
	if ($email =~ m/@/) {
		print LIST "$FullName,$exnumber,$email,$department\n";
	}
	close LIST;
	
	open(LIST, "<$TesterFile") || die "Can't open file: $!";
	
	# PROBLEM IS HERE
	@FileContents = split(/,/, $TesterFile);
	
	close LIST;
	
	print header, start_html( {-title=>'Beta Testers Application Results'} );
	
	print "$list[0]";
	
	
	print end_html;
	
}

Come visit my website
------------------------------------
 
Your problem is in the variable you're using when doing the split. You're splitting on $Testerfile, which, according to your code is c:/progra~1/apache~1/appache/bin/list.txt.

To read your information from the file, you need to loop though the contents of the file itself, and split on each line.

Code:
open (FILE, "$myfile");
while (my $line = <FILE>)
{
    #perform processing on line
}
close (FILE);

Alternately, you could slurp the entire file into an array and process information out of that array and element at a time.

Code:
open (FILE, "$myfile");
my @file = <FILE>;
close (FILE);
foreach my $line (@file)
{
    #perform processing here
}

- Rieekan
 
Code:
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";
    
    open(LIST, ">>$TesterFile") || die "Can't open file: $!";
    
    if ($email =~ m/@/) {
        print LIST "$FullName,$exnumber,$email,$department\n";
    }
    close LIST;
    
    open(LIST, "<$TesterFile") || die "Can't open file: $!";
    my @array = <LIST>;
    close LIST;
    
    print header, start_html( {-title=>'Beta Testers Application Results'} );
    
    foreach $element(@array)  {
        ($item1,$item2,$item3,item4) = split/,/,$element
        print $item1.." - ".$item2."<br>";
    }
    
    print end_html;
    
}

Rob Waite
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top