in reply to CSV SPLIT

G'day serene_monk,

Welcome to the monastery.

"I am working on csv file , viewing as excel . so i am splitting it by comma for formatting . but the problem is when the data itself has comma in it . is there any way to overcome the effect of this comma"

Yes, there certainly is. Take a look at Text::CSV.

-- Ken

Replies are listed 'Best First'.
Re^2: CSV SPLIT
by serene_monk (Initiate) on Jun 21, 2013 at 05:27 UTC
    Thank you for replying

    hey the thing is , my code wants to work on formatting the structure of the csv data , so I need to split it into words and with help of some specific words I have to do the formatting . I have each column of csv(as seen by excel ) in an array , via split function . this works absolutely fine when there is no user input ,

    I think this cpan , only aims to convert csv to text directly

      As kcott posted, there are almost no circumstances where you should use split to retrieve or store CSV data due to problems similar to those you've encountered. Text::CSV and its close relatives will happily get/put data between a CSV file and an array, and deal with the separators in data properly by quoting them. Once the row is in the array you can process it anyway you like.

      If you post some example data and what you'd like to happen to it, perhaps we can give you a more detailed answer. Don't forget to use <code> tags to show your data in the question properly

      If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)

        hey the cpan gives an error that says " Expected fields to be in an array "

        and the line is

        $status = $csv->print ($io, $colref);

      I concur with the advice space_monk has given you. We get these types of questions every day or two: search in Super Search for Text::CSV or, if you prefer, here's one I prepared earlier :-)

      Some additional information:

      • CPAN (uppercase) stands for Comprehensive Perl Archive Network. It's a searchable repository of Perl modules.
      • cpan (lowercase) is a command line utility for interacting with CPAN. You get this for free: it's installed when you install Perl. When you write "cpan" in your posts, this is what people will think you are referring to.
      • When you wish to refer to a CPAN module, you should do so by name. It's helpful to provide a link to the module in question, as I did in my original reply to you. There's Shortcuts for doing this.
      • Don't reply to yourself as you did with (Re^2) "... csv to text directly" (Re^3) "and i was thinking ..."; just edit your post as explained in "How do I change/delete my post?".
      • In order to get the best possible responses to your questions, follow the guidelines in "How do I post a question effectively?".

      -- Ken

      and i was thinking is it possible to convert comma to any special char while reading and change it back to comma while printing