my $attr_detail = $dbh->prepare("select pa.masterid,pa.pco_attribute_id,pa.pco_value from attributes_short pa, pageids p, prodpg pg where p.page_id=pg.page_id and pg.masterid=pa.masterid;"); my (%details, $m_id, $attr_id); $attr_detail->execute(); while (my $r = $attr_detail->fetchrow_hashref()) { $m_id = $r->{'masterid'}; $attr_id = $r->{'pco_attribute_id'}; # $details{$m_id}{pco_attribute_id} = $r->{'pco_attribute_id'}; $details{$m_id,$attr_id}{pco_value} = $r->{'pco_value'}; } open (DETAILS,">>attr_detail.tsv"); foreach $m_id (keys %details) { print DETAILS "$m_id\t$attr_id\t$details{$m_id,$attr_id}\n"; } close DETAILS;