IV。将以下输出写入一个新文件,其中列由制表符(“\ t”)分隔: 日期,月份,最高温度,最低温度,最高湿度,最低湿度
#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:我通过一些人的亲切建议做了以下更改,但我没有输出......我不知道为什么,我做了一些根本错误的事情?阵列DO存在btw
# 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";
答案 0 :(得分:4)
您的错误来自:
$fh=$headers;
print "$fh\n";
和
$fh=join('\t',@riteline);
print "$fh\n";
你要写:
print $fh $headers,"\n";
和
print $fh join("\t",@riteline),"\n";
我觉得你想要的最后一个:
print $fh @riteline,"\n";
此外,请勿使用@DAY[$ii]
,$DAY[$ii]
答案 1 :(得分:3)
我的错误消息刚刚出现:(
因为你说:
open (my $fh,'>', 'DATAEXPORT.txt') | die "Could not open file :( fix bugs";
说:
open (my $fh,'>', 'DATAEXPORT.txt') || die "Could not open file :( fix bugs";
或
open (my $fh,'>', 'DATAEXPORT.txt') or die "Could not open file :( fix bugs";
答案 2 :(得分:1)
猜测一下,您正在阅读文件句柄FILE
上打开的任何内容,以填充各种数组@DAY
,@MONTH
等等。然后您正在尝试阅读 AGAIN 输出数组中的数据,但它已经在文件末尾,因此while
循环永远不会执行。
除了申请
use strict;
use warnings;
到代码的开头,并根据需要使用my
声明所有变量,我建议改为
my @headers = ('DATE', 'MONTH', 'MAX TEMP', 'MIN TEMP', 'MAX HUMID', 'MIN HUMID');
open my $fh, '>', 'DATAEXPORT.txt' or die "Could not open file: $!";
my @columns = \(@DAY, @MONTH, @MAX_TEMPERATURE, @MIN_TEMPERATURE, @MAX_HUMIDITY, @MIN_HUMIDITY);
print join("\t", @headers), "\n";
for my $i (0 .. $#DAY) {
my @line = map $_->[$i], @columns;
print $fh join("\t", @line), "\n";
}
close $fh;
print "HW 2 complete";