Here is an example of how the many variables can be replaced with a few arrays in the datafilla sub
sub datafilla { for my $y (1..5){ my $y_diff = ($f_mpy[$y] - $f_mpy[$y-1]) || 1; my $e_diff = ($f_mpe[$y] - $f_mpe[$y-1]); $popbyyer[$y] = $e_diff / $y_diff; $grand[$y] = $grand[$y-1] + ( ($e_diff<0) ? abs ($e_diff) : 0 ); } }
and a test page to check it works as expected
#!/usr/bin/perl use strict; use CGI; use CGI::Carp 'fatalsToBrowser'; our $VERSION = 3.11; #INPUTS form values my $q = CGI->new(); my $f_copyerr = $q->param('copyerr'); my $f_model = $q->param('model'); my $f_initial = $q->param('initial'); my @f_mpy=(); # year my @f_mpe=(); # estimate foreach (0..5){ $f_mpe[$_] = $q->param('mpe'.$_) || 0; $f_mpy[$_] = $q->param('mpy'.$_) || 1; } $f_mpy[0]=0; # CALCS my @grand=(0); my @popbyyer=($f_mpe[0]); datafilla(); my $gener = (reverse sort @f_mpy)[0]; my $total = (reverse sort @f_mpe)[0] + $grand[5] - 1; # RESULTS my $results = results(); # print page print "Content-type: text/html\n\n"; print qq(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'> <head><meta http-equiv='Content-Type' content='text/html; charset=iso- +8859-1' /> <meta http-equiv='Content-Style-Type' content='text/css' /> <style type='text/css'> td {padding: .5em; min-width:2.5em; } </style> <title>Model $f_model</title> </head> <body> $results <p>Program Ver: $VERSION</p </body> </html>); # datafilla sub datafilla { for my $y (1..5){ my $y_diff = ($f_mpy[$y] - $f_mpy[$y-1]) || 1; my $e_diff = ($f_mpe[$y] - $f_mpe[$y-1]); $popbyyer[$y] = $e_diff / $y_diff; $grand[$y] = $grand[$y-1] + ( ($e_diff<0) ? abs ($e_diff) : 0 ); } } # calculation results sub results { my $html = q{<table border="1"> <tr> <td>ix</td> <td>f_mpy</td> <td>f_mpe</td> <td>popbyyer</td> <td>grand</td> </tr>}; for my $ix (0..5){ $html .= qq{ <tr> <td>$ix</td> <td>$f_mpy[$ix]</td> <td>$f_mpe[$ix]</td> <td>$popbyyer[$ix]</td> <td>$grand[$ix]</td> </tr>}; } $html .= qq{</table> gener = $gener<br/> total = $total<br/>}; return $html; }
poj

In reply to Re: This runs WAY too slow by poj
in thread This runs WAY too slow by Dandello

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.