&&ã¨andã¨||ã¨or
&&ã¨||ã§ä¸é æ¼ç®ã使ã
ãªãã ãé
ã¨æ³ªã¨ç·ã¨å¥³ãæãåºãã¦ãã¾ãã¿ã¤ãã«ãã¤ããããã§ããã
è¿ãå¤ã0ã1ããè¿ãå¦çãæ¸ãå ´åã«&&ã¨||ã¯éå®ãã¾ãã
ç§ã¯ããä¸é æ¼ç®åãã»ããã«ãã¦ä½¿ãã¾ãã
ä¸é æ¼ç®åã¯æ¸ããªãã¦ãè¯ãããããã¾ããããå¾ã§ã½ã¼ã¹ã³ã¼ããè¦ãã¨ãããããããªããªãã®ã§æ¸ããã»ããããã¨æãã¾ãã
ã¹ã¯ãªãã
#!/usr/bin/perl use strict; use warnings; use utf8; my $boolean; warn $boolean = false() && true() && true() ? 1 : 0; warn $boolean = false() || true() || false() ? 1 : 0; sub true { warn "this is true"; return 1; } sub false { warn "this is false"; return 0; }
å®è¡çµæ
this is false at script/op.pl line 19. 0 at script/op.pl line 7. this is false at script/op.pl line 19. this is true at script/op.pl line 14. 1 at script/op.pl line 8.
解説
A && B && C ã¯ãA, B, Cã®å ¨ã¦ã1ã®å ´åã«çã¨ãªãã¾ããã¨ãããã¨ã¯Aã®çµæãå½ã ã£ãå ´åã¯Bã¨Cã®å¦çã¯è¡ãã¾ããã
A or B or C 㯠çã®çµæãå¾ãããã¾ã§é çªã«A, B, Cã¨å¦çããã¾ãããçãå¾ãããå ´åã¯æ®ãã®å¦çãã¹ããããã¾ãã
and 㨠or
and 㨠orã¯ãããã&&ã¨||ã¨åãæåããã¾ããããããã次ã®ã¹ã¯ãªãããã覧ãã ããã
ã¹ã¯ãªãã
#!/usr/bin/perl use strict; use warnings; use utf8; my $boolean; warn $boolean = false() and true() and true() ? 1 : 0; warn $boolean = false() or true() or false() ? 1 : 0; sub true { warn "this is true"; return 1; } sub false { warn "this is false"; return 0; }
å®è¡çµæ
:!perl script/op.pl this is false at script/op.pl line 19. 0 at script/op.pl line 10. this is true at script/op.pl line 14. this is true at script/op.pl line 14. this is false at script/op.pl line 19. 0 at script/op.pl line 11.
ããããããããããªããã¡ãããã
解説
æ¼ç®åã®åªå é ä½ãéããããã
12:23 (kan) ||ã ã¨ãããã®ã«orã ã¨é§ç®ãªã®ã¯ãä¸é æ¼ç®åã¨ã®åªå é ä½ã®åé¡ã ã¨æãã¾ã>ãããããã 12:24 (okamura) ãªãã¨
perldocjpã¿ã¦ã¿ã
perldocjp -J perlop
å·¦çµå && å·¦çµå || // â¦â¦ å³çµå ?: â¦â¦ å·¦çµå and å·¦çµå or xor
ããã¼æåãåãã ãã©æ¼ç®ã®åªå é ä½ãéãã®ãã¼
ãªã®ã§A and B and C ? 1 : 0ã®å ´åã¯æ¬¡ã®å¦çããã¦ãã¾ã*1
- A ? 1 : 0
- ããã¦0ãè¿ããã®ã«ãªãã Bãè©ä¾¡ããã¦ãã
- Bãçãè¿ããã®ã§Cãè©ä¾¡ãããã
- Cã¯1ãè¿ãã¦ãããã©æ¢ã«A ? 1 : 0ã«ãã£ã¦å·¦è¾ºã®å¤æ°ã«ã¯0ãæ ¼ç´ããã¦ãã
ã¨ããããA and B and C ? 1 : 0ã¿ãããªå¦çæ¸ãã¡ãã ãã§A && B && C ? 1 : 0ã¨æ¸ãããã«ããã*2
ç¿æ¥è¿½è¨
ã¨ããããã§ç¿æ¥ç¤¾å ã«ã¦ã()ã§å²ãã°ããããããªãï¼ãã¨è¨ãããå·±ã®æªçããæãç¥ããããããã§ãã
ソウï¾ï¾ï½¼ï¾ï¾ãソウスï¾ï¾ï¾ï½²ï½²ï¾ï¾ï½½ï¾ï¾ã
ã§ããA or B or C ? 1 : 0ãã¯æåã«C ? 1: 0ãæ¼ç®ããã¦ãããã®ã§ã¯ï¼ã¨ããçåãé æ´ããã®ã§æ¬¡ã®ãããªã³ã¼ããç¨æãã¾ããã
#!/usr/bin/perl use strict; use warnings; use utf8; my $boolean; warn $boolean = A() and B() and C() ? 1 : 0; warn "-" x 10; warn $boolean = a() and b() and c() ? 1 : 0; warn "-" x 10; warn $boolean = A() or B() or C() ? 1 : 0; warn "-" x 10; warn $boolean = a() or b() or c() ? 1 : 0; sub A { warn "A"; return 1; } sub a { warn "a"; return 0; } sub B { warn "B"; return 1; } sub b { warn "b"; return 0; } sub C { warn "C"; return 1; } sub c { warn "c"; return 0; }
çµæ
:!perl script/op.pl A at script/op.pl line 19. 1 at script/op.pl line 8. B at script/op.pl line 29. C at script/op.pl line 39. ---------- at script/op.pl line 9. a at script/op.pl line 24. 0 at script/op.pl line 10. b at script/op.pl line 34. ---------- at script/op.pl line 12. A at script/op.pl line 19. 1 at script/op.pl line 14. ---------- at script/op.pl line 15. a at script/op.pl line 24. 0 at script/op.pl line 16.
ã¾ãã¨ã«ãããªãã§ãããªããã¯ããããããä»ã¯()ãã¤ãã¦æ¸ãããããããã