在Perl程序中显示进度条之多姿多彩的自写代码
生活随笔
收集整理的這篇文章主要介紹了
在Perl程序中显示进度条之多姿多彩的自写代码
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在Perl程序中顯示進(jìn)度條之多姿多彩的自寫代碼
yixf 于 2011-11-29,09:57??-
方法一
| 1 2 3 4 5 6 7 8 | #!/usr/bin/perlmy $max = 10; for ( 1 .. $max ) {my $percent = $_ / $max * 100;print "$_ - $percent % OK!\n";sleep(1); } |
-
方法二
從“…..”的長度來判斷進(jìn)度。
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); } -
方法三
用滾輪在原地顯示進(jìn)度,當(dāng)然也可以加入百分比。
1 2 3 4 5 6 7 8 9 10 11 12 #!/usr/bin/perl -wlocal $| = 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, undef, 0.1 ); } print "\n"; local $| = 0; -
方法四
在原地顯示走時(shí)的數(shù)字時(shí)鐘,可以加入百分比。
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 -wlocal $| = 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"; } -
方法五
有百分比,有不斷增長的進(jìn)度條。
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, undef, 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, undef, 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, $n );select( undef, undef, undef, 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, $a, $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/perluse 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); } -
總結(jié)
以上是生活随笔為你收集整理的在Perl程序中显示进度条之多姿多彩的自写代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 期货连续合约是什么意思
- 下一篇: 两个合伙人股份如何分配 按投资出资占比分