12月 09

在Perl中高效sort——Orcish算法

The Orcish Maneuver (invented by Joseph N. Hall [8]) eliminates the preprocessing pass over the data, which might save keeping a copy of the data if they are being read directly from a file. It does the sortkey extraction only once per record, as it checks the hash to see if it was done before. The test and storage of the sortkey is done with the ||= operator (short-circuit or-assignment), which will evaluate and assign the expression on the right to the lvalue on the left, if the lvalue is false. The name “orcish” is a pun on “or-cache”. The full statement in the sortsub looks like this:……【阅读全文】

11月 29

shell中的bash与dash

Linux中的shell有多种类型,其中最常用的几种是Bourne   shell(sh)、C   shell(csh)和Korn   shell(ksh)。三种shell各有优缺点。Bourne   shell是UNIX最初使用的shell,并且在每种UNIX上都可以使用。Bourne   shell在shell编程方面相当优秀,但在处理与用户的交互方面做得不如其他几种shell。Linux操作系统缺省的shell是Bourne   Again   shell,它是Bourne   shell的扩展,简称Bash,与Bourne   shell完全向后兼容,并且在Bourne   shell的基础上增加、增强了很多特性。Bash放在/bin/bash中,它有许多特色,可以提供如命令补全、命令编辑和命令历史表等功能,它还包含 了很多C   shell和Korn   shell中的优点,有灵活和强大的编程接口,同时又有很友好的用户界面。……【阅读全文】

11月 29

在Perl中使用Term::ANSIColor模块控制颜色的三种方法

  • 方法一

Term::ANSIColor的函数color有以下参数:
clear, reset, dark, bold, underline, underscore, blink, reverse, concealed, black, red, green, yellow, blue, magenta, cyan, white, on_black, on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, and on_white

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/perl
 
use Term::ANSIColor;
 
print color "red";
print "REDn";
print color "green";
print "GREENn";
print color "bold blue";
print "BOLD BLUEn";
print color "on_yellow";
print "ON YELLOWn";
print color "on_cyan";
print "ON CYANn";
print color "reset";

Continue reading

11月 29

在Shell脚本中显示进度条

注意:如果你使用的是Ubuntu,第一行中的程序路径一定要写/bin/bash而不是/bin/sh;其他版本的两者皆可,因为/bin/sh只是/bin/bash的一个软链接;在Ubuntu中/bin/sh是/bin/dash的软链接。

  • 方法一

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    #!/bin/bash
     
    b=''
    for ((i=0;$i<=100;i+=2))
    do
           printf "progress:[%-50s]%d%%r" $b $i
           sleep 0.1
           b=#$b
    done
    echo

    Continue reading

11月 29

在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, undef, 0.1 );
    }
    print "n";
    local $| = 0;

    Continue reading

    11月 29

    在Perl程序中显示进度条之使用Term::ProgressBar模块

    • A really simple use

    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      
      #!/usr/bin/perl -w
       
      use Term::ProgressBar 2.00;
      use constant MAX =100_0000;
       
      my $progress = Term::ProgressBar-new(MAX);
       
      for ( 0 .. MAX ) {
         my $is_power = 0;
         for ( my $i = 0 ; 2**$i= $_ ; $i++ ) {
             $is_power = 1
               if 2**$i == $_;
         }
       
         if ($is_power) {
             $progress-》update($_);
         }
      }
    • A smoother bar update

    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      
      #!/usr/bin/perl -w
       
      use Term::ProgressBar 2.00;
       
      my $max = 100000;
       
      my $progress = Term::ProgressBar-new($max);
       
      for ( 0 .. $max ) {
         my $is_power = 0;
         for ( my $i = 0 ; 2**$i= $_ ; $i++ ) {
             $is_power = 1
               if 2**$i == $_;
         }
       
         $progress-》update($_);
      }

      Continue reading