http://www.perlmonks.org?node_id=1050713

jdlev has asked for the wisdom of the Perl Monks concerning the following question:

Having some difficulty designing this program. Here's the specs:

1) Pull player info out of DB by position, and insert information into an array at each position. @QB, @RB, @WR, etc... I'm not sure how to store the information from the DB in an array? I assume the best way to do this is to put all the DB info in an array and iterate through the arrays so the program doesn't do 50 Billion calls to the DB. I'm guessing each record would have to be saved as a multidimensional array, because you have your player ID, which could represent the first dimension, and then you have a key and a value based on the record? So, I guess what I'm saying is if I have the following info:

player_id: 1
player_name: Jeff
player_salary: 5
What would that array look like to index it by it's id first, and then using the keys, player_name, set to value Jeff?

I can't figure out how to get the information from the database in a useable format WHILE keeping track of the important statistics. I don't just want max value, I want to know the variables that lead to the max value. TIA for any advice.

I love it when a program comes together - jdhannibal

Replies are listed 'Best First'.
Re: Multidimensional Array Iteration?
by Your Mother (Archbishop) on Aug 23, 2013 at 19:15 UTC

    This sounds like an SQL question so far; you can do pretty complicated stats and such without ever leaving the DB. Sample input (meaning schema info probably) and desired output would be more likely to get feedback, advice, or sample code.

Re: Multidimensional Array Iteration?
by glenn (Scribe) on Aug 23, 2013 at 19:43 UTC

    my @qb; foreach my $position ("qb","rb","wr") { my @data = #query database; for (my $d = 0; $d < @data; $d++) { if ($position eq "qb") { my @temp = ($data[$d], $data[$d+1], $data[$d+2]); push (@qb,@temp); #--or-- push (@qb,($data[$d], $data[$d+1], $data[$d+2])); #--or-- push (@qb,[$data[$d], $data[$d+1], $data[$d+2]]); $d+=4; } } } foreach my $player (@qb) { print $@player[0] $@player[1] $@player[2]; }
Re: Multidimensional Array Iteration?
by marinersk (Priest) on Aug 23, 2013 at 20:37 UTC
    When you use DBI to pull the data, one of the ways you can pull it is in an array of hashes.

    Each element in the array is a row from the database.

    Each element in the hash is the column -- and the hash allows you to access it by name.

    So the answer to your question on how to access it by index and column name -- is to do so. It's already in that format if you run the query that way.