> > If you really don't care about the output order of the final list,
> > try using a hash with the PID being the key and each element an array
> > reference. Then you just push each name onto the array associated with
> > the PID, as follows (you don't even need the order by clause with this
> > approach):
Thanks for reminding me about hashes! I ended up doing this, which
scales up, allowing me to retrieve pairing data for multiple fanfics
in one SELECT statement:
# Load pairings
my $sth = db::query("
SELECT pairings.fid, pairings.pid, pairings.name
FROM pairings, fanfics
WHERE $where
AND pairings.fid = fanfics.fid
");
while (my ($fid, $pid, $name) = $sth->fetchrow_array) {
push(@{$self->{pairings}{$fid}[$pid]}, $name);
}
Since I'm now using the PID as an array index, I should renumber my
PIDs in the database to start from 0 instead of 1, but that's easy
enough to do. It stores the pairing data for each fanfic as a list of
lists under @{$self->{pairings}{$fid}.
If I then want to print this list of lists for human consumption, I
do:
print join(', ', map(join('/', @{$_}), @{$self->{pairings}}));
Given the previous example of
[['Rei', 'Minako', 'Michiru'],
['Usagi', 'Rei'],
['Ami', 'Usagi'],
['Makoto', 'Minako', 'Haruka']]
it would be printed as:
Rei/Minako/Michiru, Usagi/Rei, Ami/Usagi, Makoto/Minako/Haruka
> Haruka and Michiru won't appreciate being separated.
Hey, I recognize you from the FFML. :)
In case you're interested, this is the webpage that I'm writing the
above code for (and no, there isn't really a Rei/Minako/Michiru
fanfic; I just made that up for the example above):
http://www.shoujoai.com/fanfics/smoon/