in reply to Re: make_path for creating directory tree
in thread make_path for creating directory tree

Hi all! I have been trying to edit the above example in order to create a directory tree that goes: structure/molecule/step. As in, /struct1/AB0001/S00001/ and /struct2/BC0001/S00001/. So, struct1 corresponds only to molecule AB, and struct2 corresponds only to molecule BC. However, both AB and BC files should have the same "steps" subdirectories in them, each time from S00001 to whatever step we are calculating at the time. With that in mind, I figured to first create the "parent" directories struct1 and struct2 and then, in another loop, to put in the relevant subdirectories. This however gives me the error "Use of uninitialized value $structs in concatenation (.) or string".
#/bin/perl/ use strict; use warnings; use File::Path qw(make_path); my @structs; my $structs; my @molecs; my $molecs; my @steps; my $steps; # Parent structure directory. @structs = ( dir_tree( 'struct', 7, 1, 1), dir_tree( 'struct', 7, 2, 2 ), # dir_tree( 'struct', 7, 3, 3 ), # dir_tree( 'struct', 7, 4, 4 ), # dir_tree( 'struct', 7, 5, 5 ), # dir_tree( 'struct', 7, 6, 6 ), ); # Subfolder molecule directory. @molecs = ( dir_tree( 'AB', 6, 1, 1 ), dir_tree( 'BC', 6, 1, 1 ), # dir_tree( 'XZ', 6, 1, 4 ), # dir_tree( 'XY', 6, 1, 4 ), # dir_tree( 'QU', 6, 1, 4 ), # dir_tree( 'QE', 6, 1, 4 ), ); # Subfolder steps directory. @steps = ( dir_tree( 'S', 6, 1, 2 ), ); for $structs (@structs) { make_path "/media/RAIDstorage/home/results/$structs"; } for $molecs (@molecs) { for $steps (@steps) { make_path "/media/RAIDstorage/home/results/$structs/$molecs/$s +teps"; } } # Define subroutine here. sub dir_tree { my ( $name, $total_length, $from, $to ) = @_; my $length = $total_length - length($name); my $format = "${name}%0${length}d"; return map sprintf( $format, $_ ), $from .. $to; }
If I use an integrated loop, like so
for $structs (@structs) { for $molecs (@molecs) { for $steps (@steps) { make_path "/media/RAIDstorage/home/results/$structs/$molecs/$s +teps"; } } }
I get no errors but the wrong directory tree. That way, all $molecs are added in both $structs, when I want AB to go only in $struct1 and BC to go only in $struct2. I also tried using two different subroutines, like sub1 and sub2, but it also gave me the same error, "uninitialized value". Is the answer the two different subroutines, only I did it wrong? Any hints as to what I'm messing up? Thank you all!

Replies are listed 'Best First'.
Re^3: make_path for creating directory tree
by fasoli (Beadle) on Jan 22, 2016 at 16:54 UTC
    Oops sorry, it looks like I forgot to log in when posting the above.
      for my $molecs (@molecs) { my $structs = shift @structs; ## Add for my $steps (@steps) { make_path "/media/RAIDstorage/home/results/$structs/$molecs/$steps +"; } }
      poj

        Hi poj, thank you for the quick reply! Just so I'm sure I'm trying this correctly, do you mean I should use only this loop? Or, add this loop to my code?

        I've tried it both alone and combined with one of my loops (below) (this one)

        for $structs (@structs) { make_path "/media/RAIDstorage/home/results/$structs"; }
        but in both cases it behaves weird. It still complains for the same uninitialized value, but it does go ahead, in both cases, and creates some directories, however it's not what I want to get in the end.

        It puts AB0001 in struct1 (correct) but instead of putting BC0001 in struct2, it puts AB0002. I tried increasing the lengths of the string to check for such a behaviour. It's probably wrong because I either did something wrong or I failed to explain what I want to get in the end. I want all AB0001, AB0002, AB0003 and so on to go into struct1 and all BC0001, BC0002, BC0003 and so on, to go into struct2

        I'm really, really sorry to be a pain - if anyone can point to what might be happening...? I don't get it because $structs is initialised so I don't know why it's complaining. And also it's confusing me how to get the subdirectories in correctly.