in reply to File read and strip

I think the problem is in your section of:
for(@array){ ($i)= grep { m/^#/ } $_; push @new, $i; }
What about replacing that with:
foreach my $line (@array) { next if ($line =~ /^#/); push @new, $line; }


Mine might be a bit less compact, but it is much more readable and easier to see what it happening there.

Also it appears to me that when you are writing back to your file, you are putting in 4 extra linebreaks. Is that intentional?

Replies are listed 'Best First'.
Re^2: File read and strip
by Andrew_Levenson (Hermit) on Nov 14, 2006 at 20:00 UTC
    The line breaks are intentional to make the final file more readable when I format it.
    What does using the {parameter?} "my $line" before the (@array) do?
    And, let's see if I have any idea what's going on, the second line skips a line if it starts with a #?

    Thanks!
      The line:
      foreach my $line (@array) {
      assigns the value from the next item in @array to the variable $line. I find this makes it eaiser to see what you are doing, rather than using $_, which I find non-intuitive.

      Yes, you are correct, that next line of code skips anything that starts with a #, so that only lines from your file that are not comments are inserted into the array that you are intending to send to the output file.