in reply to appending an array to a hash

Assuming that the first column contains the value you want as the hash key, this would work:
my @row; my %hash; while (@row = $sth5->fetchrow_array) { $hash{$row[0]} = \@row; }

Update: Oops, merlyn pointed out my error. You'd want this instead:
my %hash; while (my @row = $sth5->fetchrow_array) { $hash{$row[0]} = \@row; }
Hot Pastrami

Replies are listed 'Best First'.
Re: Re: appending an array to a hash
by merlyn (Sage) on Mar 22, 2001 at 02:44 UTC
Re: Re: appending an array to a hash
by Anonymous Monk on Mar 22, 2001 at 03:10 UTC
    if the selection from the database returns many rows how would i enter each row into the hash so that i can access each row element buy element and output them. so when i print the html
    <td> round1 team name</td>
    how can i reference the values??
      So you want each row to be stored in it's own hash, right? So if you've got 2 columns per row called "ID" and "value", you'd want to be able to access them like this?:
      my $value = $rows{$ID}{value}
      This sort of breakdown will require you to know all column names and what order they appear in, but that shouldn't be too hard. If you have the column names in an array, for instance, it would look like this:
      my @columns = ("ID","value","some_other_column"); my %rows; while (my @row = $sth5->fetchrow_array) { my %rowHash = (); for (my $i=0; $i < scalar @row; $i++) { $rowHash{$columns[$i]} = $row[$i]; } $rows{$row[0]} = \%rowHash; }
      Hope that helps...

      Hot Pastrami

        Here's another alternative:

        $sth->bind_col(1, \my $key); my %rows; while(my $row = $sth->fetchrow_hashref) { $rows{ $key } = $row; }

        With this version you don't need to maintain a list of columns, and it will automatically make the first column the key in %rows. It's probably more efficient/faster to use $sth->fetchrow_hashref than doing a foreach since $row is made using a hash slice.

        i need to create this hash will this do it??
        each row produces a round number teamname score eg round teamname Score 1 someone 6 2 some1else 7
        where round is the key or maybe a 2D array might be the solution.
        i want to create something like a 2d array structure i've done the following will this work?
        my $sth5 = $dbh->prepare("SELECT round, teamname, score from trees whe +re sport = ‘$sport’ and year = ‘$year’, ORDER BY round DESC") || die +"Can't prepare SQL get data statement"; $sth5->execute || die “can’t execute sql statement”; ##need to get all the data one row at a time from the table. my @row; $rowcount = 0; while (@row = $sth5->fetchrow_array) { $round = $row[0]; $teamname = $row[1];## need to put each row into list_ $Score = + $row[2];## of_lists a variable at a time push (@{list_of_teamdata[$rowcount]}, qw($round $teamname $score); $rowcount++; } This creates a list of structure round teamname score round teamname score round teamname score round teamname score etc i then need to substitute values from the list into the html document my $compsize = $#list2+1; If ($compSize = 7); Print “Content-Type: text/html\n\n”; Print “<html>\n” Print “<head>\n” Print “<title> tree7</title>\n” Print “</head>\n” Print “<body>\n” Print “<table border="1" width="100%">\n” Print “<tr>\n” Print “<td width="9%">&nbsp;</td>\n” Print “<td width="9%">&nbsp;</td>\n” Print “<td width="9%">&nbsp;</td>\n” Print “<td width="9%"><b>$list_of_teamdata [6][1]</b></td>\n” Print “<td width="9%"><b>$list_of_teamdata [6][2] </b></td>\n” is this correct. i realise this is not the best way but i'm very new +to perl and cgi programming.