in reply to Creating hash from data extracted from text file in fasta format

G'day reebee3,

To read lines from a file on the command line, you can just do this:

while (<>) { # $_ holds the line read - process it here }

In your foreach loop, you can get each key in the sorted order you want by changing

... (keys ...

to

... (sort {length $sequences{$a} <=> length $sequences{$b} } keys ...

With this input file:

$ cat pm_1144803_fasta.txt >SequenceID|1234_Gene1 CTTTTAAGCTGATTAGGCTTTTATACCATTAGATTTAGTAACTATTGTCTTTTAA >SequenceID|9876_Gene2 GTGCTGTCTTAAGTTGAACAGAGTGTGGGAGGAAATATAAGCAAAGTTATTCCGTAGAATT >SequenceID|4567_Gene3 CATCCTCCTTTACACCCCACAAACATTTGGCAACCCCTGATAGGTTTCTTTCTTGTGGA

and this script:

$ cat pm_1144803_fasta_seq_len_sort.pl #!/usr/bin/env perl use strict; use warnings; my (%sequences, $seq_key); while (<>) { chomp; if (/^>/) { $seq_key = substr $_, 1; } else { $sequences{$seq_key} = $_; } } foreach my $key ( sort {length $sequences{$a} <=> length $sequences{$b} } keys %sequ +ences) { my $len = length ($sequences{$key}); print "$key:$len\n"; }

You can run this from the command line:

$ pm_1144803_fasta_seq_len_sort.pl pm_1144803_fasta.txt SequenceID|1234_Gene1:55 SequenceID|4567_Gene3:59 SequenceID|9876_Gene2:61

— Ken