The simplest solution, assuming unique student IDs, is so simple and somewhat idiosyncratic to Perl that my reflex is to suspect this of being a homework question... That solution is "turn your list into a hash":
my @student_info = ("001","John","002","Mary","003","Tom");
my %hash = @student_info;
say join ", ", sort keys %hash;
As a side note, you really don't want to get into the habit of sticking variable data directly into your SQL statements. Use placeholders instead.
Taking all of the above into account, your posted code becomes something like:
my @student_info = ("001","John","002","Mary","003","Tom");
my %name_hash = @student_info;
my $grade_sth = $dbh->prepare("select id from grades where id = ? and
+grade = 'A'");
for my $student_id (sort keys %name_hash){
$grade_sth->execute($student_id);
my $student_name = $name_hash{$student_id};
# do output here
}
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.