Skyler99 has asked for the wisdom of the Perl Monks concerning the following question:

I have worked with this program to parse a text file. It does what it is suppose to do. it replaces (,) commas with (|) pipes. However it gives me a warning message at the begining "Unrecognized escape \d passed through at "Filename.pl" line 5" I tried to debug this program but I haven't gotten any where. here is my code :

I'll appreciate any help. Please write to -->edge99off@hotmail.com

#! perl -w use strict; open(IN, "<c:\doclist.chr") or die "Couldn't open file, $!"; open(OUT, ">c:\doclist.txt") or die "Couldn't open file, $!"; while(<IN>) { chomp; # Remove the newline my ($var1, $var2, $var3, $var4, $var5, $var6, $var7) = split /,/; print OUT $var1,"|",$var2,"|",$var3,"|",$var4,"|",$var5,"|",$var6," +|",$var7,"\n"; } exit; close IN close OUT

update (broquaint): added formatting

Replies are listed 'Best First'.
Re: Parsing a comma delimited file
by broquaint (Abbot) on Feb 13, 2003 at 14:07 UTC
    However it gives me a warning message at the begining
    That would be the filenames in you open calls. You need to use single quotes instead as the backslash is trying to escape the 'd' following it which is not what you want e.g
    open(IN, '<c:\doclist.chr') or die "Couldn't open file, $!"; open(OUT, '>c:\doclist.txt') or die "Couldn't open file, $!";
    Also can I suggest a simplification to your while loop
    print OUT join '|', split /,/ while <IN>;
    Now you're not limited to seven fields (although you can enforce this using the 3rd argument of split) and you've done it all on a single line, hurrah!
    HTH

    _________
    broquaint

      Thanks for the info ... the program works like a charm!
Re: Parsing a comma delimited file
by tachyon (Chancellor) on Feb 13, 2003 at 14:44 UTC

    Of course you can do it in a one liner too

    perl -pi.bak -e 's/,/|/g' <file>

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Parsing a comma delimited file
by busunsl (Vicar) on Feb 13, 2003 at 14:06 UTC
    open(IN, "<c:\doclist.chr") or die "Couldn't open file, $!"; open(OUT, ">c:\doclist.txt") or die "Couldn't open file, $!";
    When specifying pathnames use forward slashes '/' not backslashes '\', even on windows machines.

    Perl will handle this for you.

Re: Parsing a comma delimited file
by bart (Canon) on Feb 13, 2003 at 14:36 UTC
    Either double your backslashes in your string containing the path, or use a forward slash instead. A single backslash in a double-quotish string usually means something other than a literal backslash.

    And if your records aren't that trivial, in that they may be quoted and contain comma's, perhaps you should be using one of the CSV modules instead.

Re: Parsing a comma delimited file
by tcf22 (Priest) on Feb 13, 2003 at 17:19 UTC
    you can either replace the backslash with 2 backslashes in double quoted strings, use a slash instead of a backslash(even on windows), or put your path in single quotes, as long as the path doesn't contain any variables or special characters(\n, \t, etc.)