본문 바로가기

program

[perl] 연습문제 1



#!/usr/bin/perl -w        ## -w 옵션은 자세한 에러 확인 옵션
@words = ("camel","llama","alpaca");
## @words = qw(camel llama alpaca); (위 내용과 동일한 효과, ","등의 인용부호를 두지 않고 작성하는 방법임)

print "what is your name?";
$name = <STDIN>;    ## 사용자 변수값을 입력을 받음
chomp ($name);      ## 뉴라인 (\n) 값을 제거하는 함수

if ($name eq "Randal") {
        print "Hello, Randal !! How good of ypu to be here !! \n";
} else {
        print "Hi, $name !! \n";
        print "what is ther secret word? ";
        $guess = <STDIN>;
        chomp ($guess);
        $i = 0;
        $correct = "maybe";
        while ($correct eq "maybe") {           ## while 은 참이면 반복, 거짓이 나오면 exit
                if ($words[$i] eq $guess) {
                        $correct = "yes";
                } elsif ($i < 2) {
                        $i = $i + 1;
                } else {
                        print "sorry!, try again. what is your secret word? ";
                        $guess = <STDIN>;
                        chomp ($guess);
                        $i = 0;
                }
        }               ## while 끝

}       ## not Randal 끝