Hello, I've been trying to write a code that allows me to remove the first line of a file and then calculate the percent of the letters G and C in files from many subdirectories in a single directory. I know the program to calculate those percentages works in singular files but I cannot get it to work on files in subdirectories. Here is the script that I have so far:

I haven't tested your code but I immediately notice a couple of issues.

The first thing the subroutine list_recursively does is assign $directory with 'all.fna' regardless. You most likely meant to assign $directory the first argument passed in:

Code

my $directory = 'all.fna'; my $directory = shift;

I'm pretty certain you don't mean to print the result of $directory divided by $file. Don't forget to wrap in double quotes to ensure the value is interpolated appropriately. Also chuck in \n for readability purposes:

Code

print $directory/$file;print "$directory/$file\n";

Finally, not sure if you meant to put an exit at the end of the GC subroutine. There are also 2 closing curly braces afterwards (I am unable to tell if they are both necessary due to the formatting of your code upon post).

Also, I just formatted your code by hand to see that the two closing braces at the end are necessary. I couldn't tell that the GC subroutine was contained within the list_recursively subroutine (it would probably be best to separate). Wrap code you post here in code tags.

Alright, I have made the changes (including separating subroutines) but I am still getting bad results. When I run code I get scattered letters and parts of some of the folder names. I have never successfully been able to write code for accessing files in subdirectories and I was wondering if you would know whether that should successfully open those files? I've been trying to piece together code from different tutorials and forum answers.

Is 'all.fna' the correct starting directory (just in case, it looks like it could be a filename).

Also, the exit at the end of the list_recursively subroutine needs to be removed, which is probably the reason why you are only seeing "some of the folder names". This may resolve your issues, although I haven't looked into the workings of the GC subroutine (which you mentioned was working fine).

Okay, I just removed the "exit;" from the end of the subroutine. I tried changing the directory name to one that I knew was wrong and it gave me an error "Cannot open directory", but with 'all.fna' I did not get that error. Also the folder 'all.fna' and the perl program I am using are in the same folder.

If you use a directory that doesn't exist then your code will exit with the "Cannot open directory" error. If all.fna is a directory that sits in the same directory as your Perl script, then you are fine. If you run with just the exit removed, is the output better than before.

I re-wrote your recursive subroutine, and simplified the GC subroutine to just print the file_path. It might be a good starting point, since it only prints each file it recursively comes across. Printing too many unformatted strings can become confusing. If it works out, replace the GC subroutine with yours (don't forget to remove the final exit):

Okay, It seems that using your program and adding my GC (with some tweaks) worked. I really appreciate your help very much! But also if there is anyway I could get feedback on why my previous code did not work that would be great so that I really understand perl language. Really appreciate the help! This is pretty sweet! :)