Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Reading Multiple lines

by moritz (Cardinal)
on Oct 15, 2008 at 09:14 UTC ( [id://717157]=note: print w/replies, xml ) Need Help??


in reply to Reading Multiple lines

The problem is that you're reading line by line, and not record by record, and you have no solid detection of when a record is continued on the next line.

I think that Text::CSV has a solution for that, you might need to tweak some of the options though.

Replies are listed 'Best First'.
Re^2: Reading Multiple lines
by mick2020 (Novice) on Oct 15, 2008 at 09:25 UTC
    When I open the file I have the correct pattern
    
    eg Input is.
    "data","data","data","data"
    "data","data with 
    new with new Line" "data"
    "data","data","data","data"
    
    "data","data","data","data"
    "data","data with new with new Line" "data"
    "data","data","data","data"
    
    I'm not sure if I need to use that module.As, at this point in the process I don't process the fields and only process each line
      Oh, I should have read your code more thoroughly.
      <code> "data","data","data","data" "data","data with new with new Line" "data"

      It would be more consistent to put a comma between the two last fields, not just a blank.

      If you do that, you can just feed the lines to Text::CSV. Or reinvent the wheel by crafting a clever regex, but that has been discussed here many times (try Super Search for CSV or "comma separated"), so I won't re-write the wheel.

        sorry I missed that the comma in that example. it should be.
        
        input:
        "data", "data" ,"data", "data","data", "data"
        "data", "data" ,"data", "data
        with new line
        ","data", "data"
        "data", "data" ,"data", "data","data", "data"
        
        output:
        "data", "data" ,"data", "data","data", "data"
        "data", "data" ,"data", "data with new line","data", "data"
        "data", "data" ,"data", "data","data", "data"
        
        All I am doing at this stage is making sure that the line
        ends with a quotation mark. I would prefer not to use the TEXT::CSV module as this will be used on a server
        that I don't have permissions to load modules.
Re^2: Reading Multiple lines
by blazar (Canon) on Oct 18, 2008 at 10:16 UTC
    I think that Text::CSV has a solution for that, you might need to tweak some of the options though.

    I personally believe that if nothing else for completeness one should mention Text::xSV as well. In particular, before posting I checked its docs and it says:

    People usually naively solve this with split. A next step up is to read a line and parse it. Unfortunately this choice of interface (which is made by Text::CSV on CPAN) makes it difficult to handle returns embedded in a field. (Earlier versions of this document claimed impossible. That is false. But the calling code has to supply the logic to add lines until you have a valid row. To the extent that you don't do this consistently, your code will be buggy.) Therefore you it is good for the parsing logic to have access to the whole file. This module solves the problem by creating a CSV object with access to the filehandle, if in parsing it notices that a new line is needed, it can read at will.

    The additional emphasis is mine: what is claimed there means that the module should solve the OP's problem. Apologies both to you and the OP for replying so late...

    --
    If you can't understand the incipit, then please check the IPB Campaign.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 02:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found