Maybe my description is not clear. I'm trying to get every IP that is associated with one particular department, so I can create a list and feed into an external program. Since there are multiple colleges, and each may have multiple departments, I figured that I need to save them into arrays, so I can have a nested loop. I'd be delighted if there are better ways to do it. Thanks! | [reply] |
| [reply] |
I'd love to hire someone if I was rich. :) Anyway, this is what I attempted to do:
#$colleges = sth->fetchrow_array();
#while (my($colleges)=$colsth->fetchrow_array()){
# print "$colleges \n";
# $myQuery = "select d.departmentName from Colleges c join Departments d on c.college_id=d.college_id";
# $deptsth = &exeQuery($mydbh, $myQuery);
# while (my($departments)=$colsth->fetchrow_array()){
# print "$departments \n";
# }
#}
| [reply] |
The point about the query that Corion suggested is that you get a single stream of rows returned from the database, each row contains a college name, a department name, an "itstuff" name, and an IP address, and you get one row for every IP address that exists in your IP table.
From that single stream of rows, you can split the results into multiple arrays if you want to (one for each college, or one for each college/department, or one for each college/department/itstuff) -- although it might be better to arrange them into a hash structure -- but in any case, you get to process all the data in a single loop over the query output. Something like this:
my $sql = "select ..."; # query string as suggested by Corion
my $sth = $dbh->prepare( $sql );
$sth->execute;
my $row_data = $sth->fetchall_arrayref; # gets all data from query
my %data_structure;
for my $row ( @$row_data ) {
my ( $college, $dept, $it, $ip ) = @$row;
$data_structure{$college}{$dept}{$it} = $ip;
}
You haven't said yet what you really want to do once you have the data from the tables, but there's a good chance that a hash structure will be more useful than a bunch of arrays.
BTW, you said at the top that you had 5 tables, but you only talk about 4. If there really are 5, you should be able to figure out how to extend Corion's query and the code snippet shown here.
Also, do please look at some of the links provided on the posting form: Writeup Formatting Tips, How do I post a question effectively, and Markup in the Monastery -- it's for your own good. | [reply] [d/l] |