Is it possible to put elements of array to hash in perl? -


example of file content:

>random sequence 1 consisting of 500 residues. vilvwrisemnptheiypevsyedrqpfrcfdeginmqmgqkscrncliftrnafaygiv hflewgillthiihcchqiqggcdctrhpvrfypqhrnddvdkpcqtkspmqvrygddsd;  >random sequence 2 consisting of 500 residues. kaaatkkpwadtipyllctfmqtsglewlhtdynnfssvvcvryfeqfwvqcqdhvfvkn knwhqvlweeyavidsmnfawpplyqsvssnldstermmwwwvyyqfedniqirmewcni ysgflsreklelthnkcevcvdkfvrlvfkqtkwvrtmnnrrrvrfrgiyqqtaiqeyhv hqkiirypchvmqfhdpsapcdmtrqgkrmnfcfiiflytlyevkywmhfltylnclehr;  >random sequence 3 consisting of 500 residues. aycscwrihnvvfqkdvvlgywghcwmswgsmnqpfhrqpynkyfcmapdwcnigtyawk 

i need algorithm build hash $hash{$key} = $value; lines starting > values , following lines keys.

what have tried:

open (data, "seq-at.txt") or die "blabla"; @data = <data>; %result = (); $k = 0; $i = 0;  while($k != @data) {     $info = @data[$k]; #istrina pirma elementa     if(@data[$i] !=~ ">") {         $key .= @data[$i]; $i++;     } else {         $k = $i;     }     $result{$key} = $value; } 

but doesn't work.

you don't have use array, can directly build hash:

use strict; use warnings; # ^- start code see errors , ambiguous  # declare variables using "my" specify scope $filename = 'seq-at.txt';   # use 3 parameters open syntax avoid overwrite file: open $fh, '<', $filename or die "unable open '$filename' $!";  %hash; $hkey = ''; $hval = '';  while (<$fh>) {     chomp; # remove newline \n (or \r\n)     if (/^>/) { # when line start ">"         # store key/value in hash if key isn't empty         # (the key empty when first ">" encountered)         $hash{$hkey} = $hval if ($hkey);         # store line in $hval , clear $hkey         ($hval, $hkey) = $_;     } elsif (/\s/) { # when line isn't empty (or blank)         # append line key         $hkey .= $_;     } }  # store last key/val in hash if $hash{$hkey} = $hval if ($hkey);  # display hash foreach (keys %hash) {     print "key: $_\nvalue: $hash{$_}\n\n"; } 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -