jakeeboy has asked for the wisdom of the Perl Monks concerning the following question:
I have a program that takes a directory string, opens it and then reads the directory into an array. It then checks the filesystem objects to see if there is another directory that needs to be searched for files or if there are files. If the objects are files it places those filenames into another array. This array is then placed into a hash with two keys ROOT and FILES.
If there are no files the hash just contains ROOT with a value of "0". The value of ROOT is the directory that contains the files and FILES is a reference to the @files array. The hash is then returned and a refernce to it is placed into another array. Essentially that new array is the entire directory structure with Parent directories and Child files.
The problem is when I print the array there seems to be blank lines that I can't account for and don't know how their getting into my structure? Heres a snippet of code that does all this:
our @Search = ("/directory_2b_searched"); sub open_dir { ...Code to open and read directory... #Loop thru Directory objects foreach $object (@objects) { if (-d $path . $object) { push @Search, $path . $object; # It's a directory to be se +arched } else { push @files, $object; # It's a file } } # If @files is empty return %tree with 1 key if ($#>files > 0) { my %tree = (ROOT=>$path, FILES=>\@files); return %tree; } else { my %tree = (ROOT=>"0"); return %tree; } } for (@Search) { my %dir_tree = open_dir($_); push @dir, \%dir_tree if ($dir_tree{ROOT}); }
Before %dir_tree is pushed into the @dir array it is checked to make sure it has a TRUE value, which is why I put ROOT=>"0" so that it's evaluated as false. This works because I have no hash values with "0" just blank lines in my output. I just can't figure it out. Thanks
Updated: Also, when I print out the structure I print it out with ROOT a tab and then FILES. So I know that there seems to be a ROOT in a hash with no value and a FILES with no value.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problems with blank lines
by Errto (Vicar) on Nov 30, 2005 at 16:34 UTC | |
by jakeeboy (Sexton) on Nov 30, 2005 at 16:50 UTC | |
|
Re: Problems with blank lines
by Roy Johnson (Monsignor) on Nov 30, 2005 at 16:23 UTC | |
by jakeeboy (Sexton) on Nov 30, 2005 at 16:46 UTC |