国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Perl > 正文

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

2020-10-31 15:17:16
字體:
來源:轉載
供稿:網友
復制代碼 代碼如下:

#!/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>; 

#這里是利用正則表達式來匹配首字符是q的字符串,如果有,那么就退出,/^是匹配起始位置,/i是不區分大小寫 
until ($input=~/^/s*q/i); 
exit;
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 城口县| 永靖县| 临颍县| 鄂尔多斯市| 通河县| 台东县| 隆安县| 朝阳市| 寻乌县| 永寿县| 稷山县| 临沂市| 普陀区| 前郭尔| 宁强县| 盖州市| 临汾市| 宁化县| 化德县| 翁源县| 齐齐哈尔市| 乐清市| 普定县| 沧州市| 丹江口市| 莎车县| 鄂托克旗| 蒙阴县| 东山县| 陈巴尔虎旗| 咸丰县| 宁远县| 商河县| 铁岭县| 花垣县| 通江县| 水富县| 台中县| 河北区| 弥勒县| 封开县|