in reply to Substitute script for a newbie

Escape the backslash:
$line =~ s/A.TL5F00/+TL\\F5C/g;

Tip #1 from the Basic debugging checklist: diagnostics might give you a little more info (it didn't for me on Perl version 5.12, but it did for 5.14).

Did you really want the print inside the while loop? while ( $line = <FILE> ) { is really the same as while (defined($line = <FILE>)) {. So, outside the while, $line is undefined.

UPDATE: You can simplify the if/else code with this single substitution:

use warnings; use strict; while (my $line = <DATA>) { $line =~ s/^A.TL5([EF])00/+TL\\${1}5C/g; print $line; } __DATA__ A.TL5E001100.TAV A.TL5F001100.TAV

Replies are listed 'Best First'.
Re^2: Substitute script for a newbie
by jspanos (Initiate) on Jul 14, 2015 at 17:14 UTC

    Thank you. One thing I should have mentioned is that I need to replace several different stock option symbols. That is why I have the elsif statement. There will be more.

    I also think that I am making this a lot more complex than it needs to be so I looked at an old script from a Perl lesson and I am going to use that. I will keep you posted.

      I would suppose that you have somewhere a file with a correspondence between old names and new names. If so, please provide a sample of that file so that we can figure out how to use it.

      The idea would be then to read this file and to load the information into a hash (old name = key, new name = value). Then you read your other file (the one you want to process), read the input, lookup into the hash and write the new name (to a new file).

      I can't give you much more details at this point. We would need to see a sample of the file with the translation (old to new name) as well as a sample of your input file.

        The file I have is only the old symbols and it is a list of 590; like below (minus the spaces)

        A.TL5E001100.TAV

        A.TL5F001560.TAV

        A.TL5T001240.TAV

        A.TL5E001230.TAV

        A.TL5E001270.TAV

        So A.TL5E001100.TAV needs to change to +TL\E5C1100.TAV, A.TL5F001560.TAV needs to change to +TL\F5C1560.TAV, etc

        I rewrote the code to be a lot simpler and only did one substitution just so I could see what the output is. Now it runs, but I get no output so I need to read up on matching and substitution string syntax.

        use strict; use warnings; my $input = 'TAV.stock.opt.oldsym.txt'; { unless(open(INPUT,$input)) { die "\nCannot open $input\n"; } <INPUT>; my @lines; while(my $line = <INPUT>) { chomp $line; if ($line =~ m/^A.TL5E00/) { $line =~ s/A.TL5E00/'+TL\E5C'/g; } } close INPUT; foreach my $line(@lines) { print $line,"\n"; } }