Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Listing of files using glob

by Anonymous Monk
on Jan 05, 2012 at 07:58 UTC ( #946336=note: print w/replies, xml ) Need Help??

in reply to Listing of files using glob

Can anyone suggest me why the file created via perl code is not being grabbed by the glob function??

*ahem* assumes facts not in evidence

the program you posted is not complete

the program you posted doesn't use strict or warnings

the program you posted doesn't create any files or directories

the program you posted doesn't doesn't check for errors

chdir can fail, you can check for errors with autodie

Replies are listed 'Best First'.
Re^2: Listing of files using glob
by AG87 (Acolyte) on Jan 05, 2012 at 08:24 UTC

    Here is the complete code. It basically takes a file...splits it and save the portions of file in separate files by creating a directory. Afterwards I want to list the individual file using glob. The output is still the same. I dont know where am wrong? The folders are being created, files are being created, the complete file name is not being listed only the extension is printed which is ".fasta".

    #!/usr/bin/perl use Cwd; use strict; use warnings; my $path_to_fastaSeqs = $ARGV[0]; my $path_to_fragmentScript = $ARGV[1]; my %seqInfo = (); &storeInHash; &createFolderAndFiles; &callFragmentScript; sub storeInHash { 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); } } sub createFolderAndFiles { my $dir = getcwd; while(my ($key, $value) = each(%seqInfo)) { my $nameWithoutFastaSign = $key; $nameWithoutFastaSign =~ tr/>//d; mkdir $nameWithoutFastaSign, 0777; open(FILEOUT, ">$dir/$nameWithoutFastaSign/$nameWithoutFastaSign.fasta +"); { print FILEOUT "$key\n$value\n"; } } } sub callFragmentScript { while(my $key = each %seqInfo) { my $nameWithoutFastaSign = $key; $nameWithoutFastaSign =~ tr/>//d; my $currentDir = getcwd; chdir "$currentDir/$nameWithoutFastaSign"; my $changedDir = getcwd; print "changed directory is: $changedDir\n"; while(my $fastaFile = glob("*.fasta")) { print "$fastaFile\n"; } chdir "$currentDir"; } }

    output is

    changed directory is: /home/ammarah/Documents/code/1elwA .fasta changed directory is: /home/ammarah/Documents/code/1ghwA .fasta changed directory is: /home/ammarah/Documents/code/1flwA .fasta

      storeInHash seems problematic. I have seen a .fasta file, and it looks like  >gi|21040368|ref| ... , and on my platform there is no way to create a file/directory with a | character in the name, so what Platform/Filesystem are you on?

        I can't speak for AG87, but on Linux it's pretty easy to create a file with a pipe symbol in it.

        touch 'foo|bar' && ls -l foo* && rm -i 'foo|bar'

        If I recall correctly, the only character disallowed in Linux filenames is the slash.

        I am working on linux and it has no issues in creating a filename with "|" sign in it. I have figured out the problem. In actual the file created in sub createFoldersAndFiles automatically inserts a space before the extension .fasta due to which the complete file name is not being grabbed by glob. Can anyone suggest how to overcome this problem coz its very strange. Normally the FILEOUT handle does not add a space before extension. Any thoughts??? :(

        I cant see any space though

        Even the code (part of the previous complete code) below does not print the complete filename. It only prints the extension

        $nameWithoutFastaSign =~ tr/>//d; print "$nameWithoutFastaSign.fasta\n";

        Any more thoughts?? :(

        Before tr it has ">1elwA". and tr is used to omit the ">" sign

        A separate code does work for me as well. But in the full fledge code its not working. The file names ">1elwA" etc etc are stored in a hash as a key. In a loop it has to retrieve the key value, tr it, and then print the output with .fasta extension. In the embedded code it is not printing the complete file name. Maybe there is some problem in storing the file in hash. Is there some other way to capture the output of the regular expression into the key of the hash other than the way used in my code in the first sub with the name of "StoreInHash"????

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://946336]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2018-06-18 16:05 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.