in reply to appending to a particular line in a file

When you read in your first line, it has the form      1\n with a trailing newline. So when you say:      $_ =~ s/$_/$_ 1.0/g; you are saying, "If $_ can be found in the variable $_ (which of course it can), substitute for the entire value, that same value with " 1.0" on the end. Which results in the string:      1\n 1.0 Which is just what you report.

Doing a substitution on the whole string is a bit odd anyway. And you don't need the /g. How about (staying with the level of verbose clarity you are using):

foreach $txtfile (glob("*.TXT")) { print $txtfile; print "\n"; open FILE, "$txtfile"; open NEWFILE, ">$txtfile.tmp"; while (<FILE>){ chomp $_; if ($. == 1){ $_ .= " 1.0"; # i.e. $_ = $_ . " 1.0"; } print NEWFILE $_, "\n"; } close FILE; close NEWFILE; }
Update: A more streamlined version might be:
foreach $txtfile (glob("*.TXT")) { print $txtfile; print "\n"; open FILE, "$txtfile"; open NEWFILE, ">$txtfile.tmp"; while (<FILE>){ $_ =~ s/\n/ 1.0\n/ if $. = 1; print NEWFILE $_; } close FILE; close NEWFILE; }

Replies are listed 'Best First'.
Re: Re: appending to a particular line in a file
by mndoci (Scribe) on Aug 03, 2001 at 02:37 UTC
    Thank you for your reply. I have it working now with the following little change
    if ($. == 1){   chomp($_); #  $_ =~ s/$_/$_ 1.0/g;   $_ .= " 1.0\n"; # i.e. $_ = $_ . " 1.0"; }

    Cheers
    mndoci

    "What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?"-Sherlock Holmes in 'A study in scarlet'