# create pairs my @tmp = map { $_, $_ } @filenames; # rotate push @tmp, shift @tmp; # create the result my %next = @tmp; my %prev = reverse @tmp;