Wednesday, December 16, 2009

Re: Rectify some bad csv

On 16 déc, 17:40, "Christian Brabandt" <cbli...@256bit.org> wrote:
> On Wed, December 16, 2009 5:20 pm, epanda wrote:
> > Try only ./g on the second line of this example :
> > DDD_III/CTRL/PT/VERSION;_INSTALL;CSTRING;;;;Commentary
> > DDD_AAA/CTRL/PT/VERSION;;;;;;;;;;;;;
> > DDD_CCCC/CTRL/PT/VERSION;;;;;;;;;;;;;
> > DDD_TTTT/CTRL/PT/VERSION;;;;;;;;;;;;;
>
> > It complete the second line but should not. Causes first item's of the
> > splitted line are not the same.
>
> That contradicts a little bit to your previous use case:
>
> ,----
> | First case
> | ____________
> | foo1;foo2;foo3;;;;;
> | ;foo2;foo3;;;;;
> |
> | Has to become
> | foo1;foo2;foo3;;;;;
> | foo1;foo2;foo3;;;;;
> `----
>
> But here we go:
>
> fu! <SID>CSV() range
>   if line('$') < 2 | return | endif
>   let prevline=split(getline(1), ';', 1)
>   let linecount=2
>   while linecount <= line ('$')
>       let tline=split(getline(linecount), ';', 1)
>       let i=0
>       let status=0
>       while i < (len(tline)<=len(prevline)?len(tline):len(prevline))
>         if empty(tline[i]) && ((tline[0] == prevline[0]) || empty(tline[0]))
>            let tline[i]=prevline[i]
>            let status=1
>         endif
>         let i+=1
>       endw
>       if status
>          call setline(linecount, join(tline, ';'))
>       endif
>
>       let prevline=tline
>       let linecount+=1
>   endw
> endfu
>
> :com! ReorderCSV :call <SID>CSV()
>
> regards,
> Christian
> --
> :wq

It is more complicated, the script should complete empty fields
between semi colonn only if all previous field of the current line
are identical to the same position fields of the previous line.


DDD_III/CTRL/PT/VERSION;_INSTALL;CSTRING;;;;Commentary
DDD_AAA/CTRL/PT/VERSION;;;;;;;;;;;;;
DDD_CCCC/CTRL/PT/VERSION;;;;;;;;;;;;;
DDD_TTTT/CTRL/PT/VERSION;_INSTALL;CSTRING;;;;;;;;;;;
DDD_TTTT/CTRL/PT/VERSION;;;ANYTHINGELSE

should be


DDD_III/CTRL/PT/
VERSION;_INSTALL;CSTRING;;;;Commentary
< nothing to do
DDD_AAA/CTRL/PT/
VERSION;;;;;;;;;;;;; <
nothing to do
DDD_CCCC/CTRL/PT/
VERSION;;;;;;;;;;;;; <
nothing to do
DDD_TTTT/CTRL/PT/
VERSION;_INSTALL;CSTRING;;;;;;;;;;;
< nothing to do
DDD_TTTT/CTRL/PT/
VERSION;_INSTALL;CSTRING;ANYTHINGELSE
< correction to do


--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments: