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

Data --> BAD Format: "1,123,342,342, BAD INFO ,342.34,16" GOOD Format: "1,123,342,342,GOOD INFO,342.34,16" In my DATAFILE I am parsing, it seperates each record by \n\n. Within that, each column of Information is seperated by a comma. Everything is parsed perfectly except for a few records which contain a return (CR) and does havoc with the entire output.
  • Comment on How do I strip \r (CR) globally from an array?

Replies are listed 'Best First'.
Re: How do I strip \r (CR) globally from an array?
by chromatic (Archbishop) on Jan 21, 2001 at 10:13 UTC
    To remove a character from each record in an array, you can use a loop:

    tr/\r//d for (@lines);

    I'd probably use a chomp while reading lines in, though, depending on your OS. If that's not possible, I'd do the transliteration on each line while reading them in.

Re: How do I strip \r (CR) globally from an array?
by AF_INET (Initiate) on Jan 21, 2001 at 09:39 UTC
    <sigh> I forgot to add <BR>'s... sorry guys. Data -->
    BAD Format:
    "1,123,342,342,


    BAD INFO


    ,342.34,16"


    GOOD Format:
    "1,123,342,342,GOOD INFO,342.34,16"


    In my DATAFILE I am parsing, it seperates each record by
    \n\n. Within that, each column of Information is
    seperated by a comma. Everything is parsed perfectly
    except for a few records which contain a return (CR)
    and does havoc with the entire output.
Re: How do I strip \r (CR) globally from an array?
by blueAdept (Beadle) on Jan 22, 2001 at 13:24 UTC
    Either of these should work.
    map { $_ =~ s/\r//g } @array; or foreach $item (@array) { $item =~ s/\r//g; }
    hope that helps.. It'd probably be best to filter them out on a per line basis when you're creating the datafile(if you have control over it), as opposed to when you're reading it in.