Re: print values out of a hash
On Nov 11, 2003, at 1:47 PM, Christiane Nerz wrote: Hi! If I want to print out every value of a hash, what's wrong with doing it like that: foreach (keys %hash) { print $hash{$_}; print "\n";} Why do I only get one value??? I don't seen anything wrong with your code. I believe there is only one value in the hash, if that's all you see. Check the code that loads it. James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: print values out of a hash
James Edward Gray II wrote: On Nov 11, 2003, at 1:47 PM, Christiane Nerz wrote: Hi! If I want to print out every value of a hash, what's wrong with doing it like that: foreach (keys %hash) { print $hash{$_}; print "\n";} Why do I only get one value??? I don't seen anything wrong with your code. I believe there is only one value in the hash, if that's all you see. Check the code that loads it. What you probably want is: foreach (keys %hash) { print "$_ => $hash{$_}\n"; } -- Andrew Gaffney -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: print values out of a hash
On Nov 11, 2003, at 2:08 PM, Christiane Nerz wrote: Nope - 'cause if I print out the values key for key, I get all four: my $array = keys %hash; print $hash{$array[0]}; print"\n"; print $hash{$array[1]}; print"\n"; print $hash{$array[2]}; print"\n"; print $hash{$array[3]}; With the code foreach (keys %hash) { print $hash{$_}; print "\n";} I only get the value corresponding to $hash{$array[3]}. As near as I can tell, the above two chunks of code have identical effects. If you put the first chunk in the program EXACTLY where the foreach() loop is you see all of them? James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: print values out of a hash
If you would like to post more of your code, I would be happy to take a look at it. James On Nov 11, 2003, at 2:27 PM, Christiane Nerz wrote: jepp - all four are there.. I really don't understand it. thx so far - I have to finish for today - my little baby-son is crying :-( Jane ... As near as I can tell, the above two chunks of code have identical effects. If you put the first chunk in the program EXACTLY where the foreach() loop is you see all of them? James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: print values out of a hash
James Edward Gray II wrote: > On Nov 11, 2003, at 2:08 PM, Christiane Nerz wrote: > >> Nope - 'cause if I print out the values key for key, I get all four: >> >> my $array = keys %hash; $array is a scalar, which gets the number of keys in %hash. not the actual keys in %hash. you probably want my @array = keys %hash; >> >> print $hash{$array[0]}; >> print"\n"; >> print $hash{$array[1]}; >> print"\n"; >> print $hash{$array[2]}; >> print"\n"; >> print $hash{$array[3]}; where is @array declared? how does it get the keys of %hash? >> >> With the code >> foreach (keys %hash) { >> print $hash{$_}; >> print "\n";} >> >> I only get the value corresponding to $hash{$array[3]}. as far as printing out the values of %hash is concerned, there is nothing wrong with this piece of code. if you only see one value, you either have one key in your %hash or all your keys in the hash contain exectly the same value, $array[3] in this case. the problem most likely originated in other part of your script. for example, how do you store the keys in %hash? david -- $_=q,015001450154015401570040016701570162015401440041,,*,=*|=*_,split+local$"; map{~$_&1&&{$,<<=1,[EMAIL PROTECTED]||3])=>~}}0..s~.~~g-1;*_=*#, goto=>print+eval -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: print values out of a hash
On Tuesday, Nov 11, 2003, at 11:47 US/Pacific, Christiane Nerz wrote: Hi! If I want to print out every value of a hash, what's wrong with doing it like that: foreach (keys %hash) { print $hash{$_}; print "\n";} Why do I only get one value??? probably because there is only One Value in the hash? my %hash = qw/ a b c d e f g h/ ; print "$_ => $hash{$_} \n" foreach (sort keys %hash); will generate: a => b c => d e => f g => h so if your code is only getting ONE iteration through the loop, then %hash has only one key in it. ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: print values out of a hash
Christiane Nerz wrote: > > Hi! Hello, > If I want to print out every value of a hash, what's wrong with doing it > like that: > > foreach (keys %hash) { > print $hash{$_}; > print "\n";} Nothing wrong with that but if you just want the values then use values() instead of keys(). foreach ( values %hash ) { print "$_\n";} > Why do I only get one value??? You must only have one key/value entry in the hash. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: print values out of a hash
Nope - 'cause if I print out the values key for key, I get all four: my $array = keys %hash; print $hash{$array[0]}; print"\n"; print $hash{$array[1]}; print"\n"; print $hash{$array[2]}; print"\n"; print $hash{$array[3]}; With the code foreach (keys %hash) { print $hash{$_}; print "\n";} I only get the value corresponding to $hash{$array[3]}. Jane James Edward Gray II wrote: On Nov 11, 2003, at 1:47 PM, Christiane Nerz wrote: Hi! If I want to print out every value of a hash, what's wrong with doing it like that: foreach (keys %hash) { print $hash{$_}; print "\n";} Why do I only get one value??? I don't seen anything wrong with your code. I believe there is only one value in the hash, if that's all you see. Check the code that loads it. James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: print values out of a hash
jepp - all four are there.. I really don't understand it. thx so far - I have to finish for today - my little baby-son is crying :-( Jane ... As near as I can tell, the above two chunks of code have identical effects. If you put the first chunk in the program EXACTLY where the foreach() loop is you see all of them? James -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: print values out of a hash
oh nice - like that I get all keys and the corresponding values printed.. But I only wanted to get all values :-) Jane Andrew Gaffney wrote: James Edward Gray II wrote: On Nov 11, 2003, at 1:47 PM, Christiane Nerz wrote: Hi! If I want to print out every value of a hash, what's wrong with doing it like that: foreach (keys %hash) { print $hash{$_}; print "\n";} Why do I only get one value??? I don't seen anything wrong with your code. I believe there is only one value in the hash, if that's all you see. Check the code that loads it. What you probably want is: foreach (keys %hash) { print "$_ => $hash{$_}\n"; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: print values out of a hash
[ Please do not top-post. TIA ] Christiane Nerz wrote: > > Nope - 'cause if I print out the values key for key, I get all four: > > my $array = keys %hash; $array is a scalar and holds a single value. It is not related to @array in any way. > print $hash{$array[0]}; > print"\n"; > print $hash{$array[1]}; > print"\n"; > print $hash{$array[2]}; > print"\n"; > print $hash{$array[3]}; If you have 'strict' and 'warnings' turned on then perl would tell you that @array doesn't exist for those four lines. > With the code > foreach (keys %hash) { > print $hash{$_}; > print "\n";} > > I only get the value corresponding to $hash{$array[3]}. It sounds like you are assigning a list to a scalar. $scalar = ( 'one', 'two', 'three', 'four' ); In this case the comma operator will evaluate the first three items and discard them and assign the fourth item to the scalar. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: print values out of a hash
Christiane Nerz wrote: > Andrew Gaffney wrote: > > > James Edward Gray II wrote: > > What you probably want is: > > > > foreach (keys %hash) { > > print "$_ => $hash{$_}\n"; > > } > > > oh nice - like that I get all keys and the corresponding values printed.. > But I only wanted to get all values :-) > Jane Maybe so. OTOH, if you don't trash information before you have a chance to evaluate its significance, you might get a better concept of what is actuall happening in your code. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: print values out of a hash - filling the hash
Good morning! Thx for your help.. The problem seems to lay in "filling the hash". But I can't see why. I want to compare two fasta-files, more precisely the IDs of two sets of sequences. Each file looks like: >gi|13699918|dbj|BAB41217.1|. MSEKEIWEKVLEIAQEKLSAVSYSTFLKDTELYTIKDGEAIVLSSIPFNANWLNQQYAEIIQAILFDVVG YEVKPHFITTEELANYSNNETATPKETTKPSTETTEDNHVLGREQFNAHNTFDTFVIGPGNRFPHAASLA VAEAPAKAYNPLFIYGGVGLGKTHLMHAIGHHVLDNNPDA.. >gi|13699919|dbj|BAB41218.1| QFQTLITSGHSEFNLSGLDPDQYPLLPQVSRDDAIQLSVKVLKNVIAQTNFAVSTSETRPVLTGVNWLIQ ENELICTATDSHRLAVRKLQLEDVSENKNVIIPGKALAE >gi|13699920|dbj|BAB41219.1|. MIILVQEVVVEGDINLGQFLKTEGIIESGGQAKWFLQDVEVLINGVRETRRGKKLEHQDRIDIPELPEDA GSFLIIHQGEQ >gi|13699921|dbj|BAB41220.1|... MKLNTLQLENYRNYDEVTLKCHPDVNILIGENAQGKTNLLESIYTLALAKSHRTSNDKELIRFNADYAKI EGELSYRHGTMPLTMFITKKGKQVKVNHLEQSRLTQYIGHLNVVLFAPEDLNIVKGSPQIRRRFIDMELG QISAVYLNDLAQYQRILKQKNNYLKQLQLGQ The whole code is as follows: #!/usr/local/bin/perl # ### # add_IDs_by_pattern_matching # # Input: Two fastafiles. # (Some sequences are in both files, but labeled with different IDS) # Output: One fasta-file, sequences labeled with both IDs. ### use strict; my $fastafilename1 = ''; my $fastafilename2 = ''; my %hash_fasta1 = ''; my %hash_fasta2 = ''; my $key_fasta1 = ''; my $key_fasta2 = ''; my $value_fasta1 = ''; my $value_fasta2 = ''; my %hash_pattern = ''; my $key_pattern = ''; my $value_pattern = ''; print "Please type the name of the first file:\n"; chomp ($fastafilename1 = ); open(FASTAFILE1, $fastafilename1) || die("Cannot open file for reading: $!"); #read in the first file #and filling two hashes #values in hash_pattern later used for pattern-matching my $k = 0; my $i = 0; while () { if(/^>/) { chomp; $i =1; if ($k==1) { chomp; $hash_fasta1{$key_fasta1} = $value_fasta1; $value_fasta1 =''; } else { $key_fasta1 = $_; $key_pattern = $_; } } else { if ($i==0) { chomp; $value_fasta1 = $value_fasta1 . $_; $k=1; } else{ $i = 0; chomp; $hash_pattern{$key_pattern} = $_; $value_fasta1 = $value_fasta1 . $_; $k=1; } } } $hash_fasta1{$key_fasta1} = $value_fasta1; delete $hash_fasta1 {''}; delete $hash_pattern {''}; close(FASTAFILE1) || die("Can't close in file: $!") ; #read in the second file print "Please type the name of the first file:\n\n"; chomp ($fastafilename2 = ); open(FASTAFILE2, $fastafilename2) || die("Cannot open file for reading: $!"); my $j = 0; while () { if(/^>/) { chomp; if ($j==1) { chomp; $hash_fasta2{$key_fasta2} = $value_fasta2; $value_fasta2 =''; } else { $key_fasta2 = $_; } } else { chomp; $value_fasta2 = $value_fasta2 . $_; $j=1; } } $hash_fasta2{$key_fasta2} = $value_fasta2; delete $hash_fasta2 {''}; close(FASTAFILE2) || die("Can't close in file: $!") ; my $outputfile = ''; #open outputfile $outputfile = "both_IDs"; unless (open(BOTH_IDS, ">$outputfile") ) { print "Cannot open file \"$outputfile\" to write to!!\n\n"; exit; } my @array1 = keys %hash_fasta1; my @array2 = keys %hash_fasta2; ## # Because I only found one sequence, which is in both fasta-files, # I tried to find out, if the hashes are correctly filled. # So here I put the code as described** and got the different # output. Rest of code: my $key_hash1 = ''; my $key_hash2 = ''; #pattern-matching foreach (@array1) { $key_hash1 = $_; foreach (@array2) { $key_hash2 = $_; if ($hash_fasta2{$key_hash2} =~ $hash_pattern{$key_hash1}) { print "Pattern $hash_pattern{$key_hash1} is in $hash_fasta2{$key_hash2} \n"; print BOTH_IDS $key_hash1 . $key_hash2 . $hash_fasta2{$key_hash2}; } } } #close outputfile close (BOTH_IDS) || die("Can't close in file: $!"); ** I get all four values with:: my @array = keys %hash; print $hash{$array[0]}; print"\n"; print $hash{$array[1]}; print"\n"; print $hash{$array[2]}; print"\n"; print $hash{$array[3]}; With the code foreach (keys %hash) { print $hash{$_}; print "\n";} I only get the value corresponding to $hash{$array[3]}. James Edward Gray II wrote: If you would like to post more of your code, I would be happy to take a look at it. James On Nov 11, 2003, at 2:27 PM, Christiane Nerz wrote: jepp - all four are there.. I really don't understand it. thx so far - I have to finish for today - my little baby-son is crying :-( Jane ... As near as I can tell, the above two chunks of code have identical effects. If you put the first chunk in the progr
Re: print values out of a hash - got the faults
On Wednesday, Nov 12, 2003, at 02:15 US/Pacific, Christiane Nerz wrote: Hi everybody! I got the mistakes - no bugs but simple human errors ;-) My program wasn't working correctly, because two hashes wasn't filled properly and the reason for the miraculous different output of the values of that hash was a simple typo - I typed once hash_fasta1 and the second time hash_fasta2 and copied that typo a hundred times s dumb!:-( would this be a gooder time to think about creating 'subs' where one has more common code that is independent of the name of the thing one is comparing? your recent code sample showed that you were repeating whole chunks of it for reading in two different files and having one sub that you called twice would at least limit the amount of typing, and, uh 'cut and paste' OOOPSIES... ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: print values out of a hash - filling the hash
Christiane Nerz wrote: > > Good morning! Hello, > Thx for your help.. > > The problem seems to lay in "filling the hash". > But I can't see why. > > I want to compare two fasta-files, more precisely the IDs of two sets > of sequences. I take it that the ID is everything between ' >' and "\n"? > Each file looks like: > > >gi|13699918|dbj|BAB41217.1|. > MSEKEIWEKVLEIAQEKLSAVSYSTFLKDTELYTIKDGEAIVLSSIPFNANWLNQQYAEIIQAILFDVVG > YEVKPHFITTEELANYSNNETATPKETTKPSTETTEDNHVLGREQFNAHNTFDTFVIGPGNRFPHAASLA > VAEAPAKAYNPLFIYGGVGLGKTHLMHAIGHHVLDNNPDA.. > >gi|13699919|dbj|BAB41218.1| > QFQTLITSGHSEFNLSGLDPDQYPLLPQVSRDDAIQLSVKVLKNVIAQTNFAVSTSETRPVLTGVNWLIQ > ENELICTATDSHRLAVRKLQLEDVSENKNVIIPGKALAE > >gi|13699920|dbj|BAB41219.1|. > MIILVQEVVVEGDINLGQFLKTEGIIESGGQAKWFLQDVEVLINGVRETRRGKKLEHQDRIDIPELPEDA > GSFLIIHQGEQ > >gi|13699921|dbj|BAB41220.1|... > MKLNTLQLENYRNYDEVTLKCHPDVNILIGENAQGKTNLLESIYTLALAKSHRTSNDKELIRFNADYAKI > EGELSYRHGTMPLTMFITKKGKQVKVNHLEQSRLTQYIGHLNVVLFAPEDLNIVKGSPQIRRRFIDMELG > QISAVYLNDLAQYQRILKQKNNYLKQLQLGQ > > The whole code is as follows: > > #!/usr/local/bin/perl > # > ### > # add_IDs_by_pattern_matching > # > # Input: Two fastafiles. > # (Some sequences are in both files, but labeled with different IDS) > # Output: One fasta-file, sequences labeled with both IDs. Here you seem to say that you want to compare the sequences and not the IDs? > ### > > use strict; > my $fastafilename1 = ''; > my $fastafilename2 = ''; > my %hash_fasta1 = ''; > my %hash_fasta2 = ''; > my $key_fasta1 = ''; > my $key_fasta2 = ''; > my $value_fasta1 = ''; > my $value_fasta2 = ''; > my %hash_pattern = ''; > my $key_pattern = ''; > my $value_pattern = ''; > > print "Please type the name of the first file:\n"; > chomp ($fastafilename1 = ); > open(FASTAFILE1, $fastafilename1) || > die("Cannot open file for reading: $!"); > > #read in the first file > #and filling two hashes > #values in hash_pattern later used for pattern-matching > > my $k = 0; > my $i = 0; > while () { > if(/^>/) { Isn't there a space before the '>' character? > chomp; > $i =1; > if ($k==1) { > chomp; You have already chomped the line, there is no point in chomping it again. > $hash_fasta1{$key_fasta1} = $value_fasta1; > $value_fasta1 =''; > } > else { > $key_fasta1 = $_; > $key_pattern = $_; > } > } > else { > if ($i==0) { > chomp; You have already chomped the line, there is no point in chomping it again. > $value_fasta1 = $value_fasta1 . $_; > $k=1; > } > else { > $i = 0; > chomp; You have already chomped the line, there is no point in chomping it again. > $hash_pattern{$key_pattern} = $_; > $value_fasta1 = $value_fasta1 . $_; > $k=1; > } > } > } > > $hash_fasta1{$key_fasta1} = $value_fasta1; > delete $hash_fasta1 {''}; > delete $hash_pattern {''}; You should probably avoid adding that key in the first place instead of deleting it later. > close(FASTAFILE1) || die("Can't close in file: $!") ; > > #read in the second file > > print "Please type the name of the first file:\n\n"; Don't you mean the second file? :-) > chomp ($fastafilename2 = ); > open(FASTAFILE2, $fastafilename2) || > die("Cannot open file for reading: $!"); > > my $j = 0; > while () { > if(/^>/) { > chomp; > if ($j==1) { > chomp; > $hash_fasta2{$key_fasta2} = $value_fasta2; > $value_fasta2 =''; > } > else { > $key_fasta2 = $_; > } > } else { > chomp; > $value_fasta2 = $value_fasta2 . $_; > $j=1; > } > } > > $hash_fasta2{$key_fasta2} = $value_fasta2; > delete $hash_fasta2 {''}; > close(FASTAFILE2) || die("Can't close in file: $!") ; > > my $outputfile = ''; > > #open outputfile > $outputfile = "both_IDs"; > unless (open(BOTH_IDS, ">$outputfile") ) { > print "Cannot open file \"$outputfile\" to write to!!\n\n"; > exit; > } > > my @array1 = keys %hash_fasta1; > my @array2 = keys %hash_fasta2; > > ## > # Because I only found one sequence, which is in both fasta-files, > # I tried to find out, if the hashes are correctly filled. > # So here I put the code as described** and got the different > # output. Rest of code: > > my $key_hash1 = ''; > my $key_hash2 = ''; > > #pattern-matching > > foreach (@array1) { > $key_hash1 = $_; foreach $key_hash1 (@array1) { > foreach (@array2) { > $key_hash2 = $_; foreach $key_hash2 (@array2) { > if ($hash_fasta2{$key_hash2} =~ $hash_pattern{$