On  8 Mar, E.Horn wrote:

> Hello!
> I want to read this file into an array.
> How can i just get 4, 5, 6,7,8 into an array?
> And later, how can i get the contents out of this array?
> 1)            2)        3)       4)      5)                6)
> 7)            8)           9)    10)                11)
> ATOM   2909  CG1   VAL B 183       1.130  28.458 104.360  1.00
> 23.04           C
> ATOM   2910  CG2   VAL B 183       0.996  29.769 102.236  1.00
> 24.61           C
> ATOM   2911  N       THR B 184       3.313  31.739 103.453  1.00
> 25.98           N
> ATOM   2912  CA     THR B 184       3.261  33.012 104.149  1.00
> 29.31           C
> ATOM   2913  C       THR B 184       1.911  33.642 103.859  1.00
> 29.26           C
> ATOM   2914  O       THR B 184       1.442  33.626 102.720  1.00
> 28.75           O
> regards

#! /usr/bin/perl

use strict;
use warnings;

# Each element in an interpolated array
# is followed by a trailing newline
$" = "\n";

my $file = '/path/to/file';

open(FH, $file) or die "Couldn't open $file: $!\n";
while (my $line = <FH>) {
    # Remove all whitespaces (and trailing newline) 
    # and insert remaining items in @arr
    my @arr = split /\s+/, $line;
    # Remove first of first three and last two elements
    splice(@arr, 0, 3) && splice(@arr, -2, $#arr);
    # Separate 'VAL B' into array
    my @tmp = splice(@arr, 0, 2);
    my $tmp;
    # @tmp = qw(VAL B) becomes string 'VAL B'
    $tmp .= "$_ " for @tmp;
    # Remove trailing space and insert at front of @arr
    chop($tmp) && unshift(@arr, $tmp);
    # Print each element in @arr
    print "@arr\n\n";
}
close(FH);



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to