#!/usr/bin/perl # Perl V Problems V & VI # CSHL 2013 PFB #################### # Hashes from a file ####################### use warnings; use strict; ## Problem V my $fasta = shift @ARGV; my %sequences; # Empty hash, will store fasta sequences from file my $c = 0; # seq counter my $seq = ''; my $k; open(FAS,'<',$fasta) or die "Can't open $fasta: $!\n"; while(my $line = ) { chomp $line; if ($line =~ m/^>/) { $c++; if($c > 1) { $sequences{$k} = $seq; $seq = ''; } $k = $line; } else { $seq .= $line; } } $sequences{$k} = $seq; close(FAS); ## Problem VI # Check the contents of the hash foreach my $key (keys %sequences) { print "$key:$sequences{$key}\n"; } print "\n\n\n\n"; # Sort first foreach my $key (sort keys %sequences) { print "$key:$sequences{$key}\n"; } print "\n\n\n\n"; # Sort numeric, small to large by value foreach my $key (sort { length($sequences{$a}) <=> length($sequences{$b})} keys %sequences) { print "$key:$sequences{$key}\t",length($sequences{$key}),"\n"; } print "\n\n\n\n"; # Sort numeric, large to small by value foreach my $key (sort {length($sequences{$b}) <=> length($sequences{$a}) }keys %sequences) { print "$key:$sequences{$key}\t",length($sequences{$key}),"\n"; }