Reputation: 633
I would like to extract some columns from a file in Perl. Below is my code:
#!/usr/bin/perl
use strict;
use warnings;
my $file = 'file.txt';
open( my $fh, "<" ,$file) or die $!;
while (<$fh>) {
if (/a/../z/) {
next if /a/ || /z/;
print +(split) [5,6,7], "\n";
}
}
Output of this code:
1ALACA
1ALACB
2THRH
2THRCA
I would like the output instead to read (just adding some more spaces, making it easier to read):
1 ALA CA
1 ALA CB
2 THR H
2 THR CA
Upvotes: 0
Views: 78
Reputation: 29844
You can simply change the Output Field Separator before the loop:
$, = ' ';
Or more explicitly:
use English qw<$OUTPUT_FIELD_SEPARATOR>;
local $OUTPUT_FIELD_SEPARATOR = ' ';
Upvotes: 1
Reputation: 50637
Use spaces to join the columns?
print join(" ", +(split)[5,6,7]), "\n";
Upvotes: 3