Export to CSV - proper format
Brian K. White
brian at aljex.com
Fri Oct 22 13:16:01 PDT 2010
On 10/21/2010 5:20 PM, Fairlight wrote:
> With neither thought nor caution, Brian K. White blurted:
>>
>> In any event the most convenient way I found so far to do that kind of
>> conversion (inserting more characters into the output than were in the
>> input), isn't exactly convenient or efficient, but it's the most
>> optimized I have come up with so far. you make a small gosub that loops
>> through every character in a variable, and use it before every export
>> assignment that might want that kind of translation.
>
> Instead of going through every character in the loop, why not just use
> instr() in the loop, and save yourself a bunch of iterations? Mark the
> place you last found the character, add however many characters you may
> have added, and use that as the place for the next instr().
>
> Far more optimised.
Hmm, I was going to say that's only a win if the real job really is only
to test for a single character and do a single type of fix. Where really
you usually have to examine every character and pass it through a stack
of possible tests and changes. But I eventually arrived at this, which
uses the instr() to do both the finding & jumping position, and the
identifying instead of "if ic eq ...". It ends up being not much larger
in total, and probably not much more cpu per iteration, but with the
benefit of being able to skip many/most iterations.
Something like this but with some +"1" and/or -"1" sprinkled in to fix
off-by-one errors. I didn't even try to get those right.
cln If: ' clean '**************************************
Then: p = "1" ; i = n{"" ; s = len(i{"")
nxtchr If:
Then: ps = s ; oc = ""
If: ' look for a "
Then: pt = instr(i,chr("34"),p)
If: pt ne "0" and pt lt ps
Then: ps = pt ; oc = chr("34") & chr("34")
If: ' look for a \
Then: pt = instr(i,chr("92"),p)
If: pt ne "0" and pt lt ps
Then: ps = pt ; oc = "/"
If: ' look for a CR
Then: pt = instr(i,chr("13"),p)
If: pt ne "0" and pt lt ps
Then: ps = pt ; oc = ""
If:
Then: n = n & mid(i,p,ps-p) & oc ; p = ps
If: p lt s
Then: p = p + "1" ; goto nxtchr
If:
Then return ' cln
The idea is:
for each iteration of the loop,
start by setting the current saved position (ps) (the possible next
position to jump to unless we find something closer) to the end of the
input string, and set the current output character to ""
then have a stack of instr()'s.
For each instr(),
If the current isntr() returned a smaller value than the last one, but
greater than 0, then save this position and set oc to whatever this
characters replacement is.
At the end of the stack of instr()'s, ps is the smallest value of any of
the instr()'s and oc is the matching replacement output for whatever
character that instr() was looking for.
mid out the skipped chars from the last position from the previous
iteration of the loop (p) to the current ps, append that and the current
oc to the output. loop until ps = the length of the input (s)
--
bkw
More information about the Filepro-list
mailing list