$flip and ( $board, $heights ) = flip $board, $heights; substr $board, $pos, length $word, $word; #say "new mask is $newmask"; # $newmask has unprintable characters, use something like Data::Dump that will show them. substr $heights, $pos, length $highs, ( $highs & $newmask ) =~ tr/0-4/1-5/r | ( $highs & ~$newmask );