load all of table2 into a hash, keyed by "id,hid" load all of table3 into a hash, keyed by "id,hid,instance" foreach row (id,hid,instance,etc...) in table1 do something with row data, $table2{"id,hid"} and $table3{"id,hid,instance"}