$i=0; while ($line=) { chop($line); # use "chomp" here, not "chop" ... } #### my_script.pl file1 file2 #### #!/usr/bin/perl -w use strict; # painful at first, maybe, but worth it # using command line args is good (and easy): die "Usage: $0 solvent.file structure.file\n" unless ( @ARGV == 2 and -f $ARGV[0] and -f $ARGV[1] ); open( PDB, ">Solvent.pdb" ) or die "...:$!\n"; open( TXT, ">OverL.txt" ) or die "...:$!\n"; my %input; my %atmnb; # declare storage near where you need it my %coord; my %natms; for my $file ( @ARGV ) { open( IN, $file ) or die "open failed on $file: $!\n"; while () { # use a slice on split to get just the needed data: my ($atb,$x,$y,$z,$rad) = (split)[1,5,6,7,9]; push @{$input{$file}}, $_; # keep full input line push @{$atmnb{$file}}, $atb; push @{$coord{$file}}, [ $x, $y, $z, $rad ]; } close IN; print "Number of atoms in $file is $.\n"; $natms{$file} = $.; } my ($f1,$f2) = @ARGV; for my $i ( 0 .. $natms{$f1}-1 ) { for my $j ( 0 .. $natms{$f2}-1 ) { # use map to compute the x,y,z diffs just once each: my @difs = map { $coord{$f1}[$i][$_] - $coord{$f2}[$j][$_] } ( 0, 1, 2 ); my $dist = sqrt( $difs[0] * $difs[0] + $difs[1] * $difs[1] + $difs[2] * $difs[2] ); my $rsum = $coord{$f1}[$i][3] + $coord{$f2}[$j][3]; if ( $dist < $rsum ) { print TXT "Overlap for $atmnb{$f1}[$i] and $atmnb{$f2}[$j]\n"; } else { print PDB "$input{$f1}[$i]"; } } } #### for my $i ( 0 .. $natms{$f1}-1 ) { my $xyz = @{$coord{$f1}[$i]}; my $rad = pop @xyz; for my $j ( 0 .. $natms{$f2}-1 ) { # use map to compute the x,y,z diffs just once each: my @difs = map { $xyz[$_] - $coord{$f2}[$j][$_] } ( 0, 1, 2 ); my $dist = sqrt( $difs[0] * $difs[0] + $difs[1] * $difs[1] + $difs[2] * $difs[2] ); my $rsum = $rad + $coord{$f2}[$j][3]; if ( $dist < $rsum ) { print TXT "Overlap for $atmnb{$f1}[$i] and $atmnb{$f2}[$j]\n"; } else { print PDB $input{$f1}[$i]; } } }