My code to write a .txt file from my @data isn't working :(. What am I doing wrong? I am using a while loop

大兔子大兔子 提交于 2019-12-13 09:49:56

问题


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

EDIT1: 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";

回答1:


Your errors come from :

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

and

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

You' d write:

print $fh $headers,"\n";

and

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

for the last one I think you want:

print $fh @riteline,"\n";

Also, don't use @DAY[$ii] but $DAY[$ii]




回答2:


My error msg just comes up :(

It would because you say:

open (my $fh,'>', 'DATAEXPORT.txt') | die "Could not open file :( fix bugs";

Say:

open (my $fh,'>', 'DATAEXPORT.txt') || die "Could not open file :( fix bugs";

or

open (my $fh,'>', 'DATAEXPORT.txt') or die "Could not open file :( fix bugs";

Of course, the other issues have been pointed out by M42 here.




回答3:


At a guess, you are reading through whatever you have opened on filehandle FILE to populate the various arrays @DAY, @MONTH etc. Then you are trying to read it AGAIN to output the data in the arrays, but it is already at end of file so your while loop never executes.

In addition to applying

use strict;
use warnings;

to the beginning of your code, and declaring all your variables with my as appropriate, I suggest this instead

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


来源:https://stackoverflow.com/questions/19030643/my-code-to-write-a-txt-file-from-my-data-isnt-working-what-am-i-doing-wro

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!