注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Mr.Right

不顾一切的去想,于是我们有了梦想。脚踏实地的去做,于是梦想成了现实。

 
 
 

日志

 
 
关于我

人生一年又一年,只要每年都有所积累,有所成长,都有那么一次自己认为满意的花开时刻就好。即使一时不顺,也要敞开胸怀。生命的荣枯并不是简单的重复,一时的得失不是成败的尺度。花开不是荣耀,而是一个美丽的结束,花谢也不是耻辱,而是一个低调的开始。

网易考拉推荐

MATLAB Perl去掉文件的首尾行/Perl入门范例  

2015-10-02 21:29:41|  分类: 学习 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
即以此功德,庄严佛净土。上报四重恩,下济三途苦。惟愿见闻者,悉发菩提心。在世富贵全,往生极乐国。

#! /usr/bin/perl -w
# usage in MATLAB: perl('demo.pl')

# (1) subroutine
sub marin{
  $n += 1;
  print "Hello,$n";
 }
 &marin;#调用函数marin=呼叫子程序
 &marin;#调用函数marin=呼叫子程序

$maximum = &max(3, 5, 10, 4, 6);
print "\nMax:", $maximum, "\n";

sub max {
    my($max_so_far) = shift @_;  # the first one is the largest yet seen
    foreach (@_) {               # look at the remaining arguments
        if ($_ > $max_so_far) {  # could this one be bigger yet?
            $max_so_far = $_;
        }
    }
    $max_so_far;
}

%myGrades = ( 
'name' => 'Bob',  # Using the => symbol between each key / value pair
'test1' => 89, 
'test2' => 76, 
'test3' => 0); 
$average = ($myGrades{'test1'} + $myGrades{'test2'} + $myGrades{'test3'}) / 3;
$average = int($average + .5); 
print "$myGrades{'name'}'s Current Average: $average\n";
$myGrades{'test3'} = 100; 
$average = ($myGrades{'test1'} + $myGrades{'test2'} + $myGrades{'test3'}) / 3; 
$average = int($average + .5);
print "$myGrades{'name'}'s Corrected Average: $average\n";

# (2) Writing files
$str = "Namo Amitabha!\n";
open FILE, ">", "filename.txt" or die $!;  # $! denotes the human-readable complaint from the system
print FILE $str;
close FILE or die $!;

$str1 = "Namo Ksitigarbha!\n";
open FILE, "+>>", "filename.txt" or die $!; # operand +>> denotes "read/append"
print FILE $str1; # write
close FILE or die $!;

# (3) read a binary file
open FILE, "<", "filename.txt" or die $!;
binmode FILE;
my ($buf, $data, $n);
while (($n = read FILE, $data, 4) != 0) { # read FILEHANDLE, SCALAR, LENGTH, OFFSET
  print "$n bytes read\n"; # The return value of read is the number of characters actually read
  $buf .= $data;
}
close(FILE);

# (4) read a text file line-by-line
open (CHECKBOOK, "<", "filename.txt") || die "couldn't open the file!";;
$n = 0;
while ($record = <CHECKBOOK>) { #  The "<>" symbol is called the line reading operator, 
   print $record;      # and in this example we've put the checkbook file handle in the line reading operator, indicating that we'd like to read a line from the checkbook file.
   $n += 1;
   print $n, "\n";
}
# an alternative method, 
print "Before seek, Current byte:", tell(CHECKBOOK); # tell() Returns the current position in bytes for FILEHANDLE, or -1 on error.
seek(CHECKBOOK, 0, 0); # Move file position indicator to beginning of open file
print "\n After seek, Current byte:", tell(CHECKBOOK), "\n";
while (<CHECKBOOK>){ # actually  while ($_ = <CHECKBOOK>)
    print; # actually print $_
}
close(CHECKBOOK);

# (5) read a text file holistically
print  "\nread a text file holistically\n";
open (CHECKBOOK, "<", "filename.txt") || die "couldn't open the file!";;
@linelist=<CHECKBOOK>;
foreach $eachline(@linelist){
    print $eachline;
}
close(CHECKBOOK);

# (6) delete the last line of file
# 1. ========== prepare some data in the file ============
$str = "Namo Amitabha!\n";
open FILE, "+>", "filename.txt" or die $!;  # $! denotes the human-readable complaint from the system
print FILE $str;
for (my $i=10; $i <= 19; $i++) {
   if($i == 13) { last;} # last in Perl has the same effect as break in C
   print FILE "$i\n";
}
foreach my $i (0..5) {
  print FILE "$i\n";
  print FILE "$i\n"; # duplicated lines
}
seek(FILE, 0, 0); # Move file position indicator to beginning of open file
print "\nAfter seek, Current byte:", tell(FILE), "\n";
while (<FILE>){ 
    print;
}
close FILE or die $!;
# 2. ========== delete the 1ast line of a file ============
$file = "filename.txt";
if (-e $file) { print "File Exists! \n"; } # the -e (exists) statement in a conditional block so that the print statement only gets called if the file exists.
unless (-e $file) { print "File Doesn't Exist! \n"; } # test for the opposite - that the file does not exist - by using the unless conditional
#---delete the last line of a file -----------------
open (FILE, "+< $file") or die "can't update $file: $!";
while ( <FILE> ) {
    $addr = tell(FILE) unless eof(FILE);
}
truncate(FILE, $addr) or die "can't truncate $file: $!";
#-----------------------------

# %% output the file
print "\nthe last line deleted file\n";
seek(FILE, 0, 0); # Move file position indicator to beginning of open file

# == skip the first line ====
# readline FILE; # skip the first line, the same as <FILE>;

while (<FILE>){ # actually  while ($_ = <FILE>)
   print; # actually print $_
}
close FILE or die $!;


# (7) delete the first line of file
$fileSource = "filename.txt";
$fileDestination = "skip1stLine.txt";
open (hSourceFile, "< $fileSource") or die "can't read $file: $!";
open (hDestinationFile, "+> $fileDestination") or die "can't write $file: $!";

readline hSourceFile; # skip the first line, the same as <FILE>;
while (<hSourceFile>){ # actually  while ($_ = <hSourceFile>)
   print hDestinationFile; # actually print hDestinationFile $_
}

# %% output the file
seek(hDestinationFile, 0, 0); # Move file position indicator to beginning of open file
print "\nthe first line deleted file\n";
while (<hDestinationFile>){ 
   print; # actually print $_
}
close hSourceFile or die $!;
close hDestinationFile or die $!;

# (8) delete duplicated lines of file
$fileSource = "filename.txt";
$fileDestination = "uniqueLine.txt";
open (hSourceFile, "< $fileSource") or die "can't read $file: $!";
open (hDestinationFile, "+> $fileDestination") or die "can't write $file: $!";

$temp = "";
while (<hSourceFile>){ # actually  while ($_ = <hSourceFile>)
    if(not $temp eq $_){
        print hDestinationFile;
        $temp = $_;
    }
}

# %% output the file
seek(hDestinationFile, 0, 0); # Move file position indicator to beginning of open file
print "\nDuplicated lines deleted file\n";
while (<hDestinationFile>){ 
   print; # actually print $_
}
close hSourceFile or die $!;
close hDestinationFile or die $!;


  评论这张
 
阅读(228)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016