softserve has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: How to create an accurate CSV file
by CountZero (Bishop) on Mar 01, 2010 at 12:11 UTC
    Adding some <code> ... </code> tag around your code and data would make it easier to read. It would also help if you could give an example of how the CSV-file should look with your data (so we can check if any solution we suggest is correct).

    Usually reading and writing CSV-files is best done with Text::CSV, but am I right in assuming that it is rather the parsing of the original data that is bothering you?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: How to create an accurate CSV file
by Utilitarian (Vicar) on Mar 01, 2010 at 12:15 UTC
      A couple of things,
    • using <code> tags makes you code more legible without us having to inspect the source. You had to preview before creation, so how did you fail to notice that it was illegible
    • you can either roll your own parser using something like
      open (INPUT, "<", "input.txt"); while (<INPUT>){ chomp(my $record = $_); print join ",", (split /\t/, $record); }
      or use an existing module such as Text::CSV set sep_char to tab and let the module do the heavy lifting. This has several advantages over rolling your own in most instances, but given the data-set you should get away with the above capture style in this instance.

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: How to create an accurate CSV file
by planetscape (Chancellor) on Mar 01, 2010 at 21:09 UTC
Re: How to create an accurate CSV file
by snopal (Pilgrim) on Mar 01, 2010 at 13:55 UTC

    It seems you need to understand text parsing. Simply stated, your script assumes one space and a dozen spaces are the same. Obviously this is wrong, and you need to account for each column of data in a hard format.

    Whether learning Perl to solve this particular problem is the path to your solution only you can answer. I'm not totally familiar with AWK's full set of features, but I do know there are some fine columnar tools in 'cut'.

Re: How to create an accurate CSV file
by softserve (Initiate) on Mar 01, 2010 at 22:30 UTC
    Thank you all for your help. I should learn Perl or Awk, for that matter, but I find myself needing a quick tool for specific problem that I'll probably never require again. Your suggestions, especially the one on my faulty assumptions of the column width, have been quite helpful. While I don't the solution in hand, you have put me on the right path. I apologize again for the poor formatting and thank you for your prompt and authoritative responses.