I have a long sql query that I am attempting to put into VBA for Excel. VBA for Excel has a limit to the amount of text that can go on a line and it seems to be about 1000 chars. What I want to do is copy the query to a text file and run it through a perl script and output to the text file with it formatted the way I need it for VBA.
I need Perl to count chars to 1000 then write (” & _) then a line break then (“) then repeat the process till the end of the file. Spaces or type of char do not matter. Any help is greatly appreciated. I will check back frequently to see if anyone needs more information. THANKS!!!
If you have the text already in a string, one way to do this would be:
or, perhaps better:
If you’re reading the input from a file, it’s also possible to do this without reading all the input into memory, by setting the input record separator:
(Some of these methods can sometimes leave a pointless
""on the last line — in fact, the last method will always do that — but I assume that shouldn’t be a problem.)Finally, note that neither of these methods will do anything to escape any special characters (like double quotes) that might appear in the input. If your input might contain such characters, you’ll need to deal with them separately. I’ve marked the points where you could insert code to do that, if you need to.
Edit: I did some Googling, and it looks like the main characters the need to be escaped in double quoted VBA strings are double quotes (which need to be doubled) and newlines (which have no simple encoding, and need to be handled with a kluge like
" & vbCrLf & "). I’ve edited the code above to implement such escaping.I suppose, if one wanted to be extra sure, one could also escape all non-printable and non-ASCII characters with something like:
(Ps. The backslash in
s/\"/""/gis there only to avoid confusing SO’s syntax highlighter.)