zoukankan      html  css  js  c++  java
  • perl打印乘法表

    最近在看《Perl高效编程》,很赞的一本书~书上提到了打印乘法表的一个例子~借这个小例子重新学习perl,由其是引用这块。

    这个例子包括了不少基础知识,有创建二维数组,引用的自动带入等等。

    直接上代码了:

     1 #!/usr/bin/perl
    2 use strict;
    3 use Getopt::Long;
    4 my ($matrix,$num,$flag);
    5 my $maxnum=9;
    6 GetOptions("n:i"=>\$num,
    7 "h"=>\$flag,);
    8 die "num is not valid" if (!defined $num)||$num>$maxnum||$num<0;
    9 for(my $i=0;$i<=$num;$i++){
    10 for(my $j=0;$j<=$num;$j++){
    11 $matrix->[$i][$j]=$i*$j;
    12 }
    13 }
    14 my $format='%2d ' x (@{$matrix});
    15 printf "i*j $format\n",0 .. $num;
    16 foreach (0 .. $num){
    17 printf "%2d $format\n",$_,@{$matrix->[$_]};
    18 }



  • 相关阅读:
    Currency Exchange
    Robot Motion
    Crashing Robots
    Parencodings
    Y2K Accounting Bug
    Tautology
    Power of Cryptography
    Radar Installation -poj 1328
    The Pilots Brothers' refrigerator
    【java】之cron表达式
  • 原文地址:https://www.cnblogs.com/aLittleBitCool/p/2228469.html
Copyright © 2011-2022 走看看