開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Perl 6 (プログラミング言語)
- Rakudo(コンパイラ、実装)
Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)のPart 1(Starting with the basics)、Chapter 7(Strings)の Putting it all together、Extracting dates の Exercisesを取り組んでみる。
Putting it all together、Extracting dates の Exercises
コード(Emacs)
#!/usr/bin/env perl6
# -*- coding: utf-8 -*-
my $string1 = 'Christmas : 25/12/2016.';
my $string2 = 'Christmas : 2016 12, 25.';
my $y = rx/\d ** 4/;
my $m = rx/\d ** 2 <?{ 1 <= $/ <= 12}>/;
my $d = rx/\d ** 2 <?{ 1 <= $/ <= 31}>/;
$string1 ~~ /(<$d>)\/(<$m>)\/(<$y>)/;
say $/.prematch;
say $/.postmatch;
say +$/[0];
say +$/[1];
say +$/[2];
$string2 ~~ /(<$y>)\s+(<$m>)','\s*(<$d>)/;
say $/.prematch;
say $/.postmatch;
say +$/[0];
say +$/[1];
say +$/[2];
入出力結果(Terminal, REPL)
$ ./sample_dates.pl Christmas : . 25 12 2016 Christmas : . 2016 12 25 $
0 コメント:
コメントを投稿