chomp @file; my @matches = grep { $file[$_] =~ /^\Q$storeline|\E/ } 0..$#file; no_match($storeline), return unless @matches; duped_ids($storeline), return if @matches > 1; my ($number, $image, $name, $text, $used) = split /\|/, $file[$matches[0]]; # do what you do modifying those vars $file[$matches[0]] = join '|', $number, $image, $name, $text, $used; rename '/path/to/info/smileset.txt', '/path/to/info/smileset.bak' or die $!; open FLE, '>', '/path/to/info/smileset.txt' or die $!; print FLE $_, $/ for @file; close FLE or die $!;