Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
Think about Loose Coupling
 
PerlMonks  

Re^2: Parsing CSV only returns the second line of the file

by saint_geser (Initiate)
on Sep 01, 2012 at 09:54 UTC ( #991140=note: print w/ replies, xml ) Need Help??


in reply to Re: Parsing CSV only returns the second line of the file
in thread Parsing CSV only returns the second line of the file

Thanks guys for your replies! I'm still quite confused. This is my first perl script and I haven't done any programming since uni.

The reason it wouldn't compile (works fine for me) is because I have perl as a part of proprietary mining software which has their own extension modules installed, e.g. Lava. I don't get any errors or warnings when running it.

The part of code that does the parsing I got from somewhere on the internet and modified for my purpose and I'm not quite sure where the inner while cycle is from. I'm pretty sure it shouldn't be there.

So I've commented out the inner cycle and moved the part where I open TEMP file for editing and insert two lines up the top out of the outer cycle. Now this part of code looks like this:

while ( <OLD> ) { next if ($. == 1); if ($csv->parse($_)) { my @columns = $csv->fields(); # print the parsed body of old file print TEMP "$columns[0], $columns[2], $columns +[1], $columns[3], $columns[0]\n"; #while( <OLD> ) #{ #print TEMP $_; #last if $. % 1; #} } else { my $err = $csv->error_input; Lava::Message("Failed to parse line: $err"); } }

The output that I get now has only the top 2 and bottom 2 lines:

W1582165 ,01-Sep-12,,Dist=Metres 0, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000 0, 0.000, 0.000, 0.000, 0, 0.000, 0.000, 0.000, END

I still can't figure out how to make parsing work properly. I'm actually very confused about this line:

next if ($. == 1);

It says next if line number equals to 2? In the example that I saw it parsed the whole file but here does it just parse the second line?


Comment on Re^2: Parsing CSV only returns the second line of the file
Select or Download Code
Re^3: Parsing CSV only returns the second line of the file
by Athanasius (Prior) on Sep 01, 2012 at 10:19 UTC
    The reason it wouldn't compile (works fine for me) is because I have perl as a part of proprietary mining software which has their own extension modules installed, e.g. Lava. I don't get any errors or warnings when running it.

    Then what you posted isn’t exactly what you’re running, since this line in the original post (fixed in the new post, I see):

    if ($csv->parse($_)

    is missing a closing parenthesis, and the final line:

    END;

    should be __END__.

    It says next if line number equals to 2?

    No, array elements start counting at zero, but line numbers begin at one. So that statement says: Skip the first line of data. (Presumably, the original code expected the first line to be a heading?)

    You are making progress, but it’s difficult to say why your code is failing without a complete, self-contained script. Also, detailing the output you expect/desire would help the monks know what you are trying to achieve. Please see How do I post a question effectively?.

    Update: Minor edit.

    Athanasius <°(((><contra mundum

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991140]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2014-04-18 01:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (460 votes), past polls