sub match { my ($ref, $what) = @_; my $idx = 0; while ($idx<@$ref) { if (ref $what->[$idx] eq "SCALAR") { ${ $what->[$idx] } = $ref->[$idx]; } elsif ( (ref $what->[$idx] eq "ARRAY") or (ref $ref->[$idx] eq "ARRAY")) { match ($ref->[$idx], $what->[$idx]); } $idx++; } } my $a = [1, [2, 3], 4]; my $b; match ($a, [1, \$b, 3]); print $b->[1], $/; match ($a, [1, [2, \$b], 3]); print $b, $/;