Author Topic: Ignoring certain blocks on output  (Read 17330 times)

0 Members and 1 Guest are viewing this topic.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Ignoring certain blocks on output
« Reply #45 on: April 10, 2013, 12:23:28 PM »
Lee,

(setq str "\"Smith,John\",\"\"\"The Baptist\"\"\",\"abc\"\"The Baptist\"\"123\",\"acb\"\"\",\"\"\"123\"")
Comando: (ALE_String_CdfToList str ",")
("Smith,John" "\"The Baptist\"" "abc\"The Baptist\"123" "acb\"" "\"123")
Comando: (ALE_String_CdfToListFE str ",")
("Smith,John" "\"The Baptist\"" "abc\"The Baptist\"123" "acb\"" "\"123")
Comando: (LM:csv->lst str 44 0)
("Smith,John" "\"The Baptist\"" "abc\"The Baptist\"123" "acb\"\",\"\"123")
                                                                                                 ^^^^^

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Ignoring certain blocks on output
« Reply #46 on: April 10, 2013, 04:51:09 PM »
Thanks Marc, I'll investigate. 8-)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Ignoring certain blocks on output
« Reply #47 on: April 11, 2013, 11:10:18 AM »
New version for problems with multiple separator: "\"Smith,,,,,John\",123"
Code: [Select]
; Version 1.01 - 11-04-2013
(defun ALE_String_CdfToList (InpStr CarDlm / SttPos EndPos TmpStr OutLst TmpElm)
  (setq CarDlm (ascii CarDlm)   SttPos 0    EndPos (vl-string-position CarDlm InpStr))
  (while EndPos
    (cond
      ( (wcmatch (setq TmpElm (substr InpStr (1+ SttPos) (- EndPos SttPos))) "\"*\"")
        (setq OutLst (cons (ALE_StringSubstAll "\"" "\"\"" (substr TmpElm 2 (- (strlen TmpElm) 2)) 0) OutLst))
      )
      ( (wcmatch TmpElm "\"*") (setq TmpStr (substr TmpElm 2)) )
      ( (= TmpElm "") (if TmpStr (setq TmpStr (strcat TmpStr ",")) (setq TmpStr ",")) )
      ( TmpStr
        (setq
          OutLst (cons (strcat TmpStr (chr CarDlm) (substr TmpElm 1 (- (strlen TmpElm) 1))) OutLst)
          TmpStr nil
        )
      )
      ( T (setq OutLst (cons TmpElm OutLst)) )
    )
    (setq SttPos (1+ EndPos) EndPos (vl-string-position CarDlm InpStr SttPos))
  )
  (reverse
    (cons
      (cond
        ( (wcmatch (setq TmpElm (substr InpStr (1+ SttPos))) "\"*\"")
          (ALE_StringSubstAll "\"" "\"\"" (substr TmpElm 2 (- (strlen TmpElm) 2)) 0)
        )
        ( (wcmatch TmpElm "\"*")(setq TmpStr (substr TmpElm 2)) )
        ( TmpStr (strcat TmpStr (chr CarDlm) (substr TmpElm 1 (- (strlen TmpElm) 1))) )
        ( T TmpElm )
      )
      OutLst
    )
  )
)

Comando: (ALE_String_CdfToList "\"Smith,,,,,John\",123" ",")
("Smith,,,,,John" "123")

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Ignoring certain blocks on output
« Reply #48 on: April 15, 2013, 11:02:47 AM »
New version for problems with multiple separator: "\"Smith,,,,,John\",123"
New version for other problems:
Code: [Select]
; Version 1.02 - 15-04-2013
(defun ALE_String_CdfToList (InpStr CarDlm / AscDlm SttPos EndPos TmpStr OutLst TmpElm)
  (setq AscDlm (ascii CarDlm)   SttPos 0    EndPos (vl-string-position AscDlm InpStr))
  (while EndPos
    (cond
      ( (wcmatch (setq TmpElm (substr InpStr (1+ SttPos) (- EndPos SttPos))) "\"*\"")
        (setq OutLst (cons (ALE_StringSubstAll "\"" "\"\"" (substr TmpElm 2 (- (strlen TmpElm) 2)) 0) OutLst))
      )
      ( (and (not TmpStr) (wcmatch TmpElm "\"*")) (setq TmpStr (substr TmpElm 2)) )
      ( TmpStr
        (cond
          ( (= TmpElm "\"") (setq OutLst (cons (strcat TmpStr CarDlm) OutLst)  TmpStr nil) )
          ( (wcmatch TmpElm "*\"")
            (setq
              OutLst (cons (strcat TmpStr CarDlm (substr TmpElm 1 (- (strlen TmpElm) 1))) OutLst)
              TmpStr nil
            )
          )
          ( T (setq TmpStr (strcat TmpStr "," TmpElm)) )
        )
      )
      ( T (setq OutLst (cons TmpElm OutLst)) )
    )
    (setq SttPos (1+ EndPos) EndPos (vl-string-position AscDlm InpStr SttPos))
  )
  (reverse
    (cons
      (cond
        ( (wcmatch (setq TmpElm (substr InpStr (1+ SttPos))) "\"*\"")
          (ALE_StringSubstAll "\"" "\"\"" (substr TmpElm 2 (- (strlen TmpElm) 2)) 0)
        )
        ( (and (not TmpStr) (wcmatch TmpElm "\"*")) (substr TmpElm 2) )
        ( TmpStr
          (cond
            ( (= TmpElm "\"") (strcat TmpStr CarDlm) )
            ( (wcmatch TmpElm "*\"") (strcat TmpStr CarDlm (substr TmpElm 1 (- (strlen TmpElm) 1))) )
            ( T (strcat TmpStr "," TmpElm) )
          ) 
        )
        ( T TmpElm )
      )
      OutLst
    )
  )
)
Comando: (ALE_String_CdfToList "\",Smith,,r,,,John,,\",123,\",fd,,ffd,\"" ",")
(",Smith,,r,,,John,," "123" ",fd,,ffd,")

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Ignoring certain blocks on output
« Reply #49 on: May 22, 2013, 11:16:19 AM »
Lee,

