Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1515523
  • 博文数量: 289
  • 博客积分: 11086
  • 博客等级: 上将
  • 技术积分: 3291
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-22 17:06
个人简介

徐小玉的博客。

文章分类

全部博文(289)

文章存档

2023年(6)

2022年(1)

2021年(2)

2020年(9)

2019年(9)

2018年(6)

2017年(10)

2016年(10)

2014年(3)

2013年(4)

2011年(12)

2010年(16)

2009年(14)

2008年(119)

2007年(48)

2006年(20)

我的朋友

分类:

2008-02-28 11:46:55

 1:  得到一个文件的行数:
 
     一种方法:
       my @file = ;
       my $count = @file;
 
      在命令行下可以:
      perl -ne 'print $. if eof' filename;
 
    使用awk  获得文件的行数的方法:
    my $tmp = `ls -l BALANCE012008.dat|awk '{printf "%10d",  \$5/77 }'`; 
 
     利用 stat命令:
      @totalsize=stat(FILE);
      $size = $totalsize[7];  #文件大小
     chomp($size); 
       #  my @file_b = ;
       #  my $count = @file_b;   # 文件行数
        my   $len =` head -n 1 $file |awk '{print length(\$0)}'  `;
       my $length =  $len + 1; 
      #  采用文件大小/行数的方法,得到文件的行数   print "length :$length \n";
 
 2: 得到一个目录下的所有文件的方法:
 
    1): perl 方式: 
 
       my $DIR_PATH="/usr/bin";
       opendir DIR, ${DIR_PATH} or die "Can not open \"$DIR_PATH\"\n";
       my @filelist = readdir DIR;
      foreach $filedir(@filelist) {
        print $DIR_PATH."/".$filedir."\n";
     2) 利用ls 结合split命令:
 
     my $file_a = `ls`;
     my @array = split(/\n/, $file_a);
     foreach (@array){ 
     print ;
     print "\n";
     }
 
 3:  得到系统时间:
      my $a = localtime(time);
      print  LOGFILE "the start time is : $a\n"
 
      或者:
     可以按照你的需要读取相应的变量的值就可以了,函数格式如下:  
  ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);  
 
4: 打印出hash里的所有数据
   一种方法:
     while (($key ,$value)==each(%hash))
   {
     print "$key   $value \n";
    }
  另一种方法:
   foreach $key(keys(%hash))
  {
    print "$key = $hash{$key}";
  }
another:
my ($key, $value);
while (($key, $value) = each %hash){
print "$key => $value\n";
}
 
 
5:  goto语句的使用
     AARONVOX:
      while (1)
     {
        # do something
       my $xxx = <>;
       goto AARONVOX if $xxx =~ /Hello/;
     }
6:  随机数的生成:
    my $data=int(rand(50));
7:对数据库的操作:
my $dbh = DBI->connect("dbi:DB2:sample", "DB2ADMIN", "db2admin", {RaiseError => 1});      # 连接数据库
# use VALUES to retrieve value from special register
  my $stmt = "Values CURRENT DATE";  # sql串
  my $sth = $dbh->prepare($stmt);
  $sth->execute();
# associate variables with output columns...
  my $col1;
  $sth->bind_col(1,\$col1);
  while ($sth->fetch) { print "Today is: $col1\n"; }
  $sth->finish();
  $dbh->disconnect();
 
 
 其中bind_col得到具体的某列的值。
 如果想得到所有的列,采用函数bind_columns 。  它将返回所有的列。
 $sth->bind_columns(\$col1,\$col2,\$col3,\$col4,\$col5,\$col6,\$col7,\$col8);
 while ($sth->fetch){    
    print "$col1,$col2,$col3,$col4,$col5,$col6,$col7,$col8\n";
 }
 
hash与array排序问题:
 
 hash:
print "\n\tPoint SORTED BY point:\n";
foreach $key (reverse sort (keys(%hash))) {
  print "\t\t$key \t\t$hash{$key}\n";
}
 
array:
my @array2 = reverse sort(@array) ;
 
 
8:书写存在多个分支的编程技巧
 
my $size =
($width < 10 ) ? “small”:
($width < 20) ? “medium”:
($width < 50) ? “large”:
“extra_large”; #default
 
 
9:  删除所有文件的方法   (把所有.old 结尾的换正.new结尾)
 
  foreach my $file (glob "*.old") {
      my $newfile = $file;
      $newfile =~ s/\.old$/.new/;
      if (-e $newfile) {
        warn "can't rename $file to $newfile: $newfile exists\n";
      } elsif (rename $file, $newfile) {
        ## success, do nothing
      } else {
        warn "rename $file to $newfile failed: $!\n";
      }
    }
 
 
 
阅读(1021) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~