Thank you for the reply. It's now working a lot better than before. This now will add a line to the section of the file where I want it to, but it's adding $newline 2 lines under the $section string. I've included the section of code that calls this function. At this point I'm trying to track down if I have a problem with newline or returns because I'm mixing linux and windows environments when scripting and testing.
#Open add_lines.csv and add lines where needed
open(ADD, "$addstrings") || die("cannot open file $addstrings");
@addlines=<ADD>;
close(ADD);
foreach $line (@addlines)
{
($section,$newline)=split(/,<>,/,$line);
chomp $section;
chomp $newline;
$newline = "\n".$newline;
&AddLine($settings, $section, $newline);
}
sub AddLine{
my $file = $_[0];
my $section = $_[1];
my $newline = $_[2];
use Tie::File;
tie @array, 'Tie::File', $file or die ("cannot open $file");
print "Looking for section: $section so we can add line: $newline\
+n";
foreach (@array){
if (/\Q$section\E/){
$_ .= $newline;
last;
}
}
untie @array;
I'm certain there is a better way to parse the CSV file but I'm not allowed to add perl modules in our setup.(otherwise I would use a excel parser module) I also had to consider possible string combinations in the values.
|