Nabuku has asked for the wisdom of the Perl Monks concerning the following question:
sorry if this a trivial question, but I'm relatively new to perl...
anyway, here it is:
I'm writing a script what uses directory/files structure to build a multi-level hash. here's the test code I've tried to use:
#!/usr/bin/perl -w use diagnostics; my $source_dir = "/home/haim/tmp/lists"; sub init_source_list { chdir $source_dir or die "Cannot change dir to $source_dir! ($!)"; while (defined($sourcefile = glob("*"))) { # for every file in this # directory if (-d $sourcefile) { # if the file is actually a directory... while (defined($ver = glob("$sourcefile/*"))) { #$ver =~ s/.*\///; #remove everything before the "/". $apps{$sourcefile}{$ver} = undef; # add an empty key to # %apps{$sourcefile} } } else { $apps{$sourcefile} = undef; # add an empty key in the hash # %apps } } } &init_source_list; foreach $key (keys %apps) { if (keys %{ $apps{$key} }) { foreach $vkey (keys %{ $apps{$key} }) { print "$source_dir/$key/$vkey\n"; } } else { print "$source_dir/$key\n"; } }
if there are no spaces in the folders/files names then, everything is ok. the problem starts when one of the directories have spaces. below there are the correct tree and the tree I've got from this script:
Correct:
The Actual result I've got:
* note the "a b" directory and the test3/test6 directories.
As a workaround I've used 'chdir' to every directory:
this one has solved the problem but it's not a very alegant solution.
thanx
/home/haim/tmp/lists/png.list
/home/haim/tmp/lists/patches.list
/home/haim/tmp/lists/icon-list
/home/haim/tmp/lists/tcl
/home/haim/tmp/lists/test1
/home/haim/tmp/lists/test2
/home/haim/tmp/lists/test3/ver2
/home/haim/tmp/lists/test3/ver1
/home/haim/tmp/lists/test4
/home/haim/tmp/lists/a b/ver 2
/home/haim/tmp/lists/a b/ver1-default
/home/haim/tmp/lists/test5
/home/haim/tmp/lists/test6/ver2
/home/haim/tmp/lists/test6/ver1
/home/haim/tmp/lists/test7
/home/haim/tmp/lists/packages
/home/haim/tmp/lists/png.list
/home/haim/tmp/lists/patches.list
/home/haim/tmp/lists/icon-list
/home/haim/tmp/lists/tcl
/home/haim/tmp/lists/test1
/home/haim/tmp/lists/test2
/home/haim/tmp/lists/test3/test3/ver2
/home/haim/tmp/lists/test3/test3/ver1
/home/haim/tmp/lists/test4
/home/haim/tmp/lists/a b/a
/home/haim/tmp/lists/test5
/home/haim/tmp/lists/test6/test6/ver1
/home/haim/tmp/lists/test6/test6/ver2
/home/haim/tmp/lists/test7
/home/haim/tmp/lists/packages
I was wondering if there is a solution for that (I'm using perl 5.6.1 on both solaris and linux)?
--
Nabuku
| Replies are listed 'Best First'. | |
|---|---|
|
Re: globing files with spaces problem.
by kschwab (Vicar) on Aug 10, 2002 at 15:26 UTC | |
by Nabuku (Initiate) on Aug 10, 2002 at 17:37 UTC | |
by kschwab (Vicar) on Aug 10, 2002 at 17:42 UTC | |
by Nabuku (Initiate) on Aug 10, 2002 at 20:13 UTC | |
by Anonymous Monk on May 08, 2009 at 15:16 UTC | |
|
Re: globing files with spaces problem.
by atcroft (Abbot) on Aug 10, 2002 at 12:58 UTC | |
by Nabuku (Initiate) on Aug 10, 2002 at 20:17 UTC |