use Tie::File; my $id_from_form=3; my $move="up"; my $flat_file = 'path/to/flat.file'; tie my @array, 'Tie::File',$flat_file or die $!; my $index = grep{$_->[0] =~ /^$id_from_form\*\*/}@array; if($move eq 'up'){ my $elem = $array[$index]; $array[$index] = $array[$index-1]; $array[$index-1] = $elem; } else{ my $elem = $array[$index]; $array[$index] = $array[$index+1]; $array[$index-1] = $elem; } untie @array;