perl寫的一個隨機編故事的程式(rand隨機函數)

來源:互聯網
上載者:User

複製代碼 代碼如下:#!/bin/perl
use strict;
use warnings;

#定義變數
my $count;
my $input;
my $number;
my $sentence;
my $story;

#定義四個數組
#定義了人物數組
my @nouns=
(
'Dad',
'TV',
'Mom',
'Groucho',
'Rebecca',
'Harpo',
'Robin Hood',
'Joe and Moe',
);

#定義了動作數組
my @verbs=
(
'ran to',
'giggled with',
'put hot sauce into the orange juice of',
'exploded',
'dissolved',
'dissolved',
'sang stupid songs with',
'jumped with',
);

#定義了地點數組
my @prepositions=
(
'at the store',
'over the rainbow',
'at the beach',
'before dinner',
'in New York City',
'in a dream',
'around the world',
);

do
{
#每次運行以前,清空$story的內容
$story='';
#以下是隨機組合產生故事
for ($count =0;$count<6;$count++)
{
#我們劃分一下結構,scalar @nouns 是擷取@nouns數組元素的個數,然後用rand總這幾個數中隨機產生幾個,最後int是將得到的數取整
$sentence =$nouns[int(rand(scalar @nouns))]
." "
.$verbs[int(rand(scalar @verbs))]
." "
.$nouns[int(rand(scalar @nouns))]
." "
.$prepositions[int(rand(scalar @prepositions))]
.'.';
$story .=$sentence;
}
print "\n",$story,"\n";
print "\nType \"quit\"to quit, or press Enter to continue:";
$input=<STDIN>;
}
#這裡是利用Regex來匹配首字元是q的字串,如果有,那麼就退出,/^是匹配起始位置,/i是不區分大小寫
until ($input=~/^\s*q/i);
exit;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.