One flaw in you code is that you try to close $infile, where $infile is the file name, not the file handle.

And I wonder how you can think that

while (my $line = <CSVFILE>) { $line =~ tr/"\r\n//d;

is ever going to do what you think it is doing. The diamond operator reads lines based on $/, which is most likely a newline, so if the line read contains a newline, it will be the last character of $line in this loop, and tr// is not what you want, but chomp would be.

If your goal however is to delete \r and \n from the CSV fields after the line has been read, this will horribly fail. You should use a CSV parsing module instead, rendering the first loop into something like:

use Text::CSV_XS; my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 }); open my $fh, "<", $infile or die "$infile: $!\n"; while (my $row = $csv->getline ($fh)) { $row->[1] =~ tr/"\r\n"//d; my (undef, @prefinal) = split m/\\/ => $row->[1]; push @final, [ @prefinal, $row->[0], $row->[3] ]; } close $fh;

Text::CSV will work the same, but Text::CSV_XS is much faster.


Enjoy, Have FUN! H.Merijn

In reply to Re: Veriable Length Array/Hash derived from CSV to populate an XML by Tux
in thread Veriable Length Array/Hash derived from CSV to populate an XML by TheBigAmbulance

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.