punit jain wrote:
> Hi,
Hello,
> I have a requirement where I have directory structure like : -
>
> test --> test/user1/files/, test/user2/files/, test/user3/files/ etc.
> under sub-directories with usernames I have file with name usersettings.
>
> So the final structure as : -
>
> test / user1 / usersettings
> /files/
> user2 / usersettings
> /files/
> user3 / usersettings
> /files/
> user4 / usersettings
>
> etc
>
> I need to get all the subdirectories of test and then read the file
> usersettings under that later on to do some processing.
That is easy enough to do:
for my $file ( <test/*/usersettings> ) {
open my $FH, '<', $file or die "Cannot open '$file' because: $!";
# process $file contents here
}
> I wrote code below
> :-
> #!/usr/bin/perl
> use strict;
> use warnings;
> use File::Basename qw(basename dirname);
> use File::Find qw(find);
> use File::Find::Rule;
> my $indir = shift;
> my $Users = {};
Why not just use a hash instead of a reference to a hash?
> my @userdirs=File::Find::Rule->maxdepth(1)->directory->in($indir);
>
> # this will give me user directories which I want only to depth 1.
>
> foreach my $dir(@userdirs){
> next if($dir eq "$indir");
perldoc -q quoting
next if $dir eq $indir;
> # I need to skip parent directory
>
> my $user = basename($dir);
> print "$user"."\n";
perldoc -q quoting
print "$user\n";
OR:
print $user . "\n";
OR:
print $user, "\n";
> find( sub {
> print $File::Find::name;
> if ($File::Find::name =~ /Contacts/&& -s $File::Find::name> 0
> ) {
> print "$File::Find::name";
>
> # do some processing
> }
> }, $dir);
>
> }
>
> However I get :-
>
> Use of uninitialized value in print at
new.pl line 21.
> Use of uninitialized value in pattern match (m//) at
new.pl line 22.
Which lines above are 21 and 22?
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein