#!/user/bin/env perl use strict; use warnings; use Data::Dumper; my @array = qw(a b c d e f g h i 123); my $recurse_level = 0; sub to_hash { my $hash_ref = shift; my @array = @_; my $array_count = @array; die "Need at least 2 elements in original array" if (0 == $recurse_level && 2 > $array_count); my $index; if (2 < $array_count) { $index = shift @array; $recurse_level++; $hash_ref->{$index} = to_hash({},@array); } else { $index = shift @array; $hash_ref->{$index} = shift @array; } return $hash_ref; } my $hash_ref = to_hash({},@array); print Dumper($hash_ref);