Feeds:
文章
留言

Archive for the ‘Perl’ Category

很簡單, 可是不常用就會忘記,
應該要統一到utf8才好啊…
M$的big5好討厭啊…

(繼續閱讀…)

廣告

Read Full Post »

Sometimes, if you put a print statement inside of a loop that runs really really quickly, you won’t see the output of your print statement until the program terminates; sometimes, you don’t even see the output at all.

The solution to this problem is to “flush” the output buffer after each print statement; this can be performed in perl with the following command:

(繼續閱讀…)

Read Full Post »

不定時持續更新中…

1. 數字格式化
目的:

1234567 -> 1,234,567

範例:

#!/usr/local/bin/perl
$myint = “1234567″;
while ($myint =~ s/(\d)(\d\d\d)(?!\d)/$1,$2/) {};

補充說明:
(?!\d)表示出現非數字才可以使用, 如此一來可保證總是抓右邊的4個數字來做格式化

Read Full Post »

參考自http://bigsnakecat.blogspot.com/2007/08/perl-screen-wrapper.html

#!/usr/bin/perl -w
#
# A wrapper for screen.
#

use strict ;

if (system("which screen > /dev/null") == 0) {
  # screen is found
  my $screen_command = `which screen` ;
  chomp $screen_command ;

  if ($#ARGV != -1) {
    exec "$screen_command" , @ARGV ;
  } else {
    my @sock_list = `$screen_command -ls` ;
    @sock_list = grep { s/^\s*// ; m/tached/ } @sock_list ;
    if ($#sock_list == -1) {
      exec "$screen_command"
    } elsif ($#sock_list == 0) {
      exec "$screen_command -x"
    } else {
      print "There are ",$#sock_list+1," screens. Please choose one to attache.\n\n";
      for(my $i=0; $i <= $#sock_list; $i++) {
        print " ",$i+1,"\t",$sock_list[$i];
      }
      print "\nPress ENTER to choose the first one for default.\n" ;
      print "Your choice: ";
      while (my $choice = ) {
        chomp $choice;
        if ($choice eq '') {$choice = 1};
        if (($choice =~ m/\d+/) && ($choice >=1 ) && ($choice <= $#sock_list+1)) {
          $sock_list[$choice-1] =~ m/^(\S+).*$/ ;
          exec $screen_command." -x $1";
        } else {
          print "Please choose from [1-",$#sock_list+1,"] : ";
        }
      }
    }
  }
}
else {
  print STDERR "Error: screen not found. Have you installed screen?\n" ;
  exit -1;
}

Read Full Post »