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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File&Record Handling

Status
Not open for further replies.

hello99hello

Programmer
Jul 29, 2004
50
0
0
CA
Hello,

My intention here is to:

check if the file(tayo.txt) exist in directory.
opean the file, and read one record at a time.
for each record, put it in an array.
check the second element of the array, if it is not "M", change it to M.
put the record back into new file in the same directory.


However, I got stuck because I am getting "could not open outfile"

Could anyone help.

#!/usr/bin/perl
#print "Hello World. \n";
#check if file exist
my ($input_file ) ;
my ($input_record) ;
$input_file = "tayo.txt";
if (-e "$input_file"){
print "the file exist \n" ;
}else
{
print "The file does not exsit\n";
}
#open the file, and read each record into array
open(INPUT, "$input_file") || die ("could not open infilefile \n");
#while file open, read each element of the array and maniplate.
while (<INPUT>){
chomp($input_file) ;
my @array_record = split(/,/,);
s/^'|"$//g foreach @array_record;
if ($array_record[2]= "M")
{
print "record is male\n" ;
}else
{
#The element has to change M
$array_record[2] = M
Print "record is now $array_record[2]\n";
}
#write new record to output file
open (OUTPUT, ">$output_file") || die ("could not open outfile \n");
print OUTPUT ">$output_file\n";
}
close (INPUT);
close (OUTPUT);
 
Well, first thing I see is that the variable $output_file is not being set anywhere. You are trying to output to a null location.
 
Try to put put your replies in the text box below. You are starting several threads with the same subject.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top