iv. 将以下输出写入一个新文件,其中的列由制表符 ("\t") 分隔:DATE、MONTH、MAX TEMP、MIN TEMP、MAX HUMID、MIN HUMID
#a declare vars
@riteline=();
my $headers;
my $riteAline;
print 'iv. Write the following output to a new file with columns separated by tabs ("\t"):';
print "\n DATE, MONTH, MAX TEMP, MIN TEMP, MAX HUMID, MIN HUMID";
$headers= ("DATE,MONTH,MAX TEMP,MIN TEMP,MAX HUMID,MIN HUMID");
$headers=~ tr/,/\t/;
#first you can turn the arrays into string
open (my $fh,'>', 'DATAEXPORT.txt') | die "Could not open file :( fix bugs";
$i=0;
$ii=0;
#the loop to match data by index and seperate with tab
while (<FILE>) {
chomp;
if($i=0){
$fh=$headers;
print "$fh\n";
$i=1;
}else{
@riteline=(@DAY[$ii],"\t",@MONTH[$ii],"\t",@MAX_TEMPERATURE[$ii],"\t",@MIN_TEMPERATURE[$ii],"\t",@MAX_HUMIDITY[$ii],"\t",@MIN_HUMIDITY[$ii]);
$fh=join('\t',@riteline);
print "$fh\n";
$ii++
}
};
close (FILE);
print "HW 2 complete";
My error msg just comes up :(
EDIT1:我根据一些人的亲切建议进行了以下更改,但我没有输出....我不知道为什么,我做错了什么?顺便说一句,数组确实存在
# iv. Write the following output to a new file with columns separated by tabs ("\t"):
# DATE, MONTH, MAX TEMP, MIN TEMP, MAX HUMID, MIN HUMID
# a delacre
@riteline = ();
my $headers;
print 'iv. Write the following output to a new file with columns separated by tabs ("\t"):';
print "\n DATE, MONTH, MAX TEMP, MIN TEMP, MAX HUMID, MIN HUMID";
$headers = ('DATE,MONTH,MAX TEMP,MIN TEMP,MAX HUMID,MIN HUMID');
$headers =~ tr/,/\t/;
# first you can turn the arrays into string
open(my $fh, '>', 'DATAEXPORT.txt') || die "Could not open file :( fix bugs";
$i = 0;
$ii = 0;
# the loop to match data by index and seperate with tab
while (<FILE>) {
chomp;
if ($i == 0) {
print $fh $headers, "\n";
$i = 1;
}
else {
@riteline = (
$DAY[$ii], "\t", $MONTH[$ii], "\t",
$MAX_TEMPERATURE[$ii], "\t", $MIN_TEMPERATURE[$ii], "\t",
$MAX_HUMIDITY[$ii], "\t", $MIN_HUMIDITY[$ii]
);
print $fh join("\t", @riteline), "\n";
print $fh @riteline, "\n";
$ii++;
}
}
close(FILE);
print "HW 2 complete";