Snippets

sironekotoro Perl入学式 2018 第4回 練習問題 while_input.pl

Created by sironekotoro

File while_input.pl Added

  • Ignore whitespace
  • Hide word diff
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+# Perl入学式 2018 第4回 練習問題
+# https://github.com/perl-entrance-org/workshop-2018/blob/master/4th/slide.md
+# 練習問題(while_input.pl)
+
+# 標準入力に対する条件が多いので、スクリプト中に都度記載
+# 入力と出力をわかりやすくするために区切り線を入れた
+
+print '=== Input Some Strings ===', "\n";
+
+while ( chomp( my $input = <STDIN> ) ) {
+
+    print '=== Return Strings ===', "\n";
+
+    # 文字列が0の場合, ループを抜ける(lastを使って...)
+    if ( $input eq '0' ) {
+        print "input 0 , script end\n";
+        last;
+    }
+
+    # 文字列がperlないしPerlを含む場合,
+    # 「Find Perl!」と表示する.
+    if ( $input =~ /[pP]erl/ ) {
+        print "Find Perl!\n";
+    }
+
+    # 文字列に大文字小文字問わず, pythonの文字列が
+    # 含まれる場合, 「Find Python!」と表示する.
+    if ( $input =~ /python/i ) {
+        print "Find Python!\n";
+    }
+
+    # 文字列にperlないしrubyないしpythonが
+    # 含まれる場合, 「Love Programming!」と
+    # 表示する.
+    if ( $input =~ /perl|ruby|python/ ) {
+        print "Love Programming!\n";
+    }
+
+    # 文字列の先頭にpapixがある場合,
+    # 「Find papix!」と表示する.
+    if ( $input =~ /^papix/ ) {
+        print "Find papix!\n";
+    }
+
+    # 文字列にHelloが含まれる場合, その後に
+    # 続く単語xxxxを使って「Hello! xxxx!」と
+    # 表示する.
+    if ( $input =~ /Hello/ ) {
+        $input =~ /Hello (.+)/;
+        print "Hello! $1!\n";
+    }
+
+    print '=== Input Some Strings ===', "\n";
+
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.