Can someone help me with my Perl script? For some reason, open($info, \">info.tx
ID: 3834864 • Letter: C
Question
Can someone help me with my Perl script? For some reason, open($info, ">info.txt") clears the file but fails to write the data into it. I've used an array of methods for writing into the file, and even the append and "+" combinations fail to work. The "append" methods don't even write into the file at all. I've even used ">>", "+>", and "+>>" with no luck. The "w", "r", and "a" always gives me this:
Can't open data at /cygdrive/c/Users/admin/Desktop/a14.pl line 11, line 3.
The current script I've attached to this message results in the following output:
-------------------------------------------------------------------------------
admin@Dell /cygdrive/c/users/admin/desktop
$ perl /cygdrive/c/Users/admin/Desktop/a14.pl
Full Name: Firstname Lastname
Age: 30
Gender: Female
Marital Status: Single
Do you want to change the age? (Y/y or N/n): y
What is the new age? 27
Do you want to change the Marital Status? (Y/y or N/n): n
The result is:
Full Name:
Age:
Gender:
Marital Status:
admin@Dell /cygdrive/c/users/admin/desktop
$ perl /cygdrive/c/Users/admin/Desktop/a14.pl
Full Name:
Age:
Gender:
Marital Status:
Do you want to change the age? (Y/y or N/n):
admin@Dell /cygdrive/c/users/admin/desktop
$
-------------------------------------------------------------------------------
Here's the script
-------------------------------------------------------------------------------
#!/usr/bin/perl
open(DATA,"<info.txt") or die "Can't open data"; $file[0]=<DATA>; $file[1]=<DATA>; $file[2]=<DATA>; $file[3]=<DATA>; close (DATA);
print " Full Name: $file[0] Age: $file[1] Gender: $file[2] Marital Status: $file[3]";
print " Do you want to change the age? (Y/y or N/n): "; $a = <STDIN>; chomp($a);
if($a eq'Y' or $a eq 'y'){ print "What is the new age? "; $file2[1]= <STDIN>; $c=$a; }
print " Do you want to change the Marital Status? (Y/y or N/n): ";
$b = <STDIN>; chomp($b);
if($b eq 'Y' or $b eq 'y') { print "What is the new marital status? "; $file2[3] = <STDIN>; $c=$b; }
if($c eq 'Y' or $c eq 'y') {
$file2[0]=$file[0]; $file2[2]=$file[2];
open($info, "> info.txt") or die "Can't open data";
$info = join('',$file2[0], $file2[1], $file2[2], $file2[3]);
close $info;
open(DATA,"<info.txt") or die "Can't open data"; $file[0]=<DATA>; $file[1]=<DATA>; $file[2]=<DATA>; $file[3]=<DATA>; close (DATA);
print "The result is: Full Name: $file[0] Age: $file[1] Gender: $file[2] Marital Status: $file[3]";
}
Is it because Cygwin's incompatible? Is it the script itself? I don't know if it's a software issue, an error in my script, or if it's my system. Is there any advice or tips that you can provide?
Thank you.
Explanation / Answer
Here is your fixed code.
#!/usr/bin/perl
open(DATA,"<info.txt") or die "Can't open data";
# open and read first 4 file lines
$file[0]=<DATA>; chomp($file[0]);
$file[1]=<DATA>; chomp($file[1]);
$file[2]=<DATA>; chomp($file[2]);
$file[3]=<DATA>; chomp($file[3]);
close (DATA);
print " Full Name: $file[0] Age: $file[1] Gender: $file[2] Marital Status: $file[3] ";
print " Do you want to change the age? (Y/y or N/n): ";
my $c = 'n';
$a = <STDIN>; chomp($a);
if($a eq'Y' or $a eq 'y') {
print "What is the new age? ";
$file2[1]= <STDIN>;chomp($file2[1]);
$c=$a;
}
else
{
$file2[1] = $file[1];
}
print " Do you want to change the Marital Status? (Y/y or N/n): ";
$b = <STDIN>; chomp($b);
if($b eq 'Y' or $b eq 'y') {
print "What is the new marital status? ";
$file2[3] = <STDIN>; chomp($file2[3]);
$c=$b;
}
else
{
$file2[3] = $file[3];
}
if($c eq 'Y' or $c eq 'y') {
$file2[0]=$file[0];
$file2[2]=$file[2];
open(INFO, ">info.txt") or die "Can't open data";
print INFO join(" ",$file2[0], $file2[1], $file2[2], $file2[3]);
close INFO;
open(DATA,"<info.txt") or die "Can't open data";
$file[0]=<DATA>; chomp($file[0]);
$file[1]=<DATA>; chomp($file[1]);
$file[2]=<DATA>; chomp($file[2]);
$file[3]=<DATA>; chomp($file[3]);
close (DATA);
print "The result is: Full Name: $file[0] Age: $file[1] Gender: $file[2] Marital Status: $file[3] ";
}