This is a commented version of your script along with hopefully improved code. I think that will take you further.
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
my $fh1 = "File1.txt";
my $fh2 = "File2.txt";
#> so far, so good. though in a flexible program at least better writt
+en as
#> that way you can call it: perl scriptname file1 file2
# my ($position_file, $sequence_file) = @ARGV;
open(FH, "$fh1") || die ("Can't open: $!");
my @AoH;
#> that's "ok", too. three quirks: - unnecessary quoting. if you mean
+the plain value of a variable
#> there's no need to quote it ("") - The use of a Filehandle. Best pr
+actice are lexical variables for filehandles.
#> - variable names: for sake of you own sanity: use DESCRIPTIVE varia
+ble names. ALWAYS.
#> so:
# open(my $file, '<', $position_file) || die ("Can't open $position_fi
+le: $!");
# my %positions;
while ( <FH> ) {
push @AoH, { split /[\s]+/ };
}
# this will not do what you think. it creates a hashref from the a
+rray split of your positions file
# this will trigger the "Odd number of elements in anonymous hash
+at ashnator.pl line 12, <FH> line 1." warnings
# you want:
# Create a hash of arrays of positions
# while ( <$file> ) {
# my ($key, $pos) = split /\s+/;
# if (!$positions{$key}) {
# #create array at $key for the first position per key
# $positions{$key} = [$pos];
# } else {
# # add position for all subsequent lines
# my $ref = $positions{$key};
# push @$ref,$pos;
# }
# }
my %sequences;
{ #> uneccessary block
open(FD, "$fh2") || die("Cannot open: $!");
my $key;
while (<FD>) {
if ( s/^>// ) {
$key = ( split /\|/ )[1];
}
else {
chomp;
$sequences{$key} .= $_;
}
}
}
#> print Dumper(\%sequences);
#> same critics than above, but this time the loop works and i assume
+you understand how.
for my $href ( @AoH ) {
for my $role ( sort keys %$href ) {
my $key; # why $key when $role is the loop variable?
#> RULE OF THUMB: Think before you copy and paste!
#> because the data structure is now a bit different we make this:
#> while ( my ($key, $position_list) = each %positions )
#> {
#> for my $position ( @$position_list )
#> {
#> print "key: $key, position: $position\n";
#> # ... your inner loop code
}
}
Update: added @ARGV explanation
|