in reply to How to read multiple tables into arrays

If you have all the data in a database, why are you using Perl to sort it? Just query the information per departement in one go:

select college.name, department.name, itstuff.name, ip.address from college left join department on college.id = department.college left join itstuff on department.id = itstuff.department left join ip on itstuff.id = ip.itstuff

... at least that's how I read your textual description.

Replies are listed 'Best First'.
Re^2: How to read multiple tables into arrays
by pinkgladiator (Initiate) on Nov 10, 2008 at 22:08 UTC
    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!

      Just build a query that takes the college/department as parameters. Then loop over colleges and departments (from two other queries) returning what you want. Really we can't help you unless you show what your stuck at...unless you want to hire someone? Then you are probably in the wrong place.


      ___________
      Eric Hodges
        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";
        # }
        #}
      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.