Hello, and thank you. I have a question about some problems that my perl code was having. First, some background. I had to make some quick code to calculate the score of a FIRST Robotic competition match(using last year's rules) and then create a program that kept track of the average scores of all the teams, along with several other variables -- I thought perl would be perfect for this, and so I started coding. I ran across several problems, and that is why my code looks sloppy. . . I aplogize. But, the problem is with the output -- quite simply, it's incorrect. Here's the code for what it's worth:

(P.S.: above where it says "EXPERIMENTAL" all works correctly; that is merely the rules for scoring, and is non-important for this question, so far as I can tell)

# score.pl $RANKINGS = "rank.txt"; get_info(); process_info(); calc_scr(); #disp_scr(); rank(); sub get_info { print "---Enter the following data---\n", "\tNumber of black balls in goal:\t"; $blk_bls = <stdin>; print "\tNumber of big balls in goal:\t"; $big_bls = <stdin>; print "\tNumber of robots in endzone:\t"; $rbt_end = <stdin>; print "\tNumber of goals on the bridge:\t"; $gls_brg = <stdin>; print "\tNumber of seconds remaining:\t"; $tim_rmn = <stdin>; print "\t\tIs this a qualifying match(1 or 0):\t"; if(<stdin> == 1) { for(1..4) { print "\tDoes team $_ have its large ball in goal(1 or 0):\t"; $ball[$_] = <stdin>; } } for(1..4) { print "\tNumber of rule violations for team $_:\t"; $rl_vlt[$_] = <stdin>; } } sub process_info { $mult = $gls_brg + 1 + ($gls_brg == 2); if(tim_rmn <= 120 && tim_rmn >= 91) { $mult *= 3 } elsif(tim_rmn <= 90 && tim_rmn >= 61) { $mult *= 2.5 } elsif(tim_rmn <= 60 && tim_rmn >= 31) { $mult *= 2 } elsif(tim_rmn <= 30 && tim_rmn >= 15) { $mult *= 1.5 } else { $mult *= 1 } } sub calc_scr { $all_scr = ($blk_bls + $rbt_end * 10 + $big_bls * 10) * $mult; for(1..4) { $tm_scr[$_] = ($all_scr * (1 + $ball[$_] * .1)) * ((10 - $rl_vlt[$_]) * .1); } } sub disp_scr { for(1..4) { print "Team $_ score is: $tm_scr[$_]\n" } } ## ...EXPERIMENTAL... sub get_team_info { for(1..4) { print "\tEnter number of team $_:\t"; $tm_nmb[$_] = <stdin>; if($fst_tm[$_] = first_time($tm_nmb[$_])) { print "\tEnter names of team members, separated by space\n", "\t(e.g., mrawls srawls):\t"; $mbr_nms[$_] = <stdin>; } } } sub first_time { open(RANKINGS) or die "Failed to open $RANKINGS, at"; if(<RANKINGS> =~ /New File/) { $new_file = 1; return 1; } @entries = split /^<>$/, <RANKINGS>; @numbers = map { ($a,$b) = split /:/,$_,2; $a } @entries; for $n(@numbers) { if($n == $_[0]) { return } } return 1; } sub rank { get_team_info(); open(RANKINGS) or die "Failed to open $RANKINGS, at"; @entries = split /^<>$/, <RANKINGS>; for $i(1..4) { if(!$fst_tm[$i]) { $j = -1; for(@numbers) { $j++; $_ == $tm_nmb[$i] && break; } ($tn,$as,$hs,$mn,$nm) = split /:/, $entries[$j]; $as = ($as + $tm_scr[$i]) / 2; $tm_av[$i] = $as; if($tm_scr[$i] > $hs) { $hs = $tm_scr[$i] } ++$nm; $tm_rank[$i] = "\n$tn:$as:$hs:$mn:$nm\n"; } else { $tm_nmb[$i] =~ s/\n//; $mbr_nms[$i] =~ s/\n//; $tm_rank[$i] = "\n$tm_nmb[$i]:$tm_scr[$i]:$tm_scr[$i]:$mbr_nms[$i]:1 +\n" } } if(!$new_file) { @averages = map { ($a,$b,$c) = split /:/, $_, 3; $b } @entries; for $i(1..4) { for(0..$#averages) { if($averages[$_] < $tm_avg[$i]) { @part_1 = (@entries)[0..$_-1]; @part_2 = ($tm_avg[$i],(@entries)[$_..$#entries]); @sorted = (@part_1,@part_2); open(RANKINGS,">$RANKINGS"); print RANKINGS join '<>', @sorted; } } } } else { open(RANKINGS,">$RANKINGS"); @sorted = sort { (split /:/, $b)[1] <=> (split /:/, $a)[1] } @tm +_rank; pop @sorted; print RANKINGS join '<>', @sorted; } }

In reply to Ranking/Score keeping by Anonymous Monk

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.