hello99hello
Programmer
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);
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);