Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

s and replace

by andrew (Acolyte)
on Jul 07, 2002 at 16:40 UTC ( [id://179991]=perlquestion: print w/replies, xml ) Need Help??

andrew has asked for the wisdom of the Perl Monks concerning the following question:

alright I have this:
finddir("$config{home}$user{site_id}"); sub finddir { my $root = shift; chomp ( $root ); $root = $root . '/' unless ( $root =~ m|/$| ); local *DIR; opendir ( DIR, $root ); my @userfiles = sort grep !/^\.\.?\z/, readdir DIR; foreach $file (@userfiles) { next unless ( -d "$root$file" ); print "$dir"; finddir("$root$file"); } }
Now when I tell it to print, I dont want it to print "$config{home}$user{site_id}$file". I just want it to print the file, so how can I take the $config{home}$user{site_id} out.

Replies are listed 'Best First'.
Re: Find and Print Directory Names
by cjf (Parson) on Jul 07, 2002 at 17:02 UTC

    So you're trying to print out all the directories without the path attached onto them? If so, this might work:

    use warnings; use diagnostics; use strict; my %config = ( home => '/home/cjf/' ); my %user = ( site_id => 'code' ); finddir("$config{home}$user{site_id}"); sub finddir { my $root = shift; chomp $root; $root = $root . '/' unless ( $root =~ m|/$| ); opendir ( DIR, $root ) or die "Can't open Directory: $!"; my @userfiles = sort grep !/^\.\.?\z/, readdir DIR; foreach my $file (@userfiles) { next unless ( -d "$root$file" ); print $file, "\n"; finddir("$root$file"); } }

    You might want to consider File::Find as well.

      Why does this only print out one direcotry
      @alldir = finddir("$config{home}$user{site_id}"); print "@alldir"; sub finddir { $root = shift; chomp ( $root ); $root = $root . '/' unless ( $root =~ m|/$| ); local *DIR; opendir ( DIR, $root ); my @userfiles = sort grep !/^\.\.?\z/, readdir DIR; foreach $file (@userfiles) { next unless ( -d "$root$file" ); $dir = "$root$file"; $dir =~ s/^\Q$config{home}$user{site_id}\E//; push(@direct, $dir); finddir("$root$file"); } return "@direct"; }

        First off, Use strict warnings and diagnostics. It'll make debugging a lot easier.

        Secondly, try removing the        finddir("$root$file"); from within the foreach loop. You're exiting the loop after the first iteration.

        Update: Oops. Read that wrong. Hang on a sec.

        Update 2:

        my %config = ( home => '/home/cjf/' ); my %user = ( site_id => 'code' ); my @alldir = finddir("$config{home}$user{site_id}"); print $_, "\n" for @alldir; sub finddir { my $root = shift; chomp $root; $root = $root . '/' unless ( $root =~ m|/$| ); local *DIR; opendir DIR, $root or die $!; my @userfiles = sort grep !/^\.\.?\z/, readdir DIR; my @direct; foreach my $file (@userfiles) { next unless ( -d "$root$file" ); my $dir = "$root$file"; $dir =~ s/^\Q$config{home}$user{site_id}\E//; push(@direct, $dir); } return @direct; }

        That will return all directories in $config{home}$user{site_id} but not go into the sub directories. Is that what you're looking for?

        The reason it only prints out one directory is that you are not pushing the results of your recursive calls to finddir into the @direct array.

        next unless ( -d "$root$file" ); $dir = "$root$file"; $dir =~ s/^\Q$config{home}$user{site_id}\E//; push(@direct, $dir); push(@direct, finddir("$root$file"));
Re: s and replace
by Aristotle (Chancellor) on Jul 07, 2002 at 17:01 UTC
    s/^\Q$config{home}$user{site_id}\E//, print "$_\n" for finddir("$config{home}$user{site_id}");
    ____________
    Makeshifts last the longest.
Re: s and replace
by ehdonhon (Curate) on Jul 07, 2002 at 19:01 UTC
    finddir("$config{home}$user{site_id}"); sub finddir { my ($root, $relative) = @_; chomp ( $root ); $root .= '/' unless ( $root =~ m|/$| ); $relative .= '/' if ( defined($relative) and ( $relative !~ m|/$| ) +); local *DIR; opendir ( DIR, $root . $relative ); my @userfiles = sort grep !/^\.\.?\z/, readdir DIR; foreach $file (@userfiles) { next unless ( -d "$root$file" ); print "$relative$file"; finddir($root, $relative . $file ); } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://179991]
Approved by cjf
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-03-19 04:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found