in reply to Tie::File question

/"$section"/ matches lines that contains a double quote followed by something that the regexp in $section matches followed by another double quote.

If you want to match lines that contain the text in $section, you want
/\Q$section\E/

If you want to match lines that are equal to the text in $section, you want
/^\Q$section\E\z/
or
$_ eq $section

I have no idea what's in $section, so it limits my ability to guess what you are trying to do and the advice I can give.

By the way, I noticed you changed
$_ .= $text;
to
$_ .= $_ .="$newline";
Why did you add quotes?

Replies are listed 'Best First'.
Re^2: Tie::File question
by machvvv (Initiate) on Aug 08, 2008 at 13:48 UTC
    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.

      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.