我从@data 编写 .txt 文件的代码不起作用:(.我做错了什么?我正在使用 while 循环 [英] My code to write a .txt file from my @data isn't working :(. What am I doing wrong? I am using a while loop

查看:28
本文介绍了我从@data 编写 .txt 文件的代码不起作用:(.我做错了什么?我正在使用 while 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

四.将以下输出写入一个新文件,列由制表符 ("\t") 分隔:日期、月份、最高温度、最低温度、最高湿度、最低湿度

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 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 :(

我根据一些人的慷慨建议进行了以下更改,但没有输出....我不确定为什么,我做错了什么吗?数组确实存在 btw

I made the following changes by a few people's gracious suggestions, but I have no output.... I am not sure why, am I doing something fundamentally wrong? The arrays DO exist 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";

推荐答案

您的错误来自:

$fh=$headers;
print "$fh\n";

$fh=join('\t',@riteline);
print "$fh\n";

你会写:

print $fh $headers,"\n";

print $fh join("\t",@riteline),"\n";

对于我认为你想要的最后一个:

for the last one I think you want:

print $fh @riteline,"\n";

另外,不要使用@DAY[$ii]而是$DAY[$ii]

这篇关于我从@data 编写 .txt 文件的代码不起作用:(.我做错了什么?我正在使用 while 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