加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl程序中显示进度条之多姿多彩的自写代码

发布时间:2020-12-15 21:04:13 所属栏目:大数据 来源:网络整理
导读:在Perl程序中显示进度条之多姿多彩的自写代码 yixf 于 2011-11-29,09:57?? 方法一 12345678 #!/usr/bin/perl ? my $max = 10 ; for ( 1 .. $max ) { my $percent = $_ / $max * 100 ; print "$_ - $percent % OK! n " ; sleep ( 1 ) ; } 方法二 从“…..”

在Perl程序中显示进度条之多姿多彩的自写代码

  • 方法一

1
2
3
4
5
6
7
8
#!/usr/bin/perl
?
my $max = 10;
for ( 1 .. $max ) {
   my $percent = $_ / $max * 100;
   print "$_ - $percent % OK!n";
   sleep(1);
}
  • 方法二

    从“…..”的长度来判断进度。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    #!/usr/bin/perl -w
    ?
    $| = 1;
    my $max = 10;
    for ( 1 .. $max ) {
       print ".";
       print " Complete!n" if ( $_ == $max );
       sleep(1);
    }
    • 方法三

    用滚轮在原地显示进度,当然也可以加入百分比。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    #!/usr/bin/perl -w
    ?
    local $| = 1;
    my @progress_symbol = ( '-', '', '|', '/' );
    my $n = 0;
    for ( my $i = 1 ; $i <= 100 ; $i++ ) {
       print "r $progress_symbol[$n] $i";
       $n = ( $n= 3 ) ? 0 : $n + 1;
       select( undef, undef, 0.1 );
    }
    print "n";
    local $| = 0;

    ?

    • 方法四

    在原地显示走时的数字时钟,可以加入百分比。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    #!/usr/bin/perl -w
    ?
    local $| = 1;
    for ( 1 .. 10 ) {
       &gettime;
       print "r $now";
       sleep(1);
    }
    print "n";
    local $| = 0;
    exit;
    ?
    sub gettime {
       my ( $sec, $min, $hour, $day, $mon, $year, $weekday, $yeardate,
           $savinglightday )
         = ( localtime(time) );
       $sec  = ( $sec10 )  ? "0$sec"  : $sec;
       $min  = ( $min10 )  ? "0$min"  : $min;
       $hour = ( $hour10 ) ? "0$hour" : $hour;
       $day  = ( $day10 )  ? "0$day"  : $day;
       $mon = ( $mon9 ) ? "0" . ( $mon + 1 ) : ( $mon + 1 );
       $year += 1900;
       $now = "$year.$mon.$day $hour:$min:$sec";
    }
    • 方法五

    有百分比,有不断增长的进度条。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    #!/usr/bin/perl -w
    ?
    $n = 10;
    for ( $i = 1 ; $i= $n ; $i++ ) {
       proc_bar( $i, $n );
       select( undef, 0.2 );
    }
    print "n";
    ?
    sub proc_bar {
       local $| = 1;
       my $i = $_[0] || return 0;
       my $n = $_[1] || return 0;
       print "r [ "
         . ( "32" x int( ( $i / $n ) * 50 ) )
         . ( " " x ( 50 - int( ( $i / $n ) * 50 ) ) ) . " ] ";
       printf( "%2.1f %%", $i / $n * 100 );
       local $| = 0;
    }
    • 方法六

    (自己比较喜欢的一种,推荐!)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    #!/usr/bin/perl -w
    $n = 27;
    for ( $i = 1 ; $i= $n ; $i++ ) {
        proc_bar( $i, $n );
        select( undef, 0.2 );
    }
    print "n";
    ?
    sub proc_bar {
        local $| = 1;
        my $i = $_[0] || return 0;
        my $n = $_[1] || return 0;
        print "r33[36m[33[33m"
          . ( "|" x int( ( $i / $n ) * 50 ) )
          . ( " " x ( 50 - int( ( $i / $n ) * 50 ) ) )
          . "33[36m]";
        printf( "%2.1f%%33[0m", $i / $n * 100 );
        local $| = 0;
    }
    • 方法七

    (自己比较喜欢的一种,推荐!)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    
    #!/usr/bin/perl -w
    # Author: Zhao
    # Date: 2009.12.22
    # Purpose: simulate the progress bar
    # Update: 2009.12.23
    # add color output
    # Update: 2010.5.22
    # calculate remaining time
    ?
    $n = 1000;
    for ( $i = 1 ; $i= $n ; $i++ ) {
        proc_bar( $i, 0.01 );
    }
    print "n";
    ?
    sub proc_bar {
        local $| = 1;
        my ( $i, $n ) = @_;
        my $rt = '';
        my ( $num, $a, $b, @k, @v );
        if ( $i % 50 == 1 ) { $ts_global{$i} = time; }
        $num = scalar keys %ts_global;
        if ( $num= 5 ) {
            delete $ts_global{ $i - 250 };
            @k = keys %ts_global;
            @v = values %ts_global;
            ( $b, $a ) = best_line( @k, @v );
            $rt = int( $b * $n + $a - time + 1 ) . "s   ";
        }
        print "r33[36m[33[33m"
          . ( "#" x int( ( $i / $n ) * 50 ) )
          . ( " " x ( 50 - int( ( $i / $n ) * 50 ) ) )
          . "33[36m]";
        printf( "%2.1f%%33[0m", $i / $n * 100 );
        print " $rt";
    }
    ?
    sub best_line {
        my ( $array1ref, $array2ref ) = @_;
        my ( $i, $product, $sum1, $sum2, $sum1_squares, $b );
        for ( $i = 0 ; $i@$array1ref ; $i++ ) {
            $product      += $array1ref-[$i] * $array2ref-[$i];
            $sum1         += $array1ref-[$i];
            $sum1_squares += $array1ref-[$i]**2;
            $sum2         += $array2ref-[$i];
        }
        $b =
          ( ( @$array1ref * $product ) - ( $sum1 * $sum2 ) ) /
          ( ( @$array1ref * $sum1_squares ) - ( $sum1**2 ) );
        $a = ( $sum2 - $b * $sum1 ) / @$array1ref;
        return ( $b, $a );
    }
    • 方法八

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    
    #!/usr/bin/perl
    ?
    use strict;
    ?
    local $| = 1;
    ?
    my ( $min_prod_id, $max_prod_id );
    print "Product ID Range (min max): ";
    my $input =STDIN;
    if ( $input =~ /(d+)s+(d+)/ ) {
        if ( $1= $2 ) {
            ( $min_prod_id, $max_prod_id ) = ( $1, $2 );
        }
        else {
            ( $min_prod_id, $max_prod_id ) = ( $2, $1 );
        }
    }
    else {
        &fatal_error("Error input!");
    }
    ?
    foreach my $prod_id ( $min_prod_id .. $max_prod_id ) {
        my $pulse = int(
            ( $prod_id - $min_prod_id ) / ( $max_prod_id - $min_prod_id ) * 100 );
        printf( "Progressing: [%-50s] %d%%r", "#" x int( $pulse / 2 ), $pulse );
    ?
        #push(@prod_details,[$prod_id,$prod_name,$prod_price,$prod_saleprice]);
    }
    ?
    print "n";
    ?
    sub fatal_error {
        my ($msg) = @_;
        warn "$msgn";
        print "nPlease Enter to exit...";
        ;
        exit(1);
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读