Loading ...
Sorry, an error occurred while loading the content.
 

RE: Vim Tips wiki celebrates with a CSV tip

Expand Messages
  • John Beckett
    ... Very sweet, thanks. The suggestion is on the talk page if anyone feels inspired to check it:
    Message 1 of 25 , Aug 1 6:48 PM
      A. S. Budden wrote:
      > Perhaps adding a converter and changing the comparator
      > as follows would be a good idea?
      > ...

      Very sweet, thanks. The suggestion is on the talk page if anyone feels inspired to
      check it:
      http://vim.wikia.com/wiki/Talk:Working_with_CSV_files#Sorting_by_column

      BTW (for anyone not used to our wiki), in general we do NOT use the talk page
      (instead, add comments to the "Comments" section at the bottom of the tip). So far,
      the CSV tip is our only exception where, due to the phenomenal amount of work done,
      we have a talk page.

      John


      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • nh_wzg
      ... After the patch csv.vim work for the sample above correctly, but still work wrong with my sample below: ========START==========
      Message 2 of 25 , Oct 8, 2008
        A. S. Budden wrote:
        >
        > Clearly I was (I'm sure there's something wrong with answering my own
        > email): the comparison is purely a text comparison, so the sort is
        > struggling with numbers. Perhaps adding a converter and changing the
        > comparator as follows would be a good idea?
        >
        > " If the entry looks like a hex string, a number or
        > " a float, convert to a number representation
        > function! s:ChangeToNumberIfApplicable(entry)
        > if match(a:entry, '^0x\x\+$') != -1
        > let result = str2nr(a:entry, 16)
        > elseif match(a:entry, '^[+-]\?\d\+\.\d\+$') != -1
        > let result = str2float(a:entry)
        > elseif match(a:entry, '^[+-]\?\d\+$') != -1
        > let result = str2nr(a:entry)
        > else
        > let result = a:entry
        > endif
        > return result
        > endfunc
        >
        >
        > " Compare two lines based on the highlighted column.
        > function! s:CompareLines(line1, line2)
        > let col1 = matchstr(a:line1, s:GetExpr(b:csv_column_sort))
        > let col2 = matchstr(a:line2, s:GetExpr(b:csv_column_sort))
        > let comp1 = s:ChangeToNumberIfApplicable(col1)
        > let comp2 = s:ChangeToNumberIfApplicable(col2)
        > return comp1 > comp2
        > endfunction
        >
        > I'm sure all of that could be written better, but it seems to work for me.
        >
        > Al
        >

        After the patch csv.vim work for the sample above correctly, but still work
        wrong with my sample below:
        ========START==========
        md51,fullpath_filename,path,fullname1,fullname2,md52,name2,ext2,size2,ts2,tu,tc,
        44029A87AD3484711FACB7945D242A95,X:\I386\0401.CS_,X:\I386\,0401.CS_,0401.csv,61A84549F0F229D536FAE510512CD17E,0401,.csv,64746,99-10-05
        14:43 ,,,
        FFFBC075AA7E1756E35B5AF2E8BE7DEB,X:\I386\DISK.SY_,X:\I386\,DISK.SY_,disk.sys,322B9A3774DBF119F6635A476B0EB058,disk,.sys,30768,03-06-19
        12:05 ,,,
        FFF68CFBFA883D81731A6B344A25B782,X:\I386\CGA80852.FO_,X:\I386\,CGA80852.FO_,cga80852.fon,85A2B00160B440C990405CBF5FBE67D0,cga80852,.fon,5200,99-02-01
        20:00 ,,,
        FFF5CBFC71FB5C80AAD9F834B3FB5062,X:\I386\IMESHARE.DL_,X:\I386\,IMESHARE.DL_,imeshare.dll,00E1A17FB6843C6EC0537397C1EABA2D,imeshare,.dll,32768,99-12-10
        09:19 ,,,
        FF9BDB807FC99B2B6156742AC2F9F2AB,X:\I386\COADMIN.DL_,X:\I386\,COADMIN.DL_,coadmin.dll,56175F4BFCB31006B250C0CF91D3F090,coadmin,.dll,40720,03-06-19
        12:05 ,,,
        FFE31887684AED0F7B124378D942D3AC,X:\I386\CB32.EX_,X:\I386\,CB32.EX_,cb32.exe,B24E88B56A26CA2E649EB21FC04F6D4E,cb32,.exe,4880,99-12-10
        09:19 ,,,
        FF5EA7161307D7170C39D31629239CDF,X:\I386\BOPOMOFO.UC_,X:\I386\,BOPOMOFO.UC_,bopomofo.UCE,405E1EF8E3C88E9BCD2853382BB12430,bopomofo,.UCE,22984,98-06-28
        16:31 ,,,
        FF762A792EFCB11C917A2682352F71D1,X:\I386\HPCRDP.CA_,X:\I386\,HPCRDP.CA_,hpcrdp.cat,DA8532F518611521F82CE80845FA0F49,hpcrdp,.cat,11863,00-01-06
        15:04 ,,,
        FF04598C86B5DF9B78F122ABCB266790,X:\I386\AGT0419.HL_,X:\I386\,AGT0419.HL_,Agt0419.hlp,7BF84005A0547DC9112A51E885E65215,Agt0419,.hlp,8799,99-07-23
        16:53 ,,,
        FF39047446FC1C46FC0C1FA4071063CB,X:\I386\DG18D50.IC_,X:\I386\,DG18D50.IC_,Dg18d50.icm,969A3A6AE9B8824A20CD73DD20FB8136,Dg18d50,.icm,614,98-12-22
        15:37 ,,,
        10D0BA441255894A51C8B3308EB11638,X:\I386\0410.CS_,X:\I386\,0410.CS_,0410.csv,E0480BFD4042E8CACC604F0C995AE8AF,0410,.csv,66190,99-10-05
        14:43 ,,,
        FF36362448658F15D2B6E3B593CA1B3F,X:\I386\BUSY_I.CU_,X:\I386\,BUSY_I.CU_,busy_i.cur,1F713B29884F54956498C73CDCA0705F,busy_i,.cur,326,97-07-11
        20:59 ,,,
        FECC01645D83F150EBE2924887E30B91,X:\I386\DMUTIL.DL_,X:\I386\,DMUTIL.DL_,dmutil.dll,2000EFE7D4339C3B7F56A973B99BD38F,dmutil,.dll,43280,03-06-19
        12:05 ,,,
        FF6223C000741845CE8CD0C6336021D1,X:\I386\D3DXOF.DL_,X:\I386\,D3DXOF.DL_,d3dxof.dll,65553FE4E819865DD3742552DD676EB8,d3dxof,.dll,49424,99-12-10
        09:19 ,,,
        45158FF10F952BCFC590ECB2C80D487E,X:\I386\0414.CS_,X:\I386\,0414.CS_,0414.csv,870C30FC4F7DB033AC9FDECC1AC9EECF,0414,.csv,65390,99-10-05
        14:43 ,,,
        FF32B826D2F3C5E41DCAF8B272AB0BF3,X:\I386\INETSRV.MI_,X:\I386\,INETSRV.MI_,inetsrv.mib,8FDA485946897BFD248D5452628CC231,inetsrv,.mib,698,99-01-14
        18:47 ,,,
        3ECA13B43EF48F80F0179856A73A3BA6,X:\I386\0416.CS_,X:\I386\,0416.CS_,0416.csv,F1FED2848449B78B0EA00A578B7CA3A2,0416,.csv,66784,99-10-05
        14:43 ,,,
        59D691289820021216E6CDCAD74743D4,X:\I386\0419.CS_,X:\I386\,0419.CS_,0419.csv,4E3BB73463D89BA0007DE92C2F81209F,0419,.csv,66080,99-10-05
        14:43 ,,,
        FEC5273EB05873C93DA9BE16A94AEE94,X:\I386\AGT0401.DL_,X:\I386\,AGT0401.DL_,Agt0401.dll,D0615C180965DA0B91AC24F3C33B1D45,Agt0401,.dll,19456,99-12-10
        09:14 ,,,
        FE82CCD0D5BD8F6FDE7D48252C25F271,X:\I386\CATSRVUT.DL_,X:\I386\,CATSRVUT.DL_,catsrvut.dll,E1972901C273AB8C0ACE28D9F50DD125,catsrvut,.dll,591120,03-06-19
        12:05 ,,,
        FE97D798B2BC6CB4306B21E078AA6214,X:\I386\C_437.NL_,X:\I386\,C_437.NL_,c_437.nls,0BD539284D746E022BDA27C1F85A525A,c_437,.nls,66594,97-12-12
        17:12 ,,,
        FE0399122F5DB5158E28AF73A1ED94A1,X:\I386\CDOSYS.DL_,X:\I386\,CDOSYS.DL_,cdosys.dll,61FA41471EFBCD89BABA57201205FF4C,cdosys,.dll,2531088,03-06-19
        12:05 ,,,
        FE385CA3249BF6D30C031DEACE045AD3,X:\I386\DBMSRPCN.DL_,X:\I386\,DBMSRPCN.DL_,DBmsRPCn.dll,D74F057E5BF61CE333877D8B1A71DE2C,DBmsRPCn,.dll,28944,03-06-19
        12:05 ,,,
        ========END==========

        How can I deal with it?
        --
        View this message in context: http://www.nabble.com/Vim-Tips-wiki-celebrates-with-a-CSV-tip-tp18768619p19873579.html
        Sent from the Vim - General mailing list archive at Nabble.com.


        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • John Beckett
        ... Can t you provide a simpler example? You should be able to make, say, a four-line example where each line is well under 70 characters. Then tell us exactly
        Message 3 of 25 , Oct 8, 2008
          nh_wzg wrote:
          > After the patch csv.vim work for the sample above correctly,
          > but still work wrong with my sample below:

          Can't you provide a simpler example? You should be able to make, say, a four-line
          example where each line is well under 70 characters.

          Then tell us exactly what command fails. Is it ":Sort 1" which attempts to sort the
          lines based on the value in column 1??

          For other readers, the csv.vim mentioned is from the following, incorporating the
          Suggestion by Al on the talk page:
          http://vim.wikia.com/wiki/Working_with_CSV_files

          John


          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        • Brian L. Matthews
          ... Cool. I don t work with csv s a lot, but when I do that will be really useful. I saved the script to csv.vim, but I had to put it in ~/.vim/plugin, not
          Message 4 of 25 , Oct 8, 2008
            John Beckett wrote:
            > For other readers, the csv.vim mentioned is from the following,
            > incorporating the
            > Suggestion by Al on the talk page:
            > http://vim.wikia.com/wiki/Working_with_CSV_files

            Cool. I don't work with csv's a lot, but when I do that will be really
            useful. I saved the script to csv.vim, but I had to put it in
            ~/.vim/plugin, not ~/.vim/ftplugin as the page says. I'm using vim
            7.2.22 on Mac OS X Tiger built from cvs with ./configure --disable-gui
            --enable-multibyte.

            Thanks,
            Brian

            --~--~---------~--~----~------------~-------~--~----~
            You received this message from the "vim_use" maillist.
            For more information, visit http://www.vim.org/maillist.php
            -~----------~----~----~----~------~----~------~--~---
          • John Beckett
            ... It really should work by putting csv.vim in the ftplugin directory. You need also to enable file type detection as vaguely outlined in the tip. If you have
            Message 5 of 25 , Oct 8, 2008
              Brian L. Matthews wrote:
              >> http://vim.wikia.com/wiki/Working_with_CSV_files
              >
              > Cool. I don't work with csv's a lot, but when I do that will
              > be really useful. I saved the script to csv.vim, but I had to
              > put it in ~/.vim/plugin, not ~/.vim/ftplugin as the page
              > says. I'm using vim
              > 7.2.22 on Mac OS X Tiger built from cvs with ./configure
              > --disable-gui --enable-multibyte.

              It really should work by putting csv.vim in the ftplugin directory. You need also to
              enable file type detection as vaguely outlined in the tip.

              If you have ftplugin/csv.vim but no detection, you should still be able to enter the
              following when you are editing a CSV file:

              :set ft=csv

              I hope that works.

              John


              --~--~---------~--~----~------------~-------~--~----~
              You received this message from the "vim_use" maillist.
              For more information, visit http://www.vim.org/maillist.php
              -~----------~----~----~----~------~----~------~--~---
            • nh_wzg
              ... Thanks for your reply.A simpler example: =====BEGIN===== md51,fullname1,fullname2 44029A87AD3484711FACB7945D242A95,0401.CS_,0401.csv
              Message 6 of 25 , Oct 8, 2008
                John Beckett-2 wrote:
                >
                >
                > Can't you provide a simpler example? You should be able to make, say, a
                > four-line
                > example where each line is well under 70 characters.
                >
                > Then tell us exactly what command fails. Is it ":Sort 1" which attempts to
                > sort the
                > lines based on the value in column 1??
                >
                >
                Thanks for your reply.A simpler example:
                =====BEGIN=====
                md51,fullname1,fullname2
                44029A87AD3484711FACB7945D242A95,0401.CS_,0401.csv
                FFFBC075AA7E1756E35B5AF2E8BE7DEB,DISK.SY_,disk.sys
                FFF68CFBFA883D81731A6B344A25B782,CGA80852.FO_,cga80852.fon
                FFF5CBFC71FB5C80AAD9F834B3FB5062,IMESHARE.DL_,imeshare.dll
                FF9BDB807FC99B2B6156742AC2F9F2AB,COADMIN.DL_,coadmin.dll
                FFE31887684AED0F7B124378D942D3AC,CB32.EX_,cb32.exe
                FF5EA7161307D7170C39D31629239CDF,BOPOMOFO.UC_,bopomofo.UCE
                FF762A792EFCB11C917A2682352F71D1,HPCRDP.CA_,hpcrdp.cat
                FF04598C86B5DF9B78F122ABCB266790,AGT0419.HL_,Agt0419.hlp
                FF39047446FC1C46FC0C1FA4071063CB,DG18D50.IC_,Dg18d50.icm
                10D0BA441255894A51C8B3308EB11638,0410.CS_,0410.csv
                FF36362448658F15D2B6E3B593CA1B3F,BUSY_I.CU_,busy_i.cur
                FECC01645D83F150EBE2924887E30B91,DMUTIL.DL_,dmutil.dll
                FF6223C000741845CE8CD0C6336021D1,D3DXOF.DL_,d3dxof.dll
                45158FF10F952BCFC590ECB2C80D487E,0414.CS_,0414.csv
                FF32B826D2F3C5E41DCAF8B272AB0BF3,INETSRV.MI_,inetsrv.mib
                3ECA13B43EF48F80F0179856A73A3BA6,0416.CS_,0416.csv
                59D691289820021216E6CDCAD74743D4,0419.CS_,0419.csv
                FEC5273EB05873C93DA9BE16A94AEE94,AGT0401.DL_,Agt0401.dll
                FE82CCD0D5BD8F6FDE7D48252C25F271,CATSRVUT.DL_,catsrvut.dll
                FE97D798B2BC6CB4306B21E078AA6214,C_437.NL_,c_437.nls
                FE0399122F5DB5158E28AF73A1ED94A1,CDOSYS.DL_,cdosys.dll
                FE385CA3249BF6D30C031DEACE045AD3,DBMSRPCN.DL_,DBmsRPCn.dll
                =====END=====
                After the ":Sort 2",the result is:
                ====RESULT BEGIN=====
                md51,fullname1,fullname2
                44029A87AD3484711FACB7945D242A95,0401.CS_,0401.csv
                FEC5273EB05873C93DA9BE16A94AEE94,AGT0401.DL_,Agt0401.dll
                59D691289820021216E6CDCAD74743D4,0419.CS_,0419.csv
                3ECA13B43EF48F80F0179856A73A3BA6,0416.CS_,0416.csv
                45158FF10F952BCFC590ECB2C80D487E,0414.CS_,0414.csv
                FF36362448658F15D2B6E3B593CA1B3F,BUSY_I.CU_,busy_i.cur
                FF5EA7161307D7170C39D31629239CDF,BOPOMOFO.UC_,bopomofo.UCE
                10D0BA441255894A51C8B3308EB11638,0410.CS_,0410.csv
                FF04598C86B5DF9B78F122ABCB266790,AGT0419.HL_,Agt0419.hlp
                FE385CA3249BF6D30C031DEACE045AD3,DBMSRPCN.DL_,DBmsRPCn.dll
                FE0399122F5DB5158E28AF73A1ED94A1,CDOSYS.DL_,cdosys.dll
                FFE31887684AED0F7B124378D942D3AC,CB32.EX_,cb32.exe
                FE97D798B2BC6CB4306B21E078AA6214,C_437.NL_,c_437.nls
                FF6223C000741845CE8CD0C6336021D1,D3DXOF.DL_,d3dxof.dll
                FF9BDB807FC99B2B6156742AC2F9F2AB,COADMIN.DL_,coadmin.dll
                FE82CCD0D5BD8F6FDE7D48252C25F271,CATSRVUT.DL_,catsrvut.dll
                FF39047446FC1C46FC0C1FA4071063CB,DG18D50.IC_,Dg18d50.icm
                FFF68CFBFA883D81731A6B344A25B782,CGA80852.FO_,cga80852.fon
                FFFBC075AA7E1756E35B5AF2E8BE7DEB,DISK.SY_,disk.sys
                FECC01645D83F150EBE2924887E30B91,DMUTIL.DL_,dmutil.dll
                FF762A792EFCB11C917A2682352F71D1,HPCRDP.CA_,hpcrdp.cat
                FFF5CBFC71FB5C80AAD9F834B3FB5062,IMESHARE.DL_,imeshare.dll
                FF32B826D2F3C5E41DCAF8B272AB0BF3,INETSRV.MI_,inetsrv.mib
                ====RESULT END=====
                The sorted result of field 2 is wrong.But when I use about 4-lines example,
                the sorted result of field 2 is correct.
                It seems the field sort operation have some limits I can not figure out.
                --
                View this message in context: http://www.nabble.com/Vim-Tips-wiki-celebrates-with-a-CSV-tip-tp18768619p19890956.html
                Sent from the Vim - General mailing list archive at Nabble.com.


                --~--~---------~--~----~------------~-------~--~----~
                You received this message from the "vim_use" maillist.
                For more information, visit http://www.vim.org/maillist.php
                -~----------~----~----~----~------~----~------~--~---
              • John Beckett
                Can anyone point out the bug in the code below? (This encapsulates the sorting problem of the CSV tip reported by nh_wzg). Steps: - Copy below to a file, say
                Message 7 of 25 , Oct 8, 2008
                  Can anyone point out the bug in the code below? (This encapsulates the sorting
                  problem of the CSV tip reported by nh_wzg).

                  Steps:
                  - Copy below to a file, say test.txt.
                  - Yank the second para (copy the code).
                  - Type :@" and press Enter to execute the code.
                  - Press F1 to call MySort() to sort the first para.

                  Problems:
                  - Does not sort lines into any recognisable order.
                  - Changes every time you press F1 (sort is not stable).

                  Is this due to MyCompare sometimes comparing numbers and strings? Any fix?

                  ---start test.txt---
                  0100
                  0012
                  0009
                  0123
                  BAAA
                  ACCC
                  ABCD
                  BDAA
                  BAAA
                  ACCC
                  ABCD
                  BDAA
                  0100
                  0012
                  0009
                  0123

                  " Sort lines in first paragraph.
                  map <F1> :call MySort()<CR>
                  function! MySort()
                  normal! gg}k
                  call setline(1, sort(getline(1, '.'), function('MyCompare')))
                  endfunction
                  function! MyCompare(line1, line2)
                  return a:line1 > a:line2
                  endfunction
                  ---end---

                  John


                  --~--~---------~--~----~------------~-------~--~----~
                  You received this message from the "vim_use" maillist.
                  For more information, visit http://www.vim.org/maillist.php
                  -~----------~----~----~----~------~----~------~--~---
                • John Beckett
                  ... OMG I ve been lost in the complexities of the CSV tip, and when I admired my post the problem was obvious (also, sorry about the weird paste duplication in
                  Message 8 of 25 , Oct 8, 2008
                    John Beckett wrote:
                    > Is this due to MyCompare sometimes comparing numbers and
                    > strings? Any fix?
                    > ...
                    > function! MyCompare(line1, line2)
                    > return a:line1 > a:line2
                    > endfunction

                    OMG I've been lost in the complexities of the CSV tip, and when I admired my post
                    the problem was obvious (also, sorry about the weird paste duplication in my last
                    post).

                    Naturally a sort function has to do more than check if arg1 > arg2! It needs to
                    distinguish between > and < and ==.

                    Sorry about the noise. I'll play with this some more tomorrow.

                    John


                    --~--~---------~--~----~------------~-------~--~----~
                    You received this message from the "vim_use" maillist.
                    For more information, visit http://www.vim.org/maillist.php
                    -~----------~----~----~----~------~----~------~--~---
                  • Tony Mechelynck
                    ... The compare function should return zero for equal, positive for disordered, negative for ordered. You re returning respectively 0, 1, 0. Try this: function
                    Message 9 of 25 , Oct 8, 2008
                      On 09/10/08 06:19, John Beckett wrote:
                      > Can anyone point out the bug in the code below? (This encapsulates the sorting
                      > problem of the CSV tip reported by nh_wzg).
                      >
                      > Steps:
                      > - Copy below to a file, say test.txt.
                      > - Yank the second para (copy the code).
                      > - Type :@" and press Enter to execute the code.
                      > - Press F1 to call MySort() to sort the first para.
                      >
                      > Problems:
                      > - Does not sort lines into any recognisable order.
                      > - Changes every time you press F1 (sort is not stable).
                      >
                      > Is this due to MyCompare sometimes comparing numbers and strings? Any fix?
                      >
                      > ---start test.txt---
                      > 0100
                      > 0012
                      > 0009
                      > 0123
                      > BAAA
                      > ACCC
                      > ABCD
                      > BDAA
                      > BAAA
                      > ACCC
                      > ABCD
                      > BDAA
                      > 0100
                      > 0012
                      > 0009
                      > 0123
                      >
                      > " Sort lines in first paragraph.
                      > map<F1> :call MySort()<CR>
                      > function! MySort()
                      > normal! gg}k
                      > call setline(1, sort(getline(1, '.'), function('MyCompare')))
                      > endfunction
                      > function! MyCompare(line1, line2)
                      > return a:line1> a:line2
                      > endfunction
                      > ---end---
                      >
                      > John

                      The compare function should return zero for equal, positive for
                      disordered, negative for ordered. You're returning respectively 0, 1, 0.
                      Try this:

                      function MyCompare(line1,line2)
                      return (a:line1 > a:line2) - (a:line1 < a:line2)
                      endfunction


                      Best regards,
                      Tony.
                      --
                      hundred-and-one symptoms of being an internet addict:
                      205. You're constantly yelling at your spouse, family, roommate, whatever,
                      for using the phone for stupid things...like talking.

                      --~--~---------~--~----~------------~-------~--~----~
                      You received this message from the "vim_use" maillist.
                      For more information, visit http://www.vim.org/maillist.php
                      -~----------~----~----~----~------~----~------~--~---
                    • grandpa
                      hi, everyone There was a existed commander named sort, I wonder why don t you use it? ... To sort test.txt is quite easy, right? ... 0100 0012 0009 0123 BAAA
                      Message 10 of 25 , Oct 8, 2008
                        hi, everyone

                        There was a existed commander named sort, I wonder why don't you
                        use it?

                        :help sort

                        To sort test.txt is quite easy, right?

                        ---start test.txt---
                        0100
                        0012
                        0009
                        0123
                        BAAA
                        ACCC
                        ABCD
                        BDAA
                        BAAA
                        ACCC
                        ABCD
                        BDAA
                        0100
                        0012
                        0009
                        0123




                        --~--~---------~--~----~------------~-------~--~----~
                        You received this message from the "vim_use" maillist.
                        For more information, visit http://www.vim.org/maillist.php
                        -~----------~----~----~----~------~----~------~--~---
                      • fritzophrenic
                        ... The test.txt used in these examples is a intentionally over-simplified example to allow easier debugging. The original task to be accomplished in this
                        Message 11 of 25 , Oct 9, 2008
                          On Oct 9, 12:24 am, grandpa <lxd...@...> wrote:
                          > hi, everyone
                          >
                          >    There was a existed commander named sort,  I wonder why don't you
                          > use it?
                          >
                          > :help sort
                          >
                          > To sort test.txt is quite easy,  right?
                          >
                          > ---start test.txt---
                          > 0100
                          > 0012
                          > 0009
                          > 0123
                          > BAAA
                          > ACCC
                          > ABCD
                          > BDAA
                          > BAAA
                          > ACCC
                          > ABCD
                          > BDAA
                          > 0100
                          > 0012
                          > 0009
                          > 0123

                          The test.txt used in these examples is a intentionally over-simplified
                          example to allow easier debugging.

                          The original task to be accomplished in this thread was sorting on a
                          given column in a CSV file, with multiple other columns that should
                          not affect the sort.
                          --~--~---------~--~----~------------~-------~--~----~
                          You received this message from the "vim_use" maillist.
                          For more information, visit http://www.vim.org/maillist.php
                          -~----------~----~----~----~------~----~------~--~---
                        • John Beckett
                          ... Thanks for the bug report and sample. As discussed yesterday, the problem was a stupid oversight in the compare function. While contemplating the fix, I
                          Message 12 of 25 , Oct 9, 2008
                            nh_wzg wrote:
                            > After the patch csv.vim work for the sample above correctly,
                            > but still work wrong with my sample below:

                            Thanks for the bug report and sample. As discussed yesterday, the problem was a
                            stupid oversight in the compare function. While contemplating the fix, I have
                            decided to do the sort an entirely different way, using the built-in :sort command
                            (previously I had avoided that due to the jaw-dropping complexity already in the
                            script, but the fix turned out to be quite simple).

                            Please see the updated code:
                            http://vim.wikia.com/wiki/Working_with_CSV_files

                            The code is now even more astonishingly clever: You can sort by column, using all
                            the :sort options (range, reverse, case insensitive, unique, numeric).

                            Please give it a try and report any problems!

                            John


                            --~--~---------~--~----~------------~-------~--~----~
                            You received this message from the "vim_use" maillist.
                            For more information, visit http://www.vim.org/maillist.php
                            -~----------~----~----~----~------~----~------~--~---
                          • grandpa
                            John Great job ! Thanks, I love it !!! --~--~---------~--~----~------------~-------~--~----~ You received this message from the vim_use maillist. For more
                            Message 13 of 25 , Oct 9, 2008
                              John

                              Great job !

                              Thanks, I love it !!!
                              --~--~---------~--~----~------------~-------~--~----~
                              You received this message from the "vim_use" maillist.
                              For more information, visit http://www.vim.org/maillist.php
                              -~----------~----~----~----~------~----~------~--~---
                            • Christian Brabandt
                              Hi John! ... [...] ... This seems to work: return (a:line1 a:line2) ? 1 : ((a:line1 == a:line2) ? 0 : -1) regards, Christian -- • You know you re
                              Message 14 of 25 , Oct 10, 2008
                                Hi John!

                                On Thu, 09 Oct 2008, John Beckett wrote:

                                >
                                > Can anyone point out the bug in the code below? (This encapsulates
                                > the sorting
                                > problem of the CSV tip reported by nh_wzg).
                                >
                                > Steps:
                                > - Copy below to a file, say test.txt.
                                > - Yank the second para (copy the code).
                                > - Type :@" and press Enter to execute the code.
                                > - Press F1 to call MySort() to sort the first para.
                                >
                                > Problems:
                                > - Does not sort lines into any recognisable order.
                                > - Changes every time you press F1 (sort is not stable).
                                >
                                > Is this due to MyCompare sometimes comparing numbers and strings?
                                > Any fix?
                                >
                                [...]
                                >
                                > " Sort lines in first paragraph.
                                > map <F1> :call MySort()<CR>
                                > function! MySort()
                                > normal! gg}k
                                > call setline(1, sort(getline(1, '.'), function('MyCompare')))
                                > endfunction
                                > function! MyCompare(line1, line2)
                                > return a:line1 > a:line2
                                > endfunction
                                > ---end---
                                >
                                This seems to work:
                                return (a:line1 > a:line2) ? 1 : ((a:line1 == a:line2) ? 0 : -1)

                                regards,
                                Christian
                                --
                                • You know you're brilliant, but maybe you'd like to understand what you did
                                2 weeks from now.
                                Linus Torvalds: Linux 1.3.53 CodingStyle documentation.

                                --~--~---------~--~----~------------~-------~--~----~
                                You received this message from the "vim_use" maillist.
                                For more information, visit http://www.vim.org/maillist.php
                                -~----------~----~----~----~------~----~------~--~---
                              • nh_wzg
                                ... Hi John: With the example below: ===Begin=== num,md51,fullpath_filename,fullname2, 5.988,44029A87AD3484711FACB7945D242A95,X: I386 0401.CS_,0401.csv,
                                Message 15 of 25 , Oct 10, 2008
                                  John Beckett-2 wrote:
                                  >
                                  > John Beckett-2 wrote:
                                  >
                                  >> Please see the updated code:
                                  >
                                  >> http://vim.wikia.com/wiki/Working_with_CSV_files
                                  >
                                  >> Please give it a try and report any problems!
                                  >
                                  >

                                  Hi John:

                                  With the example below:
                                  ===Begin===
                                  num,md51,fullpath_filename,fullname2,
                                  5.988,44029A87AD3484711FACB7945D242A95,X:\I386\0401.CS_,0401.csv,
                                  -3.45,10D0BA441255894A51C8B3308EB11638,X:\I386\0410.CS_,0410.csv,
                                  -1.90,45158FF10F952BCFC590ECB2C80D487E,X:\I386\0414.CS_,0414.csv,
                                  1.723,3ECA13B43EF48F80F0179856A73A3BA6,X:\I386\0416.CS_,0416.csv,
                                  0.123,59D691289820021216E6CDCAD74743D4,X:\I386\0419.CS_,0419.csv,
                                  6.976,FEC5273EB05873C93DA9BE16A94AEE94,X:\I386\AGT0401.DL_,Agt0401.dll,
                                  7.892,FF04598C86B5DF9B78F122ABCB266790,X:\I386\AGT0419.HL_,Agt0419.hlp,
                                  0.175,FF5EA7161307D7170C39D31629239CDF,X:\I386\BOPOMOFO.UC_,bopomofo.UCE,
                                  4.576,FF36362448658F15D2B6E3B593CA1B3F,X:\I386\BUSY_I.CU_,busy_i.cur,
                                  2.114,FE82CCD0D5BD8F6FDE7D48252C25F271,X:\I386\CATSRVUT.DL_,catsrvut.dll,
                                  78.45,FFE31887684AED0F7B124378D942D3AC,X:\I386\CB32.EX_,cb32.exe,
                                  7.234,FE0399122F5DB5158E28AF73A1ED94A1,X:\I386\CDOSYS.DL_,cdosys.dll,
                                  12.65,FFF68CFBFA883D81731A6B344A25B782,X:\I386\CGA80852.FO_,cga80852.fon,
                                  9.123,FF9BDB807FC99B2B6156742AC2F9F2AB,X:\I386\COADMIN.DL_,coadmin.dll,
                                  4.513,FE97D798B2BC6CB4306B21E078AA6214,X:\I386\C_437.NL_,c_437.nls,
                                  0.362,FF6223C000741845CE8CD0C6336021D1,X:\I386\D3DXOF.DL_,d3dxof.dll,
                                  3.120,FE385CA3249BF6D30C031DEACE045AD3,X:\I386\DBMSRPCN.DL_,DBmsRPCn.dll,
                                  5.123,FF39047446FC1C46FC0C1FA4071063CB,X:\I386\DG18D50.IC_,Dg18d50.icm,
                                  8.679,FFFBC075AA7E1756E35B5AF2E8BE7DEB,X:\I386\DISK.SY_,disk.sys,
                                  10.32,FECC01645D83F150EBE2924887E30B91,X:\I386\DMUTIL.DL_,dmutil.dll,
                                  -0.34,FF762A792EFCB11C917A2682352F71D1,X:\I386\HPCRDP.CA_,hpcrdp.cat,
                                  9.772,FFF5CBFC71FB5C80AAD9F834B3FB5062,X:\I386\IMESHARE.DL_,imeshare.dll,
                                  3.452,FF32B826D2F3C5E41DCAF8B272AB0BF3,X:\I386\INETSRV.MI_,inetsrv.mib,
                                  ===End===

                                  I have test it with commands below correctly:
                                  :Sort " sort by the current column
                                  :Sort 3 " sort by column 3
                                  :Sort! 3 " same, reverse order
                                  :5,12Sort! 3 " same, but sort only lines 5 to 12 inclusive

                                  I have test it with command is wrong:
                                  :Sort 1 n " numeric sort by column 1
                                  ===Result Begin===
                                  num,md51,fullpath_filename,fullname2,
                                  0.123,59D691289820021216E6CDCAD74743D4,X:\I386\0419.CS_,0419.csv,
                                  0.175,FF5EA7161307D7170C39D31629239CDF,X:\I386\BOPOMOFO.UC_,bopomofo.UCE,
                                  0.362,FF6223C000741845CE8CD0C6336021D1,X:\I386\D3DXOF.DL_,d3dxof.dll,
                                  -0.34,FF762A792EFCB11C917A2682352F71D1,X:\I386\HPCRDP.CA_,hpcrdp.cat,
                                  -1.90,45158FF10F952BCFC590ECB2C80D487E,X:\I386\0414.CS_,0414.csv,
                                  1.723,3ECA13B43EF48F80F0179856A73A3BA6,X:\I386\0416.CS_,0416.csv,
                                  2.114,FE82CCD0D5BD8F6FDE7D48252C25F271,X:\I386\CATSRVUT.DL_,catsrvut.dll,
                                  -3.45,10D0BA441255894A51C8B3308EB11638,X:\I386\0410.CS_,0410.csv,
                                  3.120,FE385CA3249BF6D30C031DEACE045AD3,X:\I386\DBMSRPCN.DL_,DBmsRPCn.dll,
                                  3.452,FF32B826D2F3C5E41DCAF8B272AB0BF3,X:\I386\INETSRV.MI_,inetsrv.mib,
                                  4.576,FF36362448658F15D2B6E3B593CA1B3F,X:\I386\BUSY_I.CU_,busy_i.cur,
                                  4.513,FE97D798B2BC6CB4306B21E078AA6214,X:\I386\C_437.NL_,c_437.nls,
                                  5.988,44029A87AD3484711FACB7945D242A95,X:\I386\0401.CS_,0401.csv,
                                  5.123,FF39047446FC1C46FC0C1FA4071063CB,X:\I386\DG18D50.IC_,Dg18d50.icm,
                                  6.976,FEC5273EB05873C93DA9BE16A94AEE94,X:\I386\AGT0401.DL_,Agt0401.dll,
                                  7.892,FF04598C86B5DF9B78F122ABCB266790,X:\I386\AGT0419.HL_,Agt0419.hlp,
                                  7.234,FE0399122F5DB5158E28AF73A1ED94A1,X:\I386\CDOSYS.DL_,cdosys.dll,
                                  8.679,FFFBC075AA7E1756E35B5AF2E8BE7DEB,X:\I386\DISK.SY_,disk.sys,
                                  9.123,FF9BDB807FC99B2B6156742AC2F9F2AB,X:\I386\COADMIN.DL_,coadmin.dll,
                                  9.772,FFF5CBFC71FB5C80AAD9F834B3FB5062,X:\I386\IMESHARE.DL_,imeshare.dll,
                                  10.32,FECC01645D83F150EBE2924887E30B91,X:\I386\DMUTIL.DL_,dmutil.dll,
                                  12.65,FFF68CFBFA883D81731A6B344A25B782,X:\I386\CGA80852.FO_,cga80852.fon,
                                  78.45,FFE31887684AED0F7B124378D942D3AC,X:\I386\CB32.EX_,cb32.exe,
                                  ===Result End===
                                  I remember the last version csv.vim you post with the 4-lines example can
                                  deal with the numberic fields correctly.

                                  The rest commands I have not test:
                                  :Sort 3 x " same, hex numbers
                                  :Sort i u " ignore case; omit duplicate lines (keep unique)
                                  :Sort iu " same
                                  :Sort 3 iu " same, by column 3

                                  Thanks for your work in this lovely plugin !
                                  --
                                  View this message in context: http://www.nabble.com/Vim-Tips-wiki-celebrates-with-a-CSV-tip-tp18768619p19913865.html
                                  Sent from the Vim - General mailing list archive at Nabble.com.


                                  --~--~---------~--~----~------------~-------~--~----~
                                  You received this message from the "vim_use" maillist.
                                  For more information, visit http://www.vim.org/maillist.php
                                  -~----------~----~----~----~------~----~------~--~---
                                • John Beckett
                                  ... That s the good news - thanks. ... Ugh. OK so I ve had a closer look at what Vim is actually doing. My superficial testing made me hope that :sort n was
                                  Message 16 of 25 , Oct 10, 2008
                                    nh_wzg wrote:
                                    > I have test it with commands below correctly:
                                    > :Sort " sort by the current column
                                    > :Sort 3 " sort by column 3
                                    > :Sort! 3 " same, reverse order
                                    > :5,12Sort! 3 " same, but sort only lines 5 to 12 inclusive

                                    That's the good news - thanks.

                                    > I have test it with command is wrong:
                                    > :Sort 1 n " numeric sort by column 1

                                    Ugh. OK so I've had a closer look at what Vim is actually doing. My superficial
                                    testing made me hope that ':sort n' was sorting floating point ok. But it was
                                    self-delusion.

                                    It turns out that ':sort n' does the following:
                                    - Skip any leading nondigits.
                                    - If last nondigit was '-' take final number as negative.
                                    - Convert number to a long integer (ignoring overflow).
                                    - Number is a string of digits ending with any nondigit.
                                    - Sort using the signed long integers.

                                    So 'xxx12.65' is regarded as exactly 12, and '-0.34' is exactly 0, and (on a 32-bit
                                    machine) 2147483660 is a large negative number (overflow).

                                    I'll contemplate how this mess might be improved. Al's code (in the talk page at
                                    http://vim.wikia.com/wiki/Talk:Working_with_CSV_files#Sorting_by_column) attempts to
                                    detect the type of string, and convert it to an appropriate value, while handling
                                    floats. It would be a real shame to tack on our own compare function, particularly
                                    one which converts each string each time, but it might have to be done.

                                    The correct way to handle this would be to extend Vim to:
                                    - Make ':sort n' handle floats (probably not). -or-
                                    - Make ':sort f' (say) handle floats.

                                    Of course quite a lot would have to happen before such a patch to Vim saw daylight.

                                    John


                                    --~--~---------~--~----~------------~-------~--~----~
                                    You received this message from the "vim_use" maillist.
                                    For more information, visit http://www.vim.org/maillist.php
                                    -~----------~----~----~----~------~----~------~--~---
                                  • John Beckett
                                    The muse took me, and I have implemented sort for floating point fields in a CSV file. Please see the updated code:
                                    Message 17 of 25 , Oct 10, 2008
                                      The muse took me, and I have implemented sort for floating point fields in a CSV
                                      file.

                                      Please see the updated code:
                                      http://vim.wikia.com/wiki/Working_with_CSV_files

                                      OK, it's a bit long, but it's amazing code! Of course you need a version of Vim that
                                      supports floats. The :version command should show +float in its output (get Vim 7.2
                                      if you don't have it).

                                      A bit of testing would be good ... please let me know how you go.

                                      I'm using Vim's str2float() to convert a field to a float. A field like "1.25junk"
                                      will be regarded as 1.25. A field like "junk1.25" will be regarded as a string and
                                      will sort before numbers.

                                      John


                                      --~--~---------~--~----~------------~-------~--~----~
                                      You received this message from the "vim_use" maillist.
                                      For more information, visit http://www.vim.org/maillist.php
                                      -~----------~----~----~----~------~----~------~--~---
                                    • nh_wzg
                                      Please see the updated code: http://vim.wikia.com/wiki/Working_with_CSV_files I m using Vim s str2float() to convert a field to a float. A field like
                                      Message 18 of 25 , Oct 11, 2008
                                        Please see the updated code:
                                        http://vim.wikia.com/wiki/Working_with_CSV_files

                                        I'm using Vim's str2float() to convert a field to a float. A field like
                                        "1.25junk"
                                        will be regarded as 1.25. A field like "junk1.25" will be regarded as a
                                        string and
                                        will sort before numbers.


                                        Hi John:

                                        With the updated code,the result of the example I post above with the ":Sort
                                        1 n" is:
                                        ===Result Begin===
                                        num,md51,fullpath_filename,fullname2,
                                        -3.45,10D0BA441255894A51C8B3308EB11638,X:\I386\0410.CS_,0410.csv,
                                        -1.90,45158FF10F952BCFC590ECB2C80D487E,X:\I386\0414.CS_,0414.csv,
                                        0.123,59D691289820021216E6CDCAD74743D4,X:\I386\0419.CS_,0419.csv,
                                        0.175,FF5EA7161307D7170C39D31629239CDF,X:\I386\BOPOMOFO.UC_,bopomofo.UCE,
                                        0.362,FF6223C000741845CE8CD0C6336021D1,X:\I386\D3DXOF.DL_,d3dxof.dll,
                                        -0.34,FF762A792EFCB11C917A2682352F71D1,X:\I386\HPCRDP.CA_,hpcrdp.cat,
                                        1.723,3ECA13B43EF48F80F0179856A73A3BA6,X:\I386\0416.CS_,0416.csv,
                                        2.114,FE82CCD0D5BD8F6FDE7D48252C25F271,X:\I386\CATSRVUT.DL_,catsrvut.dll,
                                        3.120,FE385CA3249BF6D30C031DEACE045AD3,X:\I386\DBMSRPCN.DL_,DBmsRPCn.dll,
                                        3.452,FF32B826D2F3C5E41DCAF8B272AB0BF3,X:\I386\INETSRV.MI_,inetsrv.mib,
                                        4.576,FF36362448658F15D2B6E3B593CA1B3F,X:\I386\BUSY_I.CU_,busy_i.cur,
                                        4.513,FE97D798B2BC6CB4306B21E078AA6214,X:\I386\C_437.NL_,c_437.nls,
                                        5.988,44029A87AD3484711FACB7945D242A95,X:\I386\0401.CS_,0401.csv,
                                        5.123,FF39047446FC1C46FC0C1FA4071063CB,X:\I386\DG18D50.IC_,Dg18d50.icm,
                                        6.976,FEC5273EB05873C93DA9BE16A94AEE94,X:\I386\AGT0401.DL_,Agt0401.dll,
                                        7.892,FF04598C86B5DF9B78F122ABCB266790,X:\I386\AGT0419.HL_,Agt0419.hlp,
                                        7.234,FE0399122F5DB5158E28AF73A1ED94A1,X:\I386\CDOSYS.DL_,cdosys.dll,
                                        8.679,FFFBC075AA7E1756E35B5AF2E8BE7DEB,X:\I386\DISK.SY_,disk.sys,
                                        9.123,FF9BDB807FC99B2B6156742AC2F9F2AB,X:\I386\COADMIN.DL_,coadmin.dll,
                                        9.772,FFF5CBFC71FB5C80AAD9F834B3FB5062,X:\I386\IMESHARE.DL_,imeshare.dll,
                                        10.32,FECC01645D83F150EBE2924887E30B91,X:\I386\DMUTIL.DL_,dmutil.dll,
                                        12.65,FFF68CFBFA883D81731A6B344A25B782,X:\I386\CGA80852.FO_,cga80852.fon,
                                        78.45,FFE31887684AED0F7B124378D942D3AC,X:\I386\CB32.EX_,cb32.exe,
                                        ===Result End===

                                        It seems still some bugs in the code.
                                        Hope the feedback is useful.

                                        Best regards,
                                        nh_wzg
                                        --
                                        View this message in context: http://www.nabble.com/Vim-Tips-wiki-celebrates-with-a-CSV-tip-tp18768619p19931031.html
                                        Sent from the Vim - General mailing list archive at Nabble.com.


                                        --~--~---------~--~----~------------~-------~--~----~
                                        You received this message from the "vim_use" maillist.
                                        For more information, visit http://www.vim.org/maillist.php
                                        -~----------~----~----~----~------~----~------~--~---
                                      • John Beckett
                                        ... See the new documentation just above the script: http://vim.wikia.com/wiki/Working_with_CSV_files The command :Sort 1 n uses the built-in Vim :sort
                                        Message 19 of 25 , Oct 11, 2008
                                          nh_wzg wrote:
                                          > With the updated code,the result of the example I post above
                                          > with the ":Sort 1 n" is:

                                          See the new documentation just above the script:
                                          http://vim.wikia.com/wiki/Working_with_CSV_files

                                          The command ":Sort 1 n" uses the built-in Vim :sort command to sort by integers.

                                          If you want to sort floating point numbers, you need ":Sort 1 f" which uses new code
                                          in the script to sort using floats. Your sample sorts perfectly using that command.

                                          The "f" option should correctly sort integers (0, -12, 1234 etc), and floats (.5,
                                          123.456, -1.73, +12.5, 1e3 etc). Use ":Sort! 1 f" to sort in reverse order.

                                          When using "f", any fields in the column being sorted that are not numbers are
                                          sorted as strings. All strings sort before numbers. String sorting is case
                                          insensitive if the 'ignorecase' option is set.

                                          John


                                          --~--~---------~--~----~------------~-------~--~----~
                                          You received this message from the "vim_use" maillist.
                                          For more information, visit http://www.vim.org/maillist.php
                                          -~----------~----~----~----~------~----~------~--~---
                                        • John Beckett
                                          I ve gone crazy and added a bunch more code to the CSV tip: http://vim.wikia.com/wiki/csv It now supports sorting in various ways including floating point
                                          Message 20 of 25 , Oct 13, 2008
                                            I've gone crazy and added a bunch more code to the CSV tip:
                                            http://vim.wikia.com/wiki/csv

                                            It now supports sorting in various ways including floating point numbers, and allows
                                            you to specify the delimiter (default ',').

                                            Seeing tab-separated values in Vim is quite attractive, and makes me hope that the
                                            patch for variable tabstops becomes part of Vim one day (I haven't got around to
                                            trying the patch).

                                            John


                                            --~--~---------~--~----~------------~-------~--~----~
                                            You received this message from the "vim_use" maillist.
                                            For more information, visit http://www.vim.org/maillist.php
                                            -~----------~----~----~----~------~----~------~--~---
                                          • Christian Brabandt
                                            Hi John! ... Wouldn t it be better to make a plugin out of the whole tip? regards, Christian -- ... --~--~---------~--~----~------------~-------~--~----~ You
                                            Message 21 of 25 , Oct 14, 2008
                                              Hi John!

                                              On Tue, 14 Oct 2008, John Beckett wrote:

                                              > I've gone crazy and added a bunch more code to the CSV tip:
                                              > http://vim.wikia.com/wiki/csv
                                              >

                                              Wouldn't it be better to make a plugin out of the whole tip?

                                              regards,
                                              Christian
                                              --
                                              :wq

                                              --~--~---------~--~----~------------~-------~--~----~
                                              You received this message from the "vim_use" maillist.
                                              For more information, visit http://www.vim.org/maillist.php
                                              -~----------~----~----~----~------~----~------~--~---
                                            • John Beckett
                                              ... True, but after some discussion we decided to keep the code on the wiki: 1. It was real wiki collaboration between 3 or 4 people that moved the tip from a
                                              Message 22 of 25 , Oct 14, 2008
                                                Christian Brabandt wrote:
                                                >> I've gone crazy and added a bunch more code to the CSV tip:
                                                >> http://vim.wikia.com/wiki/csv
                                                >
                                                > Wouldn't it be better to make a plugin out of the whole tip?

                                                True, but after some discussion we decided to keep the code on the wiki:

                                                1. It was real wiki collaboration between 3 or 4 people that moved the tip from a
                                                pretty simple and ineffective script to a useful masterpiece. In fact, with a couple
                                                of minor exceptions, it's the only case we have of that.

                                                2. It's very easy for bit rot to set in on vim.org/scripts. Once the person who did
                                                the initial upload disappears, no one else can contribute. We thought about a shared
                                                password, but it seemed impractical.

                                                3. I'm trying to promote the wiki, so I want to keep it on the wiki!

                                                John


                                                --~--~---------~--~----~------------~-------~--~----~
                                                You received this message from the "vim_use" maillist.
                                                For more information, visit http://www.vim.org/maillist.php
                                                -~----------~----~----~----~------~----~------~--~---
                                              Your message has been successfully submitted and would be delivered to recipients shortly.