【问题标题】:Loop through file in perl and remove strings with less than 4 characters在perl中循环文件并删除少于4个字符的字符串
【发布时间】:2014-11-19 05:25:12
【问题描述】:

我正在尝试通过它进行文件循环并删除其中少于四个字符的所有字符串,然后打印列表。我来自 javascript 世界,perl 对我来说是全新的。

use strict; 
use warnings; 


sub lessThan4 { 
  open( FILE, "<names.txt" ); 
  my @LINES = <FILE>; 
  close( FILE ); 
  open( FILE, ">names.txt" ); 

  foreach my $LINE ( @LINES ) { 
    print FILE $LINE unless ( $LINE.length() < 4 ); 
  } 

  close( FILE ); 
} 

【问题讨论】:

  • 行长还是字符串?一条线的线长是perl -ne 'print unless length &gt; 4;' names.txt ...

标签: perl


【解决方案1】:
use strict; 
use warnings;
# automatically throw exception if open() fails
use autodie;

sub lessThan4 {

  my @LINES = do {
    # modern perl uses lexical, and three arg open
    open(my $FILE, "<", "names.txt"); 
    <$FILE>; 
  };
  # remove newlines
  chomp(@LINES);

  open(my $FILE, ">", "names.txt"); 

  foreach my $LINE ( @LINES ) { 
    print $FILE "$LINE\n" unless length($LINE) < 4;
    # possible alternative to 'unless'
    # print $FILE "$LINE\n" if length($LINE) >= 4;
  }

  close($FILE); 
}

【讨论】:

  • 单行使用就地编辑,perl -i -lne 'length &lt;4 or print' names.txt
【解决方案2】:

你基本上就在那里。我希望您会发现代码中的一些 cmets 很有用。

# Well done for including these. So many new Perl users don't
use strict; 
use warnings; 

# Perl programs traditionally use all lower-case subroutine names
sub lessThan4 { 
  # 1/ You should use lexical variables for filehandles
  # 2/ You should use the three-argument version of open()
  # 3/ You should always check the return value from open()
  open( FILE, "<names.txt" ); 

  # Upper-case variable names in Perl are assumed to be global variables.
  # This is a lexical variable, so name it using lower case.
  my @LINES = <FILE>; 
  close( FILE ); 

  # Same problems with open() here.
  open( FILE, ">names.txt" ); 

  foreach my $LINE ( @LINES ) { 
    # This is your biggest problem. Perl doesn't yet embrace the idea of
    # calling methods to get properties of a variable. You need to call
    # length() as a function.
    print FILE $LINE unless ( $LINE.length() < 4 ); 
  } 

  close( FILE ); 
} 

重写以考虑所有这些,我们得到以下结果:

use strict; 
use warnings; 

sub less_than_4 {
  open( my $in_file_h, '<', 'names.txt' ) or die "Can't open file: $!"; 
  my @lines = <$in_file_h>; 
  close( $in_file_h ); 
  open( my $out_file_h, '>', 'names.txt' ) or die "Can't open file: $!";

  foreach my $line ( @lines ) { 
    # Note: $line will include the newline character, so you might need
    # to increase 4 to 5 here
    print $out_file_h $line unless length $line < 4; 
  } 

  close( $out_file_h ); 
}

【讨论】:

  • 在使用 Stackoverflow 的 5 年里,我从未收到过如此贴心的回复。感谢您为 perl 新手提供的 cmets。
  • Dave Cross 上面没有打印任何东西。我确实在文件顶部调用了 less_than_4() 方法。
  • 看起来上次打印的东西丢失了。
  • 是的。我已经编辑了答案以删除我在unless 语句末尾留下的额外括号。
【解决方案3】:

我试图通过它引入一个文件循环并删除其中包含少于四个字符的所有字符串,然后打印列表

我想您需要从文件中删除长度小于 4 个字符的字符串。

#!/usr/bin/perl

use strict;
use warnings;

open ($FH, "<", "names.txt");
my @final_list;

while (my $line = <$FH>) {

    map {
        length($_) > 4 and push (@final_list, $_) ;
    } split (/\s/, $line);

}

print "\nWords with more than 4 chars: @final_list\n";

【讨论】:

  • push @final_list, grep { length($_) &gt;4 } split (/\s/, $line); 而不是void context 中的map
  • 感谢您的建议
【解决方案4】:
#Please try this one:
use strict; 
use warnings; 
my @new;
while(<DATA>)
{

#Push all the values less than 4 characters

    push(@new, $_) unless(length($_) > '4');
}
print @new;

__DATA__
Williams
John
Joe
Lee
Albert
Francis
Sun

【讨论】:

  • ... 删除 少于四个字符的任何字符串
  • @RobEarl:我更新了 '>' 而不是 '
  • 您正在删除 超过 4 个字符的任何内容
猜你喜欢
  • 1970-01-01
  • 2014-07-17
  • 2022-07-22
  • 2019-12-10
  • 2021-01-11
  • 2020-01-10
  • 2011-05-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多