in reply to Re: Tie::File question
in thread Tie::File question

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.

Replies are listed 'Best First'.
Re^3: Tie::File question
by ikegami (Patriarch) on Aug 09, 2008 at 01:44 UTC

    I see a lot of scoping errors (use "use strict" to reveal them). But that's not what you're asking about.

    Actually, I don't know what you're asking about since you said "This now will add a line to the section of the file where I want it to".

    By the way, I really don't see the point of using Tie::File here.