Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Find odd/even elements of array

by dsheroh (Parson)
on Oct 10, 2011 at 10:34 UTC ( #930578=note: print w/ replies, xml ) Need Help??


in reply to Find odd/even elements of array

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 }


Comment on Re: Find odd/even elements of array
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://930578]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-08-30 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls