http://www.perlmonks.org?node_id=946401


in reply to Re^7: Listing of files using glob
in thread Listing of files using glob

It's a bit more complicated. The OP uses $& as the hash key after matching />.*/, which removes the newline (as . does not match a newline), but it can overwrite a previous hash value if the line does not match, which we cannot tell without seeing the input data.

Replies are listed 'Best First'.
Re^9: Listing of files using glob
by MidLifeXis (Monsignor) on Jan 05, 2012 at 14:12 UTC

    Aaah, quite right. I am curious about the line endingness of FILE then (in which case, chomp would not do the right thing either :-/ )

    --MidLifeXis

Re^9: Listing of files using glob
by AG87 (Acolyte) on Jan 06, 2012 at 05:16 UTC

    The input data is like

    >1elwA AAAAAASSSSSSSSSSSSSSDDDDDDDDDDDFFFFFFFFFF >1flwA GGGGGGGGGGHHHHHHHHHHHHIIIIIIIIIIIIIIJJJJJJJJJJ >1ghwA KKKKKKKKKKLLLLLLLLLLMMMMMMMMMMNNNNNNNNNNOOOOO

    Where >1elwA, >1flwA, >1ghwA should be the keys of the hashes and the respective data should be the value of the hash. The code/sub for storing in the file is

    #!/usr/bin/perl my %seqInfo = (); open(FILE, 'test.fasta'); { while(<FILE>) { my $line = $_; if ($line =~ />.*/) { #print "$&\n"; } else { #print "$line\n"; } $seqInfo{$&} = $line; print "$& => $line\n"; } }

    and the output is

    => >1elwA => AAAAAASSSSSSSSSSSSSSDDDDDDDDDDDFFFFFFFFFF => >1flwA => GGGGGGGGGGHHHHHHHHHHHHIIIIIIIIIIIIIIJJJJJJJJJJ => >1ghwA => KKKKKKKKKKLLLLLLLLLLMMMMMMMMMMNNNNNNNNNNOOOOO

    However according to my understanding the output should be like

    >1elwA => AAAAAASSSSSSSSSSSSSSDDDDDDDDDDDFFFFFFFFFF

    Maybe the file is not properly stored as keys and values :( :(

      Thankyou for the replies, Infact I have figured out the problem and have solved it. I have added a counter in it and its working fine + I have replaced the $& character with the similar substr expression as advised. So now the code is working fine. Thanks again for all the replies... :) :) :)