(setq str "\"Smith,John\",\"\"\"The Baptist\"\"\",\"abc\"\"The Baptist\"\"123\",\"acb\"\"\",\"\"\"123\"")
Comando: (LM:csv->lst str 44 0)
("Smith,John" "\"The Baptist\"" "abc\"The Baptist\"123" "acb\"\",\"\"123")
                                                                                                 ^^^^^
Thanks Marc, I'll investigate. 8-)

I finally got around to looking into this and have now updated my ReadCSV function:

Code - Auto/Visual Lisp: [Select]
  1. _$ (setq str "\"Smith,John\",\"\"\"The Baptist\"\"\",\"abc\"\"The Baptist\"\"123\",\"acb\"\"\",\"\"\"123\"")
  2. "\"Smith,John\",\"\"\"The Baptist\"\"\",\"abc\"\"The Baptist\"\"123\",\"acb\"\"\",\"\"\"123\""
  3. _$ (LM:csv->lst str "," 0)
  4. ("Smith,John" "\"The Baptist\"" "abc\"The Baptist\"123" "acb\"" "\"123")

@Marc:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq str "\",\",\",,\",\"\"\"\",\"\"\"\"\"\",\",\"\"\",\"\"\",\"\"\"")
  2. "\",\",\",,\",\"\"\"\",\"\"\"\"\"\",\",\"\"\",\"\"\",\"\"\""

Result should be:
Code - Auto/Visual Lisp: [Select]
  1. ("," ",," "\"" "\"\"" ",\"" "\",\"")

Code - Auto/Visual Lisp: [Select]
  1. _$ (LM:csv->lst str "," 0)
  2. ("," ",," "\"" "\"\"" ",\"" "\",\"")
  3. _$ (ALE_String_CdfToList str ",")
  4. ("," ",," "\"" "\"\"" "\"" "\"" "\"")

I guess it depends how far you wish to go with it...  :|

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Ignoring certain blocks on output
« Reply #50 on: May 22, 2013, 01:22:24 PM »
Result should be:
Code - Auto/Visual Lisp: [Select]
  1. ("," ",," "\"" "\"\"" ",\"" "\",\"")
Code - Auto/Visual Lisp: [Select]
  1. _$ (LM:csv->lst str "," 0)
  2. ("," ",," "\"" "\"\"" ",\"" "\",\"")
  3. _$ (ALE_String_CdfToList str ",")
  4. ("," ",," "\"" "\"\"" "\"" "\"" "\"")
I guess it depends how far you wish to go with it...  :|

Thanks  Lee, I'll investigate. 8-)

Ciao

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Ignoring certain blocks on output
« Reply #51 on: June 27, 2013, 12:17:10 PM »
I will try again but I think it's out of my reach.
In the meantime I found another problem:

Comando: (LM:csv->lst "\"aaa," "," 0)
; errore: valore dell'argomento errato: string position out of range 6


Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Ignoring certain blocks on output
« Reply #52 on: June 30, 2013, 06:48:15 PM »
In the meantime I found another problem:

Comando: (LM:csv->lst "\"aaa," "," 0)
; errore: valore dell'argomento errato: string position out of range 6

Marc,

If possible, could you please attach the CSV file used to generate that string ("\"aaa,")?
With my understanding of the CSV format, that isn't valid CSV content.

Lee
« Last Edit: June 30, 2013, 07:15:07 PM by Lee Mac »

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Ignoring certain blocks on output
« Reply #53 on: July 01, 2013, 03:06:27 AM »
My apologies Lee, I do not know if ("\"aaa,") is a valid CSV content, it came out doing some manual testing, delete my report.

Thank you.

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Ignoring certain blocks on output
« Reply #54 on: July 01, 2013, 07:42:21 AM »
My apologies Lee, I do not know if ("\"aaa,") is a valid CSV content, it came out doing some manual testing, delete my report.

No problem at all Marc  :-)

Regarding the validity of the string, if you were to input the string "aaa, in a blank text file and save the file to CSV format, upon opening the file in Excel, the first cell contains aaa,, however, after saving the file, the text file content becomes "aaa," as expected since the quotes should be paired.

Nevertheless, the error returned by my function could be quite easily avoided by changing line 26 to:
Code - Auto/Visual Lisp: [Select]
  1. (   (or (<= (strlen str) pos) (not (setq pos (vl-string-search sep str pos))))
« Last Edit: July 01, 2013, 07:50:05 AM by Lee Mac »

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Ignoring certain blocks on output
« Reply #55 on: July 01, 2013, 08:41:33 AM »
 :kewl:

Thanks Lee, I do not think I'll be back again on this argument because I have reached saturation...
 :'(

Buona giornata.

Marco