gisrob has asked for the wisdom of the Perl Monks concerning the following question:
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); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing an array to a subroutine
by suaveant (Parson) on Jul 31, 2001 at 19:10 UTC | |
by gisrob (Novice) on Jul 31, 2001 at 19:38 UTC | |
by suaveant (Parson) on Jul 31, 2001 at 21:04 UTC | |
by gisrob (Novice) on Jul 31, 2001 at 22:49 UTC | |
by chromatic (Archbishop) on Aug 01, 2001 at 00:06 UTC | |
| |
|
Re: Passing an array to a subroutine
by John M. Dlugosz (Monsignor) on Jul 31, 2001 at 19:23 UTC |