My script retrieves some data from a server into a variable. Here's a snippet that prints the data to STDOUT:
my $ua = init_lwp(); my $uri = create_query2($url, $newdate); my $resp_dat = execute_query($ua, $uri); print $resp_dat;
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); subgen($resp_dat);
Here's the routine:
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); }
I've tried receiving it as @_ within the sub, but I think I'm invoking the <> incorrectly. Advice?

In reply to Passing an array to a subroutine by gisrob

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.