ç¶æ é·ç§»ããã°ã©ã
ãã£ã¨â¦â¦ã¡ãã£ã¨é£ãã説æã«ãªãã®ã§ãããã¨ã¯ãããããç¨åº¦ã®ã¬ãã«ã®ããã°ã©ãã§ããã°ç¥ã£ã¦æã¯ãªãã£ã¦ãããç¥ããªããæãããã¿ãªã®ã§ãé å¼µã£ã¦èª¬æãã¦ã¿ããã¨æãã¾ãã
ã¾ããç¶æ
é·ç§»å³ã£ã¦ã®ãããã¾ããããã«ã¤ãã¦ã¯ã¦ãã¨ãã«ããã£ã¦(www.google.com)ãã ããã
ãã¦ãä¾é¡ã¨ãã¦CSVãåãä¸ãã¾ããä¸ã¤ãä¾é¡ãªã®ã§ç°¡ç¥åãã¾ã :-P
CSVã®æååãäºã¤ã®ã¢ã¼ãã«åãåãã¦ã¿ã¾ãããã
ã¤ã¾ã
ã»é常ã¢ã¼ã
ã»äºéå¼ç¨å¥å
ã¢ã¼ã
ã§ãã
é常ã¢ã¼ãã«ããã¦ã,ã¯ãã¼ã¿ã®åºåããæ¹è¡ã¯ã¬ã³ã¼ãã®åºåãã表ãã¾ãã
äºéå¼ç¨å¥å
ã¢ã¼ãã«ããã¦ã¯ã,ãæ¹è¡ãããã®ã¾ãã¾ãã¼ã¿ã¨ãã¦ãåãæ±ãã¾ãã
ã¾ãã
é常ã¢ã¼ãã§äºéå¼ç¨å¥âãåºã¦ããå ´åãäºéå¼ç¨å¥å
ã¢ã¼ãã«ç§»è¡ãã¾ãã
äºéå¼ç¨å¥å
ã¢ã¼ãã§äºéå¼ç¨å¥âãåºã¦ããå ´åãé常ã¢ã¼ãã«ç§»è¡ãã¾ãã
ããã¾ã§ã¯OKã§ããããï¼
ãã¦ã
é常ããã°ã©ã ãçµãã¨ãã£ã¦ãå¤åifæãé£æãã¦ã
ã£ã¦æãã§æ¸ããã¨æãã®ã§ããã
foreach ( 対象æå <- æåå) {
ãif (ã¢ã¼ã == é常) {
ãã// é常ã¢ã¼ã ã®å¦ç
ããif (対象æå == ',') {
ããããã¼ã¿åºåãã®å¦ç
ãã} else
ããif (対象æå == '\n') {
ãããã¬ã³ã¼ãåºåãã®å¦ç
ãã} else
ããif (対象æå == '"') {
ãããã¢ã¼ã = äºéå¼ç¨å¥å
ãã} else {
ããã対象æåããã¼ã¿ã«è¶³ããã
ãã}
ã} else if (ã¢ã¼ã == äºéå¼ç¨åºå ) {
ãã// äºéå¼ç¨å¥å ã¢ã¼ã ã®å¦ç
ããif (対象æå == '"') {
ãããã¢ã¼ã = é常
ãã} else {
ããã対象æåããã¼ã¿ã«è¶³ããã
ãã}
ã}
}
ä»åã¯ã¢ã¼ããï¼ã¤ã§ãããã¾ã ããã®ã§ãããããã§ç¶æ ããã£ã¨æ²¢å±±ãã£ãããæãã¯ãAã®ç¶æ ããã¯Bã¨Cã«ãªããã¦ãBããã¯Aã¨Cã¨Dã¨Eã«ãªããã¦ãCããã¯Bã¨Dã¨Eã«ãªããã¦â¦ãã¿ãããªè¤éãªç¶æ é·ç§»å³ãã³ã¼ãåããã¨ããªããç©åããã¹ãã®åµã«ãªã£ã¦ãå¯èªæ§ãä½ä¸ãããã¨è«ãåãã§ã :-P
ããã§ç¶æ
é·ç§»ã§ãã
ç§ã¯ãã£ã±ããã¯ã©ã¹ or é¢æ°ãã¤ã³ã¿ãç¨ãã¾ãã
ä»åã¯ã¯ã©ã¹ãç¨ãã¦ã¿ã¾ãããã
ã¯ã©ã¹ã¯ããå¦çã¯ã©ã¹ããåºåºã¯ã©ã¹ã¨ãã¦ããã®æ´¾çã¯ã©ã¹ã¨ãã¦ãé常ã¢ã¼ãå¦çã¯ã©ã¹ãã¨ãäºéå¼ç¨åºå
ã¢ã¼ãå¦çã¯ã©ã¹ããå®è£
ãã¾ãã
é¢æ°ãã¤ã³ã¿ä½¿ããªããå¼æ°ã¨æ»ãå¤ããããã¦ããã°OKã§ãã
ã§ã以ä¸ãã½ã¼ã¹ã«ãªãã¾ãã
ãããªæãã§ãã
// ã¯ã©ã¹å®£è¨:ã£ã¤ãã»ã¨ãã©æ§é ä½ã§ãã
class ãã¼ã¿ã¯ã©ã¹ {
private:
ãé常ã¤ã³ã¹ã¿ã³ã¹;
ãäºéå¼ç¨å¥å ã¤ã³ã¹ã¿ã³ã¹;ãå解ããCSVãä¿åã§ããããªãã¼ã¿ã¿ã¤ã
}// åæå¦ç
ãã¼ã¿ã¯ã©ã¹->é常ã¤ã³ã¹ã¿ã³ã¹ ï¼ é常ã¢ã¼ãå¦çã¯ã©ã¹;
ãã¼ã¿ã¯ã©ã¹->äºéå¼ç¨åºå ã¤ã³ã¹ã¿ã³ã¹ ï¼ äºéå¼ç¨åºå ã¢ã¼ãå¦çã¯ã©ã¹;å¦çã¤ã³ã¹ã¿ã³ã¹ = é常ã¤ã³ã¹ã¿ã³ã¹; // åãåããåãã¯é常ã¤ã³ã¹ã¿ã³ã¹ã¹ã¿ã¼ãã£ã¦ãã¨ã§ã
foreach ( 対象æå <- æåå) {
ã// å¦ç
å¦çã¤ã³ã¹ã¿ã³ã¹ = å¦çã¤ã³ã¹ã¿ã³ã¹->å¦ç(ãã¼ã¿ã¯ã©ã¹, 対象æå);
}
ãï¼ çããã¦ããããããªãï¼
ã§ã¯é ã追ã£ã¦ã
ã¾ãåãã¯ããå¦çã¤ã³ã¹ã¿ã³ã¹->å¦ç()ãã£ã¦ã®ã¯å®éã«ã¯ãé常ã¤ã³ã¹ã¿ã³ã¹->å¦ç()ããcallããã¾ãã
ã§ãé常ã¢ã¼ãå¦çã¯ã©ã¹å
ã®å¦çã¡ã½ããã¯ãåè¿°ã®ã¾ãã¾ã,ã¯ãã¼ã¿ã®åºåããæ¹è¡ã¯ã¬ã³ã¼ãã®åºåãããã以å¤ã¯ãã¼ã¿ãã¨ããå¦çããã¾ãã
ã¾ããäºéå¼ç¨å¥âãåºã¦ããå ´åãäºéå¼ç¨å¥å
ã¢ã¼ãã«ç§»è¡ããªããã°ãªããªãã®ã§ãreturnã«ããã¼ã¿ã¯ã©ã¹->äºéå¼ç¨å¥å
ã¤ã³ã¹ã¿ã³ã¹ãããããã§ãªãã°ããã¯returnã«ããã¼ã¿ã¯ã©ã¹->é常ã¤ã³ã¹ã¿ã³ã¹ãã復帰ãã¾ãã
åãããã«ãäºéå¼ç¨å¥å
ã¢ã¼ãå¦çã¯ã©ã¹ã®å¦çã¡ã½ãããåããããªå¦çããã¾ãã
ããããã¨ãããé常ã¢ã¼ãã§äºéå¼ç¨å¥ãåºã¦ããã°ãå¦çã¤ã³ã¹ã¿ã³ã¹ãã®ãªãã¸ã§ã¯ãã¸ã®ãã¤ã³ã¿ãåãæ¿ããã®ã§èªåçã«ã¢ã¼ããäºéå¼ç¨åºå
ã¢ã¼ãã«åãæ¿ããããã®å¦çãè¡ããããã®éãã¾ãããããã¨ãªãã¾ãã
ç¶æ
é·ç§»ããã°ã©ã ã¯ãããªæãã§ããããã®ç¶æ
ã«ãã£ãå¦çãç°¡åã«è¨è¿°ã§ããå²åã«ã·ã³ãã«ã«å¦çãããã¾ãã
ã£ã¦ããããç¶æ
é·ç§»å³ã®ããããã®âãã¨ã«ã¯ã©ã¹(ãªããé¢æ°)ãæ¸ãã ããªã®ã§ãããªãè¤éãªç¶æ
é·ç§»ã§ã楽ã«æ¸ãã¾ãã
å®éã«CSVãå¦çããã¨ãã¯ãç§ã¯
ï¼set_nomalï¼é常ã¢ã¼ã
æãåºæ¬çãªç¶æ
ã以ä¸ã®æåãè¡ãã
',' cellã®åºåããæå³ããæåã¨å¤æãã
'"' ã¨ã¹ã±ã¼ãã¢ã¼ãã¸ã®ã¢ã¼ãåæ¿ãè¡ã
'\r' æ¹è¡ã ããCR-LFã®å¯è½æ§ãèããCRLFã¢ã¼ãã«åãæ¿ãã
'\n' æ¹è¡ãä¸è¡ã®ãã¼ã¿ã®çµããã¨å¤æãã
any é常ã®ãã¼ã¿ã¨ã¿ãªã
ï¼set_crlfï¼CRLFã¢ã¼ã
CR-LFãæ³å®ãã¦ããã¢ã¼ã
'\n' '\r\n'ã§ãã£ããããããã§ä¸è¡ã®ãã¼ã¿ã®çµããã¨å¤æ
any '\r\n'ã§ã¯ãªãã£ããããç¾å¨ã®ãã¼ã¿ãé常ã¢ã¼ãã§åå¦ç
ï¼set_espï¼ã¨ã¹ã±ã¼ãã¢ã¼ã
'"'ã','ãå
¥ããããã®ã¢ã¼ãã','ã¯ãã ã®ãã¼ã¿ã¨ãã¦æ±ããã
'"' ã¢ã¼ãåæ¿ããã¼ã¿ãããããªããããã¨ã¹ã±ã¼ããã§ãã¯ã¢ã¼ãã¸åãæ¿ã
any é常ã®ãã¼ã¿ã¨ã¿ãªã
ï¼set_esp_chkï¼ã¨ã¹ã±ã¼ããã§ãã¯ã¢ã¼ã
ã¨ã¹ã±ã¼ãã¢ã¼ãä¸ã«åºã¦ãã'"'ãå¦çããããã®ã¢ã¼ã
'"' '""'ã¨ãªã£ã¦ããããããã¼ã¿ã«'"âã追å ãã¢ã¼ãã¯ã¨ã¹ã±ã¼ãã
any ã¨ã¹ã±ã¼ãã¢ã¼ãããé常ã¢ã¼ãã¸ã®åãæ¿ãæ示ã¨ã¿ãªãã
ã¢ã¼ããé常ã«åãæ¿ããç¾å¨ã®ãã¼ã¿ãé常ã¢ã¼ãã§åå¦ç
ã®ï¼ã¤ã®ã¢ã¼ãã®åãæ¿ãã§å¦çãã¦ãã¾ãã
â¦ã£ã¦ãPerlã§ãããã°ãµã³ãã«ã³ã¼ãã®ã£ããã»ãããããã§ããããï¼