#!/usr/bin/env perl use Modern::Perl; use Data::Dump qw(pp); my %cols; my @patch_cols = qw(2 3); while () { my @row = split; push @{$cols{$_}}, $row[$_] for (0..$#row); } for my $col (@patch_cols) { my @fwd = @{$cols{$col}}; my @bwd = reverse @fwd; for my $i (1..$#fwd) { $fwd[$i] = $fwd[$i-1] if $fwd[$i] == 0; $bwd[$i] = $bwd[$i-1] if $bwd[$i] == 0; } @bwd = reverse @bwd; $fwd[$_] == $bwd[$_] and $cols{$col}->[$_] = $fwd[$_] for (0..$#fwd); } say pp(\%cols); say pp(\@patch_cols); __DATA__ 1 100 1 2 101 200 1 2 201 300 1 0 301 400 1 0 401 500 0 2 501 600 0 2 601 700 0 0 701 800 1 1 801 900 1 2 #### { "0" => [1, 101, 201, 301, 401, 501, 601, 701, 801], "1" => [100, 200, 300, 400, 500, 600, 700, 800, 900], "2" => [1, 1, 1, 1, 1, 1, 1, 1, 1], "3" => [2, 2, 2, 2, 2, 2, 0, 1, 2], } [2, 3]