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 #### for my $rc(@R) { if ($rc->[0] eq "a") { splice @$rc, 0, 0, '#'; print "$rc->[0]\t","$rc->[1]\t","$rc->[2]\t","$rc->[3]\n"; } }