use MLDBM qw(DB_File Storable); # ... stuff .... foreach $gid (@group_ids) { my %temp_hash; unlink "$DBFilePath/$$.$gid" if -e "$DBFilePath/$$.$gid"; tie %temp_hash, 'MLDBM', "$DBFilePath/$$.$gid", O_RDWR|O_CREAT, 0640 or die "Can't tie temp_array to $DBFilePath/$$.$gid: $!"; $data[$gid] = \%temp_hash; foreach $sample_id (@{$groups[$gid]}) { foreach $element_num ( 0 .. $element_count ) { $element = []; # ... do a bunch of stuff here ... $data[$gid]->{$sample_id}->[$element_num] = $element; } } }