开发者

How can I read the lines of a file into an array in Perl?

开发者 https://www.devze.com 2022-12-14 02:33 出处:网络
I have a file named test.txt that is like this: Test Foo Bar But I want to put each line in a arra开发者_C百科y and print the lines like this:

I have a file named test.txt that is like this:

Test

Foo

Bar

But I want to put each line in a arra开发者_C百科y and print the lines like this:

line1 line2 line3

But how can I do this?


#!/usr/bin/env perl
use strict;
use warnings;

my @array;
open(my $fh, "<", "test.txt")
    or die "Failed to open file: $!\n";
while(<$fh>) { 
    chomp; 
    push @array, $_;
} 
close $fh;

print join " ", @array;


Here is my single liner:

perl -e 'chomp(@a = <>); print join(" ", @a)' test.txt

Explanation:

  • read file by lines into @a array
  • chomp(..) - remove EOL symbols for each line
  • concatenate @a using space as separator
  • print result
  • pass file name as parameter


One more answer for you to choose from:

#!/usr/bin/env perl

open(FILE, "<", "test.txt") or die("Can't open file");
@lines = <FILE>;
close(FILE);
chomp(@lines);
print join(" ", @lines);


If you find yourself slurping files frequently, you could use the File::Slurp module from CPAN:

use strict;
use warnings;
use File::Slurp;

my @lines = read_file('test.txt');
chomp @lines;
print "@lines\n";


The most basic example looks like this:

#!/usr/bin/env perl

use strict;
use warnings;

open(F, "<", "test.txt") or die("Cannot open test.txt: $!\n"); # (1)
my @lines = ();
while(<F>) { chomp; push(@lines, $_); } # (2)
close(F);

print "@lines"; # (3) stringify

(1) is the place where the file is opened.

(2) File handles work nicely within list enviroments (scalar/list environments are defined by the left value), so if you assign an array to a file handle, all the lines are slurped into the array. The lines are delimited (ended) by the value of $/, the input record separator. If you use English;, you can use $IRS or $INPUT_RECORD_SEPARATOR. This value defaults to the newline character \n;

While this seemed to be a nice idea, I've just forgot the fact that if you print all the lines, the ending \n will be printed too. Baaad me.

Originally the code was:

my @lines = <F>;

instead of the while loop. This is still a viable alternative, but you should swap (3) with chomping and then printing/stringifying all the elements:

for (@lines) { chomp; }
print "@lines";

(3) Stringifying means converting an array to a string and inserting the value $" between the array elements. This defaults to a space.

See: the perlvar page.

So the actual 2nd try is:

#!/usr/bin/env perl

use strict;
use warnings;

open(F, "<", "test.txt") or die("Cannot open test.txt: $!\n"); # (1)
my @lines = <F>; # (2)
close(F);
chomp(@lines);

print "@lines"; # (3) stringify


This is the simplest version I could come up with:

perl -l040 -pe';' < test.txt

Which is roughly equivalent to:

perl -pe'
  chomp; $\ = $/; # -l
  $\ = 040;       # -040
'

and:

perl -e'
  LINE:
    while (<>) {
      chomp; $\ = $/; # -l
      $\ = " ";       # -040
    } continue {
      print or die "-p destination: $!\n";
    }
'


This is the code that do this (assume the below code inside script.pl) :

use strict;
use warnings
my @array = <> ;
chomp @array;
print "@array";

It is run by:

scirpt.pl [your file]
0

精彩评论

暂无评论...
验证码 换一张
取 消