use strict; use warnings; my @R; # Constructing your matrix... while () { chomp; push @R, [split(/ /)]; } for (@R) { if ($_->[0] eq 'a') { unshift @$_, '#'; print join("\t", @$_)."\n"; } } __DATA__ x y z a b c e f g h i j