Perl ã®åºç¤ç¥èã§å®ç¨ããã°ã©ãã³ã° ãæ æ ç·¨ã
Perl5 Advent Calendar 2016 7æ¥ç®ã®è¨äºã§ãã
æ¨æ¥ã¯ @mackee_w ããã® Perlã®ELVM ããã¯ã¨ã³ããå®è£
ãã¦ãã¥ã¼ãã³ã°ãã話 ã§ããã
ä»æ¥ã®ãã®è¨äºã§ã¯ãPerl åå¿è ããåãã«ãPerl ã®åºç¤ç¥èãããã°ããããªã«å®ç¨çãªããã°ã©ã ãä½ãã¾ãããã¨ããä¾ãç´¹ä»ãã¾ãã
ç®æ¬¡
- éçºåæ©
- æè¡çãããã¯
- åãæ©è½
- 使ãæ¹
- åä½ã¤ã¡ã¼ã¸
- ã¢ã¤ãã ãªã¹ããªã¹ã
- ããã°ã©ã
- é¢é£ãªã³ã¯
- æå¾ã«
éçºåæ©
ã¿ãªããã¯ãæ
è¡ã®è·ç©ã¨ã®ãã¤ãåãã¯é 調ã§ããããã
æ
å
ã§å¿ãç©ã«æ°ã¥ãã¦å°ã£ããããã®éã«ãä½è¨ãªç©ã¾ã§æåãã¦è·ç©ã®éãã«è¦ããã ããããã¨ã¯ããã¾ãããï¼ ã¾ããè·é ãã®éã«ãä½ãæã£ã¦è¡ãã¹ããã§æ¯åº¦ã®ããã«é ãæ©ã¾ãã¦ããããã¾ãããï¼
ç§ã¯ã©ãããããå½ã¦ã¯ã¾ãã®ã§ãããã©ã¡ããã¨ãããã¨ããå¿ãç©ããã¯è·ç©ã®éããé¸å®ã§å°ã£ã¦ãã¾ããã
åé¡ãèªè¦ããã®ã¯ãä»å¹´ã®å¤ã®ãã¨ã§ããéæ¥18ãã£ã·ã10æ¥åè³¼å
¥ãã¦ãå§çºã§åºçºããæ
å
ã§ãé»è»ã«ä¹ãåããçµé»1æ¬åã§å¸°å®
ããã¨ããã¢ã¯ãã£ããã£ã«å±ãã ã®ã§ããããè·ç©ã¯ãªãã¹ã軽ãå°ãããã¨ããããªã·ã¼ã«åãã¦ãã©ãã©ãè·ç©ãå¢ãã¦ãã...
æ
æ
£ãã¦ããæ¹ãªããéæ¥18ãã£ã·ãå§çºãçµé»1æ¬åã¨ãããã¼ã¯ã¼ãã ãã§ãå¯ãããããã¾ããããå®ã¯ãã®æ
ããã£ããã·ãã¢ã ã£ãã®ã§ãã移åã«çé6æéã¨ã9æéã¨ãã¯ã¶ã©ã§ãæ»ã£ã¦ããã®ã«ãåããããã®æéããããã®ã§ãããæ
å
ã§ã¡ãã£ã¨å¤§ããã«äºå®ãçãã¨ã楽ãã¿ã«ãã¦ããç®çå°ãåããªããªã£ããã家ã«å¸°ããªããªã£ããããããã§ãã
ä½åº¦ãèã®å·ããæãããã¦ãããã¡ã«ãåãããã°æãç¡ãå¼ã«ã©ãã©ãè·ç©ãå¢ãã¦ãããã¤ãã«ã¯ããªã¥ãã¯ãéããã¦èµ°ã£ã¦ãåã«é²ããªãã£ãããã«é»è»ãéããã¨ããçããä½é¨ããã¾ããã
ãã®å¾ã¯ãç¡é§ãªè·ç©ãæ¸ããããã«ãæã£ã¦è¡ãã¹ããã®ã¨ããã§ãªããã®ã®å³»å¥ã«çå£ã«åãçµãããã«ãªã£ãã®ã§ããããããã¨ãã§ããªãæéãå°ãè¦è¡ã§ããã
æ
ã®åææ¡ä»¶ã£ã¦æ¯åéããã§ããããçãæ
ã¨é·ãã®æ
ã¨é·ãæ
ãPC ã®è¦ä¸è¦ããã¬ã¹ã³ã¼ãã®æç¡ããªã©ãªã©ãªã©ãä»æããæ¡ä»¶ã®çµã¿åããã ã㧠3 * 2 * 2 = 12ãã¿ã¼ã³ã§ãããå®éã«ã¯ãã£ã¨ç´°ã
ããæãåºãã«ããäºæãããããã¨æããæ¡ä»¶ã¯ãã£ã¨è¤éã«ãªãã¾ãã
ããããæ
ã«åºãã°ä½ããã価å¤è¦³ã«å½±é¿ãåããã®ã§ãåããããªæ¡ä»¶ã§ãå¿
è¦ãªè·ç©ã¯ã©ãã©ãå¤åãã¦ããã¾ãã
ãã®é£é¡ãã©ãã«ãããããã«ãã¹ãã¬ããã·ã¼ãã®é¢æ°ãé§ä½¿ãããªã©ãã¦æã¡ç©ãªã¹ãã®ä½æããã©ãã·ã¥ã¢ããã«ãå±ãã§ã¿ã¾ããããçµå±ãç´å¾ã®è¡ããªã¹ãã¯å®æãã¾ããã§ããã
ã¨ãããããããã§ãèªåã®é ã§èããã®ã¯è«¦ãã¦ãé£é¡è§£æ±ºã®ããã®åãããã°ã©ã ãä½ã£ãã®ã§ããã
æè¡çãããã¯
ãåãããã°ã©ã ãã¨èãã¨èº«æ§ãã¦ãã¾ã人ã®ããã«ããã®è¨äºãããã°ã©ã ã«ç»å ´ããæè¡çãããã¯ãæãã¦ããã¾ãã
Perl ã®ããã·ã¥ / é
å / ãªãã¡ã¬ã³ã¹ / ããªãã¡ã¬ã³ã¹ / ãµãã«ã¼ãã³ / æ¨æºå
¥å / ãã¡ã¤ã«èªã¿è¾¼ã¿ / æ£è¦è¡¨ç¾ / ã½ã¼ã / eval (解説æã)
Perl以å¤ã§ãTSVå½¢å¼ (解説æã)
ã©ãã§ãããï¼ ãåãããã°ã©ã ã¨ããå²ã«ãããã¾ãé«åº¦ãªãã¨ã¯ãã£ã¦ãªãããã ãªãã¨ããæ°ããã¦ãã¾ãããï¼ ã ã¨ãããæ£è§£ã§ããã®è¨äºã¯ããªãã®ãããªæ¹ã®ããã«æ¸ããã¾ãããã ãããã®ã¨ããã¯ç解ã§ããå 容ã«ãªã£ã¦ãã¾ãã®ã§ãå®å¿ãã¦èªã¿é²ãã¦ãã ããã
ããã§ã¯ããããåãããã°ã©ã ã®ç´¹ä»ã¸ã
åãæ©è½
- åææ¡ä»¶ããã¢ãªã³ã°ãã¦ãæ ã®å 容ã«ãµããããæã¡ç©ãªã¹ããä¸ç¬ã§ä½ã£ã¦ããã
- ãªãã¨ãå¿ è¦ãªæ°éã¾ã§æãã¦ããã
使ãæ¹
- Excel ã Google ã¹ãã¬ããã·ã¼ããªã©ã§æå®ã®ãã©ã¼ãããã®ã¢ã¤ãã ãªã¹ããä½ã
- TSVå½¢å¼ (å¾è¿°) ã§æ¸ãåºã
- ããã°ã©ã ã¨åããã£ã¬ã¯ã㪠(ãã©ã«ã) ã« items.tsv ã¨ãããã¡ã¤ã«åã§ç½®ã
- ã³ãã³ãã©ã¤ã³ã§ããã°ã©ã ãèµ·åãã
- æ ã«ã¤ãã¦ã®è³ªåã«æ¬¡ã ã«çãã¦ãã
ãã£ãããã ãã§ããã®æ ã«å¿ è¦ãªè·ç©ã®ãªã¹ããå®æããã¨ããä»çµã¿ã«ãªã£ã¦ãã¾ãã
åä½ã¤ã¡ã¼ã¸
ç¶ãã¦ãåä½ã¤ã¡ã¼ã¸ã§ãã
ãã¼ã¿ã«ãªã¢ãªãã£ãããã»ããåããä¼ãããããã®ã§ãç§ç§ä¼ã®ã¢ã¤ãã ãªã¹ãããµã³ãã«ã¨ãã¦ä½¿ãã¾ãã
æ ã®è¨å®ããªã¢ã«ã«è¡ãã¾ããã¡ããã©æå¾æ¥ãã2æ³3æ¥ã®åæµ·éé å¾äºå®ã§ãYAPC åå ã¤ãã§ã«ã¡ãã£ã¨è¶³ã伸ã°ãã¦è¶£å³ã¹ãããã訪ããã®ã§ããã®ããã®æã¡ç©ãªã¹ããä½ãã¾ãã
$ perl ./motteke.pl
ã§èµ·åãã
ä½æ³ãã¾ããï¼ èªç¶æ°ãå ¥åãã¦ãã ããã:
2 ã¨ã¿ã¤ãã㦠Enter ãæ¼ãã¨ã
以ä¸ã®è³ªåã¯ã該å½ããå ´åã®ã¿'y'ã¨çãã¦ãã ããã
ã¨æ¥ãã®ã§ã
ITç³»ã¤ãã³ãï¼: y ã¹ã¼ãï¼: ä»äºï¼: åè¡è ï¼: å¯ãï¼: y æãï¼: æªè¸ã¨ãªã¢ï¼: y æ´æ¿¯ï¼: ç°èï¼: y ç»å±±ï¼: è¶ å¯ãï¼: y é¨ï¼: éªï¼: y é£è¡æ©ï¼: y
ãããªæãã§åçããã¨ãå³åº§ã«æã¡ç©ãªã¹ããåºæ¥ä¸ããã¾ããå°ã é·ããªãã¾ããããªã¹ãå ¨ä½ãè¼ãã¦ãã¾ãã¾ãã
-------------------- ãã£ã¦ããªã¹ã -------------------- 00 ã«ãã³ ã¹ã¿ã³ã帳 * 1å 01 ã«ãã³ å * 1æ¬ 02 ã«ãã³ MacBook Pro * 1å 03 ã«ãã³ iPhone * 1å 04 ã«ãã³ è²¡å¸ * 1å 05 ã«ãã³ ãã³ * 1æ¬ 06 ã«ãã³ ã¡ã¢å¸³ * 1å 07 ã«ã㳠交éç³»ICã«ã¼ã * 1æ 08 ã«ãã³ ååºå ¥ã (ååºå ¥ã) * 1å 09 ã«ãã³ ãµã³ã°ã©ã¹ * 1æ¬ 10 ã«ãã³ A5ãã¡ã¤ã« * 1æ 11 ã«ãã³ ãã³ã«ã * 1æ 12 ã«ãã³ï¼è¶ãã¼ã ã¢ãã¤ã«ããããªã¼ * 1å 13 ã«ãã³ï¼è¶ãã¼ã ãã±ãããã£ãã·ã¥ * 1è¢ 14 ã«ãã³ï¼è¶ãã¼ã æ¯ãã©ã· * 1æ¬ 15 ã«ãã³ï¼è¶ãã¼ã é²æ°´ç¨ããã¼ã«è¢ * 1æ 16 ã«ãã³ï¼è¶ãã¼ã ã«ããªã¼ã¡ã¤ã * 1å 17 ã«ãã³ï¼è¶ãã¼ã ã«ã¤ã * 2å 18 ã«ãã³ï¼åéæãã¼ã MacBook é»æºã¢ããã¿ * 1å 19 å¤æ³è¢ å¤åºç * 2æ¥å 20 å¤æ³è¢ ã¤ã³ãã¼ã®é´ä¸å·»ã * 2å 21 å¤æ³è¢ æ¿ããã³ã«ã * 2æ 22 å¤æ³è¢ï¼é»ãã¼ã Lightningã»USB å¤æã¢ããã¿ * 1å 23 å¤æ³è¢ï¼é»ãã¼ã iPhone å é»ã¢ããã¿ * 1å 24 å¤æ³è¢ï¼éæãã¼ã ã³ã³ã¿ã¯ãã¬ã³ãº * 2ã»ãã 25 å¤æ³è¢ï¼éæãã¼ã ã³ã³ã¿ã¯ãã¬ã³ãºä¿å液 * 2æ¬ 26 å¤æ³è¢ï¼éæãã¼ã éººæ£ * 6æ¬ è¯ãæ ãï¼
ãã1ãã¿ã¼ã³ãã£ã¦ã¿ã¾ãã
ããã¾ããªã¢ã«ãªä¾ã§ãååã®æ
è¡ã§ããç»å±±ãã¤ä»äºãã¤å®¢å
訪åã¨ãã大éã®è·ç©ãå¿
è¦ã¨ãã3æ³4æ¥ã§ããã
$ perl ./motteke.pl ä½æ³ãã¾ããï¼ èªç¶æ°ãå ¥åãã¦ãã ããã: 3 以ä¸ã®è³ªåã¯ã該å½ããå ´åã®ã¿'y'ã¨çãã¦ãã ããã ITç³»ã¤ãã³ãï¼: y ã¹ã¼ãï¼: y ä»äºï¼: y åè¡è ï¼: å¯ãï¼: y æ¥å°ãï¼: y æãï¼: æªè¸ã¨ãªã¢ï¼: y æ´æ¿¯ï¼: ç°èï¼: y ç»å±±ï¼: y è¶ å¯ãï¼: é¨ï¼: éªï¼: é£è¡æ©ï¼: -------------------- ãã£ã¦ããªã¹ã -------------------- 00 - ãã¸ãã¹ããã° * 1å 01 - ãã¬ããã³ã°ãã¼ã« * 2æ¬ 02 ã«ãã³ ã¹ã¿ã³ã帳 * 1å 03 ã«ã㳠帽å * 1æ 04 ã«ãã³ MacBook Pro * 1å 05 ã«ãã³ iPhone * 1å 06 ã«ãã³ è²¡å¸ * 1å 07 ã«ãã³ ãã³ * 1æ¬ 08 ã«ãã³ ã¡ã¢å¸³ * 1å 09 ã«ãã³ ãã¼ã * 1å 10 ã«ã㳠交éç³»ICã«ã¼ã * 1æ 11 ã«ãã³ ååºå ¥ã (ååºå ¥ã) * 1å 12 ã«ãã³ ãµã³ã°ã©ã¹ * 1æ¬ 13 ã«ãã³ A5ãã¡ã¤ã« * 1æ 14 ã«ãã³ A4ãã¡ã¤ã« * 1æ 15 ã«ãã³ ãã³ã«ã * 1æ 16 ã«ãã³ï¼è²¡å¸ EX-ICã«ã¼ã * 1æ 17 ã«ãã³ï¼è²¡å¸ EX身å証æç¨ã¯ã¬ã« * 1æ 18 ã«ãã³ï¼è¶ãã¼ã ã¢ãã¤ã«ããããªã¼ * 1å 19 ã«ãã³ï¼è¶ãã¼ã ãã±ãããã£ãã·ã¥ * 1è¢ 20 ã«ãã³ï¼è¶ãã¼ã ã¦ã§ãããã£ãã·ã¥ * 1è¢ 21 ã«ãã³ï¼è¶ãã¼ã æ¯ãã©ã· * 1æ¬ 22 ã«ãã³ï¼è¶ãã¼ã ã«ããªã¼ã¡ã¤ã * 1å 23 ã«ãã³ï¼è¶ãã¼ã ã«ã¤ã * 1å 24 ã«ãã³ï¼åéæãã¼ã MacBook é»æºã¢ããã¿ * 1å 25 ã«ãã³ï¼åéæãã¼ã ãã£ã¹ãã¬ã¤ã¢ããã¿ * 1å 26 ã¹ã¼ãã±ã¼ã¹ï¼ã¹ã¼ãè¢ ã¹ã¼ã * 1ç 27 ã¹ã¼ãã±ã¼ã¹ï¼é´è¢ é©é´ * 1足 28 å¤æ³è¢ ã·ã£ã * 1æ 29 å¤æ³è¢ å¤åºç * 3æ¥å 30 å¤æ³è¢ ã¤ã³ãã¼ã®é´ä¸å·»ã * 3å 31 å¤æ³è¢ æ¿ããã³ã«ã * 3æ 32 å¤æ³è¢ï¼ãã¬ãã±ã¼ã¹ ã¡ã¬ã * 1æ¬ 33 å¤æ³è¢ï¼é»ãã¼ã Lightningã»USB å¤æã¢ããã¿ * 1å 34 å¤æ³è¢ï¼é»ãã¼ã iPhone å é»ã¢ããã¿ * 1å 35 å¤æ³è¢ï¼é»ãã¼ã ç®è¬ * 1æ¬ 36 å¤æ³è¢ï¼éæãã¼ã ã³ã³ã¿ã¯ãã¬ã³ãº * 3ã»ãã 37 å¤æ³è¢ï¼éæãã¼ã ã³ã³ã¿ã¯ãã¬ã³ãºä¿å液 * 3æ¬ 38 å¤æ³è¢ï¼éæãã¼ã éººæ£ * 9æ¬ è¯ãæ ãï¼
ä¸ã 26件ã§ä¸ã38件ãå®ã¯ãµã³ãã«ã®ãªã¹ãããã¯ãå ¬ã«ããããªã趣å³ã®åã ãªã©ãé¤å¤ãã¦ããã®ã§ãããããã§ããã£ãããªããªã¥ã¼ã ã§ãããããã®ä¸ã¤ä¸ã¤ã«ã¤ãã¦ãé½åº¦èªåã®é ã§è¦ä¸è¦ãå¤æãã¦ããããã«ã¯ãããæ»ãããã«ããã¾ããããã®åãããã°ã©ã ã¯ãã¯ã£ããè¨ã£ã¦ããã¡ã便å©ã§ãï¼
ã¢ã¤ãã ãªã¹ããªã¹ã
ãã¦ãããããã¯ãæè¡çãªè©±ãå¢ãã¦ãã¾ãã
ããã°ã©ã æ¬ä½ã®èª¬æã«å
¥ãåã«ãã¢ã¤ãã ãªã¹ãã«ã¤ãã¦èª¬æãã¾ãã
ã¾ãã¯ãµã³ãã«ã®ã¢ã¤ãã ãªã¹ã ãã覧ãã ããã
4åãããªãã¹ãã¬ããã·ã¼ãã§ãå·¦ããé ã«
Aå "å
¥ãç©"
Bå "åå"
Cå "æ°é"
Då "åä½"
ã¨ãªã£ã¦ãã¾ãã
è«çæ¼ç®åã¨ä¸é æ¼ç®å
2è¡ç®ä»¥éã®ãã¼ã¿è¡ãè¦ãã¨ãCå "æ°é" ã®å
容ããã ã®æ°åã§ã¯ãªããã¨ã«ããæ°ã¥ãããã¨æãã¾ãã
ããã¤ãã®ãã¼ã¿ã«ã¤ãã¦ããã®æå³ã表ã«ã¾ã¨ãã¾ãã
åå | æ°é | åä½ | 説æ |
è²¡å¸ | 1 | å | 財å¸ã1åæã£ã¦è¡ã |
ã³ã³ã¿ã¯ãã¬ã³ãºä¿å液 | æ³æ° | ã»ãã | Næ³ãããªãã¬ã³ãºãNã»ããæã£ã¦è¡ã |
é©é´ | ã¹ã¼ã ? 1 : 0 | å³ | ã¹ã¼ããçããªãé©é´ã1足æã£ã¦è¡ããçãªããªãé©é´ã¯ä¸è¦ |
é¨åç¾½ | (ç»å±± && é¨) ? 1 : 0 | æ | ç»å±±ãããã¨ãã«é¨ãªãã°1ææã£ã¦è¡ãããã以å¤ã®å ´åã¯è¦ããªã |
ã¦ã§ãããã£ãã·ã¥ | (ç»å±± || æã) ? 1 : 0 | è¢ | å±±ã«ç»ããããããã¯æãææãªã 1è¢æã£ã¦è¡ãããã以å¤ã®å ´åã¯è¦ããªã |
'&&' ã '||' 㯠Perl ãä»ã®è¨èªã®è«çæ¼ç®åã¨åãåãã'?' ã ':' ã¯ä¸é æ¼ç®åã¨åãåãããã¦ãã¾ããæ¥æ¬èªã®èª¬æã¨è¦æ¯ã¹ã¦ã¿ãã¨ãæ¼ç®åãç¨ãããã¨ã§ãç°¡æ½ã§ç´ãã®ãªã表ç¾ã«è½ã¨ãè¾¼ãã¦ãããã¨ãåããã¾ããã
ããã¡ãã£ã¨å¤§ããã«æ¯è¼ãããªãããããªæãã§ããããã
ãããã¤è¦ãã®ï¼ã
ããã¼ã£ã¨ãå±±ã«ç»ãã¨ãã¨ãæãã¨ãã¯1è¢ã§ããããªãã£ããè¦ããªãã
ãããããªãã£ãããã¯ã©ãã«æããã®ããæ£ç¢ºã«è¦ç¹ã ããè¨ãã
ã (ç»å±± || æã) ? 1 : 0ã
TSVå½¢å¼
使ãæ¹ã®é ã§ãã¡ãã£ã¨ãµãã¾ããããã¢ã¤ãã ãªã¹ã㯠TSVã¨ããå½¢å¼ã§ä¿åãã¾ãã
- Google ã¹ãã¬ããã·ã¼ãã®å ´å
- ãã¡ã¤ã«ã¡ãã¥ã¼ï¼å½¢å¼ãæå®ãã¦ãã¦ã³ãã¼ãï¼ã¿ãåºåãã®å¤ ã§TSVå½¢å¼ã§ãã¦ã³ãã¼ãã§ãã¾ãã
- Excel ã®å ´å
- ã§ã¯ãååãã¤ãã¦ä¿åã® "ãã©ã¼ããã" 㧠"ã¿ãåºåãããã¹ã" ãé¸æãããã¨ã§ãä»»æã®ã·ã¼ããTSVå½¢å¼ã§ä¿åã§ãã¾ãã
TSVå½¢å¼ã¨ãããã®ãç¥ããªãã£ãæ¹ããããã¾ã§ã§ä½ã¨ãªããå¯ãã®ãã¨ã¨æãã¾ãããTSV ã¨ããã®ã¯ã¿ãæååºåãã®ããã¹ããã¼ã¿ã®ãã¨ã§ãããã¼ã¿ã®åºåããã«ã³ãã§è¡¨ç¾ãã CSV ã¨ä¼¼ããããªãã®ã§ãããä¸è¨ã®ããã«éãã¾ãã
- CSV
- ãã¼ã¿ãã«ã³ãã§åºåã£ããã¯ã©ã¼ãããããå ´åã«ãã£ã¦ã¯ã¨ã¹ã±ã¼ãããããã
- TSV
- åç´ã«ã¿ãæåã§åºåãã ã
ä»åã®ããã°ã©ã ã§ã¯ãç°¡åã«ãã¼ã¿ãèªã¿è¾¼ãããã«ãã·ã³ãã«ä»æ§ã®TSVå½¢å¼ãæ¡ç¨ãã¾ããã
# æ¹è¡æåãå¤ã㦠\t 㧠split ããã ãã§ãã¼ã¿ãåãåºãã chomp($line); my @cols = split(/\t/, $line);
ããã°ã©ã
ç¶ãã¦ããã°ã©ã æ¬ä½ã«ã¤ãã¦ã®èª¬æã§ãã
ã½ã¼ã¹ã³ã¼ã㯠ãã¡ãã«ç½®ãã¦ããã¾ãã
å
¨ä½ã®å¦çã®æµãã¯ãæç« ã§èª¬æããããããmain ãµãã«ã¼ãã³ã®ã³ã¼ããè¦ã¦ããã£ãã»ããæ´ã¿ãããã¨æãã¾ãã
sub main { # TSVãã¡ã¤ã«ããã¢ã¤ãã ãã¼ã¿ãèªã¿è¾¼ã my $file_path = './items.tsv'; my $items = load_tsv($file_path); # Cå"æ°é"ã«ç»å ´ããè¨èããã¦ã¼ã¶ã¼ã«ç¢ºèªããããã«åãåºãã¦ãã my $words = get_words($items); # ã¦ã¼ã¶ã¼ããæ ã®æ¡ä»¶ãèãåºã my $conditions = ask_conditions($words); # åã¢ã¤ãã ã®å¿ è¦æ°éãæ±ãã calculate_quantities($items, $conditions); # æã¡ç©ãªã¹ããåºåãã print_item_list($items); print "\nè¯ãæ ãï¼\n"; }
ã½ã¼ã¹ã³ã¼ãå ã«é£ããæ§æã¯æãã¾ãããããã¼ã¿æ§é ãå°ãè¤éãªã®ã§ãæ¸æãããæ¹ãããããããã¾ããã&main ã«ç»å ´ãã $items, $words, $conditions ã¨ãã3ã¤ã®å¤æ°ã«ã¤ãã¦ä¸è¨ã«ã¾ã¨ããã®ã§ãèªè§£ã®åèã«ãã¦ãã ããã
$items
ãTSV ãã¡ã¤ã«ããèªã¿è¾¼ãã å 容ãã®ããã·ã¥ã®é åã®ãªãã¡ã¬ã³ã¹ã
[ { container => 'ã«ãã³', quantity => '(ITç³»ã¤ãã³ã || ä»äº) ? 1 : 0', unit => 'å', name => 'MacBook Pro', numeric_quantity => 1, }, { container => 'ã«ãã³', quantity => '1', unit => 'å', name => 'iPhone', numeric_quantity => 1, }, (ä¸ç¥) ]
numeric_quantity ã¯ãcalucurate_quantities($items, $conditions) ã«è¿½å ããããã¼ã§ãå¤ã¯æçµçãªå¿ è¦ãªæ°éã§ãã
$words
ã$item ã® quantity ãã¼ã®å¤ããåãåºããè¨èãã®é åã®ãªãã¡ã¬ã³ã¹ã
['ITç³»ã¤ãã³ã', 'ä»äº', (ä¸ç¥) , 'æ³æ°']
$conditions
ãã¦ã¼ã¶ã¼ããå¾ãæ¡ä»¶ç¾¤ãã®ããã·ã¥ã®ãªãã¡ã¬ã³ã¹ã
"æ³æ°" 以å¤ã®ãã¼ã®å¤ã¯ãæ¡ä»¶ãæºããå ´å㯠1ãæºãããªãå ´å㯠0 ã«ãªãã¾ãã
{ 'æ³æ°' => 2, 'ITç³»ã¤ãã³ã' => 1, 'ã¹ã¼ã' => 0, 'ä»äº' => 0, (ä¸ç¥) }
é¢é£ãªã³ã¯
æå¾ã«
åå¿è
ããåãã® Perl ããã°ã©ãã³ã°åå¼·ä¼ãªã©ã§ãã·ã³ã¿ãã¯ã¹ã¯å¦ãã ãã©ããã®ç¥èãã©ãæ´»ããã°ããã®ããåãããªãããã£ã¨ãããããªãã¨ãåå¼·ããªãã¨ä½ãã§ããªãæ°ããããã¨ãã声ãã¨ãã©ãèãã®ã§ãåºç¤ç¥èã§ã§ãããã¨ã®ä¸ä¾ãç´¹ä»ãã¾ããã
ãã®è¨äºããã©ãªãããä½ããä½ãå§ãããã£ããã«ç¹ããã°å¹¸ãã§ãã
ãªããç§ @wakegisky ã¯ãPerlã¬ã¤ãæ±æµ·ã®ä¸»å¬è ã®ä¸äººã¨ãã¦ãåå¿è ããåãã®ããã°ã©ãã³ã°åå¼·ä¼ãåå¤å±ã§ä¸å®æéå¬ãã¦ãã¾ãããè¿ãã®æ¹ã¯ãããã¿ã¤ãã³ã°ãåãã°ããã² Perlã¬ã¤ãæ±æµ·ã®ã¤ãã³ãã«ãè¶ããã ããã主å¬è ä¸åããå¾ ã¡ãã¦ããã¾ãã®ã§ï¼
ææ¥ã®è¨äºã¯ãã ããæ° (æªå®) ã®ä½ã (æªå®) ã§ããã ããæ°ããï¼