Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: working with directories

by madM (Beadle)
on Sep 12, 2013 at 13:57 UTC ( [id://1053730]=note: print w/replies, xml ) Need Help??


in reply to Re: working with directories
in thread working with directories

thanks a lot! i wrote this code in an attempt of reading and working with all the files in a directory but it doesnt work... yet if i specify the path of one file i get results im still a perl beginner so don´t laugh at my code ^^ thanks!
print "Enter a directory´s path to work with all files in a folder or +a single file path\n\n"; chomp(my $input =<STDIN>); if ($input =~ /./){ ### attempt of recognizing a file extention $datei = $input; open(my $fastd,'<', $datei) or die "die datei $datei wurde nicht g +eöffnet: $!\n"; while (my $line = <$fastd>) { #reading fasta file chomp $line; if ($line =~ /^>/) { $header = $line; $header =~ s/>//g; } else { $sequences->{$header} .= $line } } }elsif($input =~ /[^.]/){ #### attempt of recognizing just a folder b +ut i know it wont work always $path = $input; opendir my $DIR, $path or die " the directory couldn't be opened\n +"; my @folder = grep -f "$path/$_", readdir $DIR; for my $file (@folder){ open (my $fastd ,'<', "$path/$file") or die "the file $file co +uldn't be opened: $!\n"; while (my $line = <$fastd>) { #reading fasta file chomp $line; if ($line =~ /^>/) { $header = $line; $header =~ s/>//g; } else { $sequences->{$header} .= $line } } } }

Replies are listed 'Best First'.
Re^3: working with directories
by choroba (Cardinal) on Sep 13, 2013 at 07:23 UTC
    What do you mean by "doesnt work"? Is there an error or warning, or does nothing happen, does the computer hang or burn in flames?

    Dot has a special meaning in regular expressions. To match a literl dot, use /\./.

    Also, you should use -d to test whether something is a directory - directory names with extensions are also valid.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      sorry ir does work! :) i have only one problem .. i get in the hash reference $sequences a key and value that shouldn´t be there.. i guess it comes from the NB_FILE or some hidden file.. here´s what i get:
      $VAR1 = { '' => 'nmentlalignmentlargo.txtIlocblobF(??alignmentpro.txtI +locblob?(?? @? @ @ @ DSDB ` @ @ @',
      thanks again!
        There is probably a file in the directory that is not in the fasta format. Therefore, $header stays empty, and the contents of the file is appended to the empty header as the sequence.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found