Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi,

Further to my idea of presorting the hash keys described in Re^2: Perl Program - Out of memory? (rather that sorting them again and again), I would suggest that you try the following modified program.

#USAGE EXAMPLE - Max Points Replaced If Higher Value Found require 'funcs_top10.pl'; my $delay = 3; my $maxSalary = 100000; my $programStartTime = localtime(); my $maxPoint; my $qbHash = getStats("QB"); #Sorts Player Statistics Into Hashes Acco +rding To Position my $rbHash = getStats("RB"); my $wrHash = getStats("WR"); my $teHash = getStats("TE"); my $dstHash = getStats("DST"); my $flHash = getFlexStats(); my @qb_keys = sort keys %$qbHash; my @rb_keys = sort keys %$rbHash; my @wr_keys = sort keys %$wrHash; my @te_keys = sort keys %$teHash; my @dst_keys = sort keys %$dstHash; my @fl_keys = sort keys %$flHash; foreach my $key (@qb_keys) { my $QB1i = $qbHash->{$key}->{PlayerID}; my $QB1s = $qbHash->{$key}->{Salary}; my $QB1p = $qbHash->{$key}->{FPPG}; my $QB1pos = $qbHash->{$key}->{Position}; #print "QB1 count = $qb1count \n"; #$qb1count++; foreach my $key (@qb_keys) { my $QB2i = $qbHash->{$key}->{PlayerID}; my $QB2s = $qbHash->{$key}->{Salary}; my $QB2p = $qbHash->{$key}->{FPPG}; my $QB2pos = $qbHash->{$key}->{Position}; #print "--QB2 count = $qb1count \n"; #$qb2count++; next if $QB1i == $QB2i; foreach my $key (@rb_keys) { my $RB1i = $rbHash->{$key}->{PlayerID}; my $RB1s = $rbHash->{$key}->{Salary}; my $RB1p = $rbHash->{$key}->{FPPG}; my $RB1pos = $rbHash->{$key}->{Position}; #print "----RB1 count = $rb1count \n"; #$rb1count++; foreach my $key (@rb_keys) { my $RB2i = $rbHash->{$key}->{PlayerID}; my $RB2s = $rbHash->{$key}->{Salary}; my $RB2p = $rbHash->{$key}->{FPPG}; my $RB2pos = $rbHash->{$key}->{Position}; #print "------RB2 count = $rb2count \n"; #$rb2count++; next if $RB1i == $RB2i; foreach my $key (@wr_keys) { my $WR1i = $wrHash->{$key}->{PlayerID}; my $WR1s = $wrHash->{$key}->{Salary}; my $WR1p = $wrHash->{$key}->{FPPG}; my $WR1pos = $wrHash->{$key}->{Position}; #print "--------WR1 count = $wr1count \n"; # $wr1count++; foreach my $key (@wr_keys) { my $WR2i = $wrHash->{$key}->{PlayerID}; my $WR2s = $wrHash->{$key}->{Salary}; my $WR2p = $wrHash->{$key}->{FPPG}; my $WR2pos = $wrHash->{$key}->{Position}; next if $WR1i == $WR2i; foreach my $key (@te_keys) #TE1 Start { my $TE1i = $teHash->{$key}->{PlayerID}; my $TE1s = $teHash->{$key}->{Salary}; my $TE1p = $teHash->{$key}->{FPPG}; my $TE1pos = $teHash->{$key}->{Position}; foreach my $key (@dst_keys) #DST Start { my $DSTi = $dstHash->{$key}->{PlayerID +}; my $DSTs = $dstHash->{$key}->{Salary}; my $DSTp = $dstHash->{$key}->{FPPG}; my $DSTpos = $dstHash->{$key}->{Positi +on}; foreach my $key (@fl_keys) #FL1 Start { my $FL1i = $flHash->{$key}->{Playe +rID}; my $FL1s = $flHash->{$key}->{Salar +y}; my $FL1p = $flHash->{$key}->{FPPG} +; my $FL1pos = $flHash->{$key}->{Pos +ition}; next unless (($FL1i != $RB1i) && ( +$FL1i != $RB2i) && ($FL1i != $WR1i) && ($FL1i != $WR2i) && ($FL1i != +$TE1i)); foreach my $key (@fl_keys) #FL2 St +art { my $FL2i = $flHash->{$key}->{P +layerID}; my $FL2s = $flHash->{$key}->{S +alary}; my $FL2p = $flHash->{$key}->{F +PPG}; my $FL2pos = $flHash->{$key}-> +{Position}; next unless (($FL2i != $RB1i) +&& ($FL2i != $RB2i) && ($FL2i != $WR1i) && ($FL2i != $WR2i) && ($FL2i + != $TE1i) && ($FL1i != $FL2i)); ############################## +################################# my $newMaxSalary = $QB1s + $QB +2s + $RB1s + $RB2s + $WR1s + $WR2s + $TE1s + $FL1s; my $newMaxPoint = $QB1p + $QB2 +p + $RB1p + $RB2p + $WR1p + $WR2p + $TE1p + $FL1p; next unless $newMaxSalary < $m +axSalary; #print "Current Salary = " . $ +newMaxSalary . "\n"; if (!$maxPoint) { $maxPoint = $newMaxPoint; next; } next unless $maxPoint < $newMa +xPoint; $maxPoint = $newMaxPoint; print "New Max Points Reached! + " . $maxPoint . "\n"; print "Here is your team! \n"; print "QB1 = " . $QB1i . "\n"; print "QB2 = " . $QB2i . "\n"; print "RB1 = " . $RB1i . "\n"; print "RB2 = " . $RB2i . "\n"; print "WR1 = " . $WR1i . "\n"; print "WR2 = " . $WR2i . "\n"; print "TE1 = " . $TE1i . "\n"; print "FL1 = " . $FL1i . "\n"; print "FL2 = " . $FL2i . "\n"; print "DST = " . $DSTi . "\n"; print "*********************** +******************** \n \n"; sleep($delay); ############################## +###################################### }#FL2 End }#FL1 End }#DST End }#TE1 End }#WR2 End }#WR1 End }#RB2 End }#RB1 End }#QB2 End }#QB1 End print "--------------- Projected Team Points = $maxPoint ------------- +-\n"; print '$$$$$$$$$$$$$$$ Projected Team Salary = '.$salary.' $$$$$$$$$$$ +$$$$' . "\n\n"; my $programStopTime = localtime(); print "Program Start Time - $programStartTime, Program Stop Time - $pr +ogramStopTime \n";

I made the following changes: presorting the hash keys into arrays and using the arrays of keys to walk though the data, added my statements to declare your variables so that you can enable warnings and strictures (I might have missed some, I can't really test or compile).

Three additional points: (1) I do not see the point of using a delay in your most inner loop, I think you can remove it. (2) The fact that you did not give lexical scope to your $key loop variable is probably a major bug in your program, because I think that the $key variable at one level of nesting gets overwritten by the last value taken by $key within the next nested loop (not entirely sure though, I have not tried in ages to use such loop variable without lexically scoping it, maybe Perl can manage this correctly, but I would be surprised). OTOH, it may have no impact because starting the inner loop is the last thing you do each time, but this is poor and dangerous design. Anyway, this should no be the case with my version above, where each $key variable is lexically scoped within its loop. (But, again, I can't test anything.) (3) Finally, the $salary variable used at the end of the program seems to be coming from nowhere. Using warning and strictures would detect this, just as the fact that you are using $maxPoint and $maxPoints.


In reply to Re: Perl Program - Out of memory? by Laurent_R
in thread Perl Program - Out of memory? by jdlev

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-19 20:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found