gokuraku has asked for the wisdom of the Perl Monks concerning the following question:
I have an array of references to the arrays so I can make sure each one is done.my @configapp1 = ("m:configapp1", "d:conf", "f:dactest1", "f:conf,4K_logging.properties", "f:conf,logging.properties", my @configbbb = ("m:configbbb", "f:b1.0", "f:bb1.0.config");
Going through the array of references I build what I need:my @allDACDirs = (\@configapp1, \@configbbb);
My question is more educational, with the numerous methods to handle things in Perl what are some of the ways this could have been done differently? I tried doing this with hashes or arrays of hashes without getting what I wanted, the array of arrays I was able to understand and follow. Or maybe I'm not good at following depth in multi-dimensional arrays. I'm sure some things could be more elegant as well, and it'd be a good learning opportunity for me to see what I could have done simpler, or better. Thanks!# Determine the size of the array so we hit everything my $array_no = scalar(@allDACDirs); for( my $i = 0; $i < $array_no; $i++) { my $curr_dir; my $entry; # Read the array and populate foreach $entry (@{$allDACDirs[$i]}) { # Review value whether its a dir or file my($chk_val, $filename) = split(/:/,$entry); # m means we need to make a new root directory if ($chk_val eq "m") { mkdir("$location/$filename",0755) or warn "Could not mkdir: $!\n"; $curr_dir = "$location/$filename"; } # d means a directory under root (like bin or lib) elsif ($chk_val eq "d") { mkdir("$curr_dir/$filename",0755) or warn "Could not mkdir: $!\n"; } # f means a file has to be made elsif ($chk_val eq "f") { # If there is a , we need a sub_dir, then make # file in that directory if ($filename =~ /,/) { my($dir_name, $file) = split(/,/,$filename); if (!-d "$curr_dir/$dir_name") { mkdir("$curr_dir/$dir_name",0755) or warn "Could not mkdir: $!\n"; } # If file exists in current directory then copy if (-e "$local_dir/files/$file") { copy("$local_dir/files/$file", "$curr_dir/$dir_nam +e/$file") or die "Could not copy $filename: $!\n"; } else { # Else create the shell for the file open(FD,">$curr_dir/$dir_name/$file") || die "Could not make $file: $!\n"; } } else { # If file exists in current directory then copy if (-e "$local_dir/files/$filename") { copy("$local_dir/files/$filename", "$curr_dir/$fil +ename") or die "Could not copy $filename: $!\n"; } else { # Else create the shell for the file open(FD,">$curr_dir/$filename") || die "Could not make $filename: $!\n"; } } } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How would you handle arrays of arrays?
by toolic (Bishop) on Jan 09, 2009 at 14:22 UTC | |
|
Re: How would you handle arrays of arrays?
by gwadej (Chaplain) on Jan 09, 2009 at 14:36 UTC | |
by gwadej (Chaplain) on Jan 09, 2009 at 14:55 UTC | |
|
Re: How would you handle arrays of arrays?
by hbm (Hermit) on Jan 09, 2009 at 15:00 UTC | |
by gokuraku (Monk) on Jan 09, 2009 at 16:41 UTC | |
by hbm (Hermit) on Jan 09, 2009 at 19:23 UTC |