This works fine, but I want to insert a subroutine that I wrote separately into the script to parse the data and write to a file. The subroutine worked when I was testing it on its own with a sample data file. Now that I have the $resp_dat variable, I don't know how to send it to the subroutine. Or rather, I'm not sure how to receive it properly inside the subroutine. A modified version of the above snippet looks like this:my $ua = init_lwp(); my $uri = create_query2($url, $newdate); my $resp_dat = execute_query($ua, $uri); print $resp_dat;
Here's the routine:my $ua = init_lwp(); my $uri = create_query2($url, $newdate); my $resp_dat = execute_query($ua, $uri); subgen($resp_dat);
I've tried receiving it as @_ within the sub, but I think I'm invoking the <> incorrectly. Advice?sub subgen { my($aref) = @_; my($cur_segno) = 0; my(@sst) = (); #open(FILE, my $aref); #or die "Cannot open: $file\n"; my $hdr = <>; #read in the first line chomp $hdr; my @colnames = split /, ?/, $hdr; #split the header into an array open GENFILE, '>genfile.txt'; open INFOFILE, '>infofile.txt'; my $sst_mean; my %fronts; while (<>) { chomp; next unless s/\"//g; # this will skip the first line and remov +e any " my(@colvalues) = split /, ?/; #read in the data @fronts{@colnames} = @colvalues; if ($fronts{segno2} == $cur_segno) { print GENFILE "$fronts{lon}, $fronts{lat}\n"; push(@sst, $fronts{sst}); } else { if ($cur_segno != 0) { print GENFILE "END\n"; print GENFILE "$fronts{segno2}\n", "$fronts{lon}, $fronts{lat}\n"; $sst_mean = calc_mean( \@sst ); print INFOFILE "$sst_mean\n", "$fronts{segno2}, "; } else { print GENFILE "$fronts{segno2}\n", "$fronts{lon}, $fronts{lat}\n"; print INFOFILE "$fronts{segno2}, "; } $cur_segno = $fronts{segno2}; @sst = (); } print GENFILE "END\n"; } close(FILE); close(GENFILE); close(INFOFILE); }
In reply to Passing an array to a subroutine by gisrob
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |