在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 = ( $sec 《 10 ) ? "0$sec" : $sec; $min = ( $min 《 10 ) ? "0$min" : $min; $hour = ( $hour 《 10 ) ? "0$hour" : $hour; $day = ( $day 《 10 ) ? "0$day" : $day; $mon = ( $mon 《 9 ) ? "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 "$msg\n"; print "\nPlease Enter to exit..."; ; exit(1); }
-
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。