foreach (@dirs) { # get filenames into @files # push dir name...? foreach (@files) { # do some sort of magic with push();, # taking into account when you get to the # fifth filename...? } }