http://www.perlmonks.org?node_id=927285

arivu198314 has asked for the wisdom of the Perl Monks concerning the following question:

I need to match particular number in the matrix formatted text like

$a=' 220021 000200 020222 020222';

From the above matrix i need to find diagonal 1111 or 2222.

I have written the code, but its very big. So can anyone optimize this code

my $arraycount=0; my @charArray=(); my @linear=grep length, split /\n/ +, $a; @lines=@linear; while (length join "", @linear) { $reversed=join("", map { s/(.)//s ? $1 : "" } @linear); push (@charArray, [split //, $reversed]); } my $result; my $findString='1|2'; for(my $rw=0; $rw < (@lines-3); $rw++) { my @line=split //, $lines[$rw]; foreach my $index (grep {$line[$_]=~m/$findString/} 0..$#line) { if (($line[$index].$charArray[$index+1][$rw+1].$charArray[ +$index+2][$rw+2].$charArray[$index+3][$rw+3] eq $line[$index] x 4) or + ($line[$index].$charArray[$index-1][$rw+1].$charArray[$index-2][$rw+ +2].$charArray[$index-3][$rw+3] eq $line[$index] x 4)) { print $line[$index]; } } }