国产探花免费观看_亚洲丰满少妇自慰呻吟_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;
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 塔河县| 永登县| 塔河县| 雷波县| 阿尔山市| 习水县| 宜宾县| 霍邱县| 玛纳斯县| 来凤县| 太和县| 德兴市| 上思县| 喀喇| 锡林郭勒盟| 武强县| 屏东市| 山西省| 宜州市| 涟水县| 深州市| 工布江达县| 兰坪| 临澧县| 文水县| 衡阳市| 南开区| 诏安县| 陕西省| 剑阁县| 荃湾区| 昌平区| 九龙城区| 科尔| 崇信县| 永兴县| 西贡区| 来凤县| 城口县| 丰台区| 新泰市|