title: Perl remove characters from string matrix according to pattern
body: <p>I have multiple strings with the same length stored in a hash data structure. <strong>Example:</strong></p>
  
  <pre><code>$VAR1 = {
            'first' =&gt; 'abcXY',
            'second' =&gt; 'XYXYa',
            'third' =&gt; '*abXZ'
          };
  </code></pre>
  
  <p>From this 'matrix' of characters, I would like to remove the 'columns' which exclusively contain the characters <code>X</code> or <code>Y</code>. In the above example, this would be the fourth character of each string (4th 'column'). 
  The desired result would be:</p>
  
  <pre><code>$VAR1 = {
            'first' =&gt; 'abcY',
            'second' =&gt; 'XYXa',
            'third' =&gt; '*abZ'
          };
  </code></pre>
  
  <p>Following code does this by creating a transpose of the values of my hash structure and then determines which indices to keep:</p>
  
  <pre><code># data structure
  my %h = ('first'=&gt;'abcXY', 'second'=&gt;'XYXYa', 'third'=&gt;'*abXZ' );
  
  # get length of all values in hash
  my $nchar = length $h{(keys(%h))[0]};
  
  # transpose values of hash
  my @transposed = map { my $idx=$_; [map {substr ($_, $idx, 1) } values(%h)]  } 0..$nchar-1; 
  
  # determine indices which I want to keep
  my @indices;
  for my $i (0..$#transposed){
          my @a = @{$transposed[$i]};
  
          # do not keep index if column consists of X and Y
          if ( scalar(grep {/X|Y/} @a) &lt; scalar(@a) ) {
                  push @indices, $i;
          }   
  }
  
  # only keep letters with indices
  for my $k (keys %h){
          my $str = $h{$k};
          my $reduced = join "", map{ substr ($str, $_, 1) } @indices;
          $h{$k} = $reduced;
  }
  </code></pre>
  
  <p>This is a terrible amount of code for such a simple operation. How could I do this more elegantly (preferably not with some matrix library, but with standard perl)?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Here another example: From the following strings, the first and last characters should be removed, because in both strings, the first and last position is either <code>X</code> or <code>Y</code>:</p>
  
  <pre><code>$VAR1 = {
            '1' =&gt; 'Xsome_strX',
            '2' =&gt; 'YsomeXstrY'
          };
  </code></pre>
  
  <p>Desired result:</p>
  
  <pre><code>$VAR1 = {
            '1' =&gt; 'some_str',
            '2' =&gt; 'someXstr'
          };
  </code></pre>