English 中文(简体)
缩略语
原标题:read file as a set of lines in Perl
  • 时间:2023-08-03 05:24:24
  •  标签:
  • perl

我比Perl更熟悉Sharma,这样一米,试图把这颗 Python鱼 equivalent成象:

lines = {line.rstrip() for line in open( file.txt )}  # method 1
lines = set(map(str.rstrip, open( file.txt )))  # method 2, same result

该代码为<>t,与Perl的h一样,只有钥匙(无价值)。 上述法典是一幅独一无二的,因为它漏掉了档案处理,但最常用的甲醇使用参考书记是用来收集垃圾的收集器,因此该泄漏被清除。

我知道Perl对契约守则有名声,因此希望能够以某种相当容易的方式做类似的事情。 我迄今所发现的最好情况类似。

my %lines;
open(FILE, "file.txt") or die "couldn t open file.txt";
while ( <FILE> ) {
 chomp;
 $lines{$_}=1;
}
close(FILE);

如果不诉诸国家空间活动委员会,这是否越是最好? 我希望像这样做一些事情,但可以说明如何做。

my @lines = <open( file.txt )>; # treat open as expression that returns a file handle
# Perl uses ref counting GC too, I think, so file is now closed
my %lines;
@lines{@lines} = 1; # I think this works??
问题回答

@ikegami在我没有理由之前在评论中展示了我所做的事情。 可在以下方案内填写@ARGV:

local @ARGV = ( ... );
my %hash = map { s/s+z//r, 1 } <>;

你们说,你想要的是一条路,但是这并不特别:

sub read_lines {
    local @ARGV = @_;
    my %hash = map { s/s+z//r, 1 } <>;
    return \%hash;
    }

但是,你不必用@ARGV around。 I m 并非特别节省打字:

sub read_lines {
    my %hash;
    foreach my $file ( @_ ) {
        open my $fh,  <:encoding(...) , $file or next; #maybe warn?
        $hash{ s/s+z//r }++ while <$fh>; 
        }
    return \%hash;
    }

我认为,你重新寻找一些trick,但你基本上必须采取一切步骤。 而且,人们记得,Perl公司对契约法典的声誉追溯到C是替代物的那一天。 这场暴徒现在已经存在,也是一种受欢迎的高语言,这意味着“完整”只是相对紧凑的。 同样,正如一项说明一样,Perl实际上在这方面倒退了,因为我们在通常的设置中增加了各种碎块。 单人仍然很少,但你不喜欢@ikegami的建议:

use File::Slurper qw( read_lines );

my %set = map { $_ => 1 } f或 read_lines( "file.txt" );

use File::Slurper qw( read_lines );

my %set; ++$set{ $_ } f或 read_lines( "file.txt" );

虽然现有的答案是“每一cl”,但阅读作为指挥线参数的档案线的标准内容则简单明了:

while (<>) {
    print $_;  # line including the EOL (use chomp to remove)
}

So to add the line as key to a hash, you d use $hash{$_} = "whatever you want";. If you opened the file handle as $fh, you d use <$fh>instead.

另见“man perlfaq5”。

#!/usr/bin/perl
# there s a quick way to read a file as an array
open(FILE, "<", "file.txt") or die("couldn t open file $fn");
my @lines = <FILE>;
close(FILE);</code></pre>




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签