On Sat, 24 Dec 2005, Brian Bernard wrote: > Is there any way to auto-size cells when you are generating a CSV file > using Perl (without using any additional modules)?
Do you understand what CSV is? It's just a plain text representation of tabular data, with rows separated by newlines and columns separated by commas. For example: Album,Artist,Year Abbey Road,The Beatles,1969 Elephant,The White Stripes,2003 That's it. Save those three lines as, say, albums.csv, and it would be a perfectly good CSV file exactly as is. CSV is the quintessential quick & dirty file format. It has no concept of formatting the data in any way -- field size, colors, fonts, etc -- because it's just plain ASCII text with some arbitrary characters thrown in as impromptu field delimiters. It's not even very good at this, because if the text you want to store happens to have that delimiter character, then the columns in that row go out of whack. Album,Artist,Year The Dropper,Medeski, Martin, & Wood,1996 Oops. Too many columns. Now you have to start making decisions. You have two or three choices: come up with some kind of escape character so that the delimiter only counts when it hasn't been escaped -- Album,Artist,Year The Dropper,Medeski\,\ Martin\,\ \&\ Wood,1996 -- or use a different character to separate fields in the text -- Album|Artist|Year The Dropper|Medeski, Martin, & Wood|1996 -- or perhaps wrap columns in additional quotation characters -- "Album","Artist","Year" "The Dropper","Medeski, Martin, & Wood","1996" -- but either way, you just sacrificed a lot of the simplicity of CSV, because now anyone trying to parse the data has to figure out which of these arbitrary adaptations to the format you had to make in order to keep the columns clean in the data. SO. Let's back up a little. You keep asking these questions about how to work with CSV, but you're clearly trying to use it in ways that it just isn't good at. If you want the things you're asking for -- formatted cells, custom column widths, predictable field boundaries, etc -- then maybe CSV isn't a suitable task for what you need. As was suggested the last time this came up, an Excel spreadsheet file is a popular and robust way to do the things you're asking for, but the minor tradeoff is that, unless you want to directly work on the Excel binary format by hand -- hint, you do not want to do this -- then you have to use a CPAN module like Spreadsheet::WriteExcel. This really isn't very hard to do, and would make your task much easier. Alternatively, you may wish to format your data as XML, which would be good for representing the data, but less so for representing the format unless you take the additional step of adding XSL or whatever to describe how to display the XML data. <album>Abbey Road</album><artist>The Beatles</artist><year>1969</year> <album>Elephant</album><artist>The White Stripes</artist><year>2003</year> <album>The Dropper</album><artist>Medeski, Martin, & Wood</artist><year>1996</year> Or to take most of the format issue out of the equation altogether, you could just use an HTML table: the data wouldn't be as well represented as it would with XML, but then that's also a problem with CSV, so you're no worse off, but now you can use HTML formatting or CSS to decorate the data in just about any way you would care to try. <table> <tr><td>Album</td><td>Artist</td><td>Year</td></tr> <tr><td>Abbey Road</td><td>The Beatles</td><td>1969</td></tr> <tr><td>Elephant</td><td>The White Stripes</td><td>2003</td></tr> <tr><td>The Dropper</td><td>Medeski, Martin, & Wood</td><td>1996</td></tr> </table> The nice thing about XML or HTML here is that, while the formats are complex enough to support a rich library of CPAN modules for generating them -- and you're strongly recommended to look at some of these -- the formats are still simple enough that you can realistically produce the data with straight core Perl and no external CPAN dependencies. So if that's really a stumbling block for you, this is a way around it. In any event, it seems like you need to reassess what you're trying to do here. The questions you're asking don't make sense at all for working with pure CSV data, which implies that you're either trying to do more than is feasible with data files you're getting from somewhere else, or you're trying to provide data to somewhere else in a format that can't do all the things you need it to do. Either way, considering a different way to represent your data would probably make your task much simpler. -- Chris Devers -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>