tags:

views:

167

answers:

7

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

Test
Foo
Bar

But I want to put each line in a array and print the lines like this:

line1 line2 line3

But how can I do this?

+2  A: 

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
Tamás Mezei
There is shortcut for chomp: `chomp(@lines);`
Ivan Nevostruev
Oh, I forgot that. Thanks for the tip, Ivan!
Tamás Mezei
+7  A: 
#!/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;
Corey
+1 for strictures, lexical filehandles and the 3-arg `open`. :)
Ether
You should check if file was opened successfully. Always. Or `use autodie;`
Ivan Nevostruev
good point. fixed!
Corey
+1  A: 

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);
Mark Byers
+4  A: 

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
Ivan Nevostruev
`perl -le 'chomp(@a=<>); print "@a"' file ..`
Greg Bacon
@gbacon: Thanks for shorter version
Ivan Nevostruev
+1  A: 

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";
toolic
A: 

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";
    }
'
Brad Gilbert
A: 

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

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

run by : scirpt.pl [your file]

dan