perl寫的一個(gè)隨機(jī)編故事的程序(rand隨機(jī)函數(shù))
更新時(shí)間:2013年03月20日 23:01:21 作者:
用perl寫的一個(gè)隨機(jī)編故事的程序,用到了rand隨機(jī)函數(shù),注釋也很詳細(xì),需要的朋友可以參考下
復(fù)制代碼 代碼如下:
#!/bin/perl
use strict;
use warnings;
#定義變量
my $count;
my $input;
my $number;
my $sentence;
my $story;
#定義四個(gè)數(shù)組
#定義了人物數(shù)組
my @nouns=
(
'Dad',
'TV',
'Mom',
'Groucho',
'Rebecca',
'Harpo',
'Robin Hood',
'Joe and Moe',
);
#定義了動(dòng)作數(shù)組
my @verbs=
(
'ran to',
'giggled with',
'put hot sauce into the orange juice of',
'exploded',
'dissolved',
'dissolved',
'sang stupid songs with',
'jumped with',
);
#定義了地點(diǎn)數(shù)組
my @prepositions=
(
'at the store',
'over the rainbow',
'at the beach',
'before dinner',
'in New York City',
'in a dream',
'around the world',
);
do
{
#每次運(yùn)行以前,清空$story的內(nèi)容
$story='';
#以下是隨機(jī)組合生成故事
for ($count =0;$count<6;$count++)
{
#我們劃分一下結(jié)構(gòu),scalar @nouns 是獲取@nouns數(shù)組元素的個(gè)數(shù),然后用rand總這幾個(gè)數(shù)中隨機(jī)生成幾個(gè),最后int是將得到的數(shù)取整
$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>;
}
#這里是利用正則表達(dá)式來匹配首字符是q的字符串,如果有,那么就退出,/^是匹配起始位置,/i是不區(qū)分大小寫
until ($input=~/^\s*q/i);
exit;
您可能感興趣的文章:
相關(guān)文章
Perl Sort函數(shù)用法總結(jié)和使用實(shí)例
這篇文章主要介紹了Perl Sort函數(shù)用法總結(jié)和使用實(shí)例,需要的朋友可以參考下2014-05-05用perl實(shí)現(xiàn)生物突變的隨機(jī)模擬程序代碼
perl寫的生物突變的隨機(jī)模擬程序,有需要的朋友可以參考下2013-03-03Perl Mysql數(shù)據(jù)庫操作實(shí)現(xiàn)代碼
對(duì)于perl操作mysql的步驟,說的比較詳細(xì),建議大家好好看看,參考2009-01-01基于charles抓取https請(qǐng)求使用過程解析
這篇文章主要介紹了基于charles抓取https請(qǐng)求使用過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11perl中srand()與time的函數(shù)使用方法介紹
這篇文章主要介紹了perl中srand與time函數(shù)的使用,需要的朋友可以參考下2013-03-03