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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.