in reply to Re: Parsing a text file
in thread Parsing a text file

thank you oshalla for your time... In response to your bullet points here are my responses.

1. I have made this change even though I had this part working merely because you are right and ^M's don't always behave the way one would expect.

2. Also added this change even though I know it was not failing to open the csv file as it was printing the contents. Suffice it to say that it is a good coding practice that I usually use. I just whipped up an example script for perlmonks.org to show my problem.

3. Must have the for loop as the ^M's are delineating lines of data. Throwing away anything after a ^M is throwing away data I need.

4. You are right, in this particular example I am not using the passed variable name. It's just a habit

Replies are listed 'Best First'.
Re^3: Parsing a text file
by gone2015 (Deacon) on Jan 14, 2009 at 08:56 UTC

    I realised as I woke up that it could be that the ^M in your code could be actual ^M characters and not the ^M that I had mistaken them for. Now that that's clear, I'd still use an explicit escape sequence e.g. \x0D (or the divinely retro \015).

    I fear I didn't get the point across re the for loop... push takes a LIST of things to push onto the ARRAY. So in push @chunks, split(/\cM/, $_) the entire list returned by split is pushed onto @chunks all in one go.

    The caveat about trailing separators and split can be seen in

    print map("'$_' ", split(/:/, 'a:b:c:::') ), "\n" ; # 'a' 'b' 'c' print map("'$_' ", split(/:/, 'a:b:c:::', -1)), "\n" ; # 'a' 'b' 'c' +'' '' ''
    see split. I fear I confused the issue by addressing two points in one paragraph, for which I will now do penance and hope for forgiveness from the gods of clear English.

    I note that the problem is now fixed. Since the code as posted worked on my machine, I'm particularly curious as to what the problem was.