-3

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";
4

3 回答 3

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]

于 2013-09-26T14:15:43.960 回答
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";

当然,其他问题已经被M42 在这里指出了。

于 2013-09-26T14:18:04.087 回答
1

猜测一下,您正在阅读您在文件句柄上打开的任何内容FILE以填充各种数组@DAY@MONTH。然后您尝试再次读取它以输出数组中的数据,但它已经在文件末尾,因此您的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";
于 2013-09-26T17:54:44.567 回答