Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^6: Listing of files using glob

by AG87 (Acolyte)
on Jan 05, 2012 at 11:56 UTC ( #946379=note: print w/ replies, xml ) Need Help??


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

No I am not. Here is the code used to store the file into hash

open(FILE, "$path_to_fastaSeqs") or die("cannot open file"); { while(<FILE>) { my $line = $_; if ($line =~ />.*/) { #print "$&\n"; } else { #print "$line\n"; } $seqInfo{$&} = $line; } close(FILE); }

Any errors or wrong approach??


Comment on Re^6: Listing of files using glob
Download Code
Re^7: Listing of files using glob
by choroba (Abbot) on Jan 05, 2012 at 13:22 UTC
    Can you show the input, too?
Re^7: Listing of files using glob
by MidLifeXis (Prior) on Jan 05, 2012 at 13:26 UTC

    > Any errors or wrong approach??

    Yep. You never chomp the data read from the filehandle FILE. Since you are building the file name directly from the line read, it most likely contains a newline. If you print your value for $line with a delimiter surrounding it (print "'$line'\n";) you will see what I mean. See chomp (as has been previously mentioned) for a solution.

    Update: Since the OP is using $& to populate the hash, and the processing loop uses the keys of the hash as the name of the file, the newline from the file should not be included in the hash key. Nice catch choroba

    I wonder what the line endings are in the data file. Is it possible that the file is in DOS line ending format? In which case, the chomp (and $&?) would not do the right thing.

    --MidLifeXis

      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.

        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

        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 :( :(

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://946379]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2014-09-17 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (88 votes), past polls