復制代碼 代碼如下:
#!/bin/perl
use strict;
use warnings;
#定義變量
my $count;
my $input;
my $number;
my $sentence;
my $story;
#定義四個數(shù)組
#定義了人物數(shù)組
my @nouns=
(
'Dad',
'TV',
'Mom',
'Groucho',
'Rebecca',
'Harpo',
'Robin Hood',
'Joe and Moe',
);
#定義了動作數(shù)組
my @verbs=
(
'ran to',
'giggled with',
'put hot sauce into the orange juice of',
'exploded',
'dissolved',
'dissolved',
'sang stupid songs with',
'jumped with',
);
#定義了地點數(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
{
#每次運行以前,清空$story的內(nèi)容
$story='';
#以下是隨機組合生成故事
for ($count =0;$count6;$count++)
{
#我們劃分一下結(jié)構(gòu),scalar @nouns 是獲取@nouns數(shù)組元素的個數(shù),然后用rand總這幾個數(shù)中隨機生成幾個,最后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>;
}
#這里是利用正則表達式來匹配首字符是q的字符串,如果有,那么就退出,/^是匹配起始位置,/i是不區(qū)分大小寫
until ($input=~/^\s*q/i);
exit;
您可能感興趣的文章:- sql server中隨機函數(shù)NewID()和Rand()
- python中隨機函數(shù)random用法實例
- php中隨機函數(shù)mt_rand()與rand()性能對比分析
- Oracle隨機函數(shù)之dbms_random使用詳解
- php數(shù)組函數(shù)序列 之shuffle()和array_rand() 隨機函數(shù)使用介紹
- 如何產(chǎn)生真正的隨機函數(shù)?
- 如何利用當前時間生成隨機函數(shù)?
- 詳解C語言中rand函數(shù)的使用