G'day all
What I need to do:
1. Extract some values from a mySQL database and store them in an associate array.
my $sth_1 = $dbh->prepare("SELECT * FROM vote WHERE item_no='$last_ite
+m'");
$sth_1->execute();
while (my @row = $sth_1->fetchrow_array())
{
#### Define variables from the mySQL output.
my $description = $row[4];
my %results = ("$row[5]","$row[11]","$row[6]","$row[12]","$row[7]","$r
+ow[13]","$row[8]","$row[14]","$row[9]","$row[15]","$row[10]","$row[16
+]");
This is how I have been doing it, and it works... however I'm sure there is a more efficient method of doing it... I just can't nut it out.
Any suggestions ?
2. Then I need to calculate the total of the votes entered and then calculate these in percentages using the total (stored in
$row[11] ->
$row[16])
#### Turn the output into percentages.
my $total = $results{"$row[5]"} + $results{"$row[6]"} + $results{"$row
+[7]"} + $results{"$row[8]"} + $results{"$row[9]"} + $results{"$row[10
+]"};
$results{"$row[5]"} = ($results{"$row[5]"} / $total) * 100;
$results{"$row[6]"} = ($results{"$row[6]"} / $total) * 100;
$results{"$row[7]"} = ($results{"$row[7]"} / $total) * 100;
$results{"$row[8]"} = ($results{"$row[8]"} / $total) * 100;
$results{"$row[9]"} = ($results{"$row[9]"} / $total) * 100;
$results{"$row[10]"} = ($results{"$row[10]"} / $total) * 100;
I don't think this
is working... It is also very ugly and innefficient as with the first part of my question. Could anyone explain why it isn't working ? (the values are not being given data to store... uninitialised)
3. I then need to sort the associate array using the percentage as the item to sort by, so that the highest percentage is the first item in the associate array.
This I have little or no idea about... so any comments suggestions most appreciated.
Thanks in advance
lagrenouille
Edit: Added <code> tags. larsen
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.