Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Re: Re: appending an array to a hash

by Hot Pastrami (Monk)
on Mar 22, 2001 at 03:22 UTC ( [id://66185]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: appending an array to a hash
in thread appending an array to a hash

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

Replies are listed 'Best First'.
(dkubb) Re: (5) appending an array to a hash
by dkubb (Deacon) on Mar 22, 2001 at 09:00 UTC

    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.

      the only examples of hashes i've seen in any of the books seem to demonstrate that the key can only reference one piece of data. whereas in the table i have i need to use the round number to reference the team name and the score for that row. I also return multiple rows from the sql selection step so i need to take each row one at a time and add them to the hash as a row of three pices of data - the first being the key. what does the putting of the "my $row" inside the while step do? what is the syntax to reference the first column in row one. what is the syntax to reference second column in row one. i'm new to perl so i might seem a bit of an amateur.
Re: Re: Re: Re: appending an array to a hash
by Anonymous Monk on Mar 22, 2001 at 16:54 UTC
    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.
Re: Re: Re: Re: appending an array to a hash
by Anonymous Monk on Mar 22, 2001 at 20:13 UTC
    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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-25 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found