Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Building scalar from File::Find

by sunadmn (Curate)
on Dec 29, 2003 at 19:18 UTC ( #317512=perlquestion: print w/ replies, xml ) Need Help??
sunadmn has asked for the wisdom of the Perl Monks concerning the following question:

Good day fellow monks I have the below code and have a it working for the most part but one issue I have ran into is when I build $out if there is not seven dirs I get output that looks like so:


Now the issue I would like to resolve is how can I build my $out scalar so that it will grab anything from dir 4 on and build itself from that,so if I have a file living under /net it will build $out like and if I have something like /us/pa/k12 it will look like and with either I wont have the trailing "." after the name if it doesn't equal seven in length. Any help you may all point out to me would be great as I am pulling my hair out trying to figure this one out right now.

#!/usr/bin/perl use strict; use File::Find; use FileHandle; my $base = "/chroot/named/master"; my $out = "/var/tmp/current_list"; find(\&wanted, $base); sub wanted { return unless ( -f "$File::Find::name" and $File::Find::name !~ m!/ +in-addr/! ); my $name = "$File::Find::name"; my $zone = new FileHandle "$name", "r"; my $out = join('.', ( split('/', $name) )[4..7]); print "$out\n"; }

Comment on Building scalar from File::Find
Download Code
Replies are listed 'Best First'.
Re: Building scalar from File::Find
by demerphq (Chancellor) on Dec 29, 2003 at 19:45 UTC
    my $out = join('.', ( split('/', $name) )[4..7]);

    Should be something like

    my @dirs=split '/', $name ; my $out = join '.', @dirs[4..(@dirs<8 ? $#dirs : 7)];

    Also, your style probably encourages bad habits. You are quoting variables unnecessarily, and you are using indirect notation, both of which are not the best ideas.

    Good luck.


      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi

Re: Building scalar from File::Find
by ysth (Canon) on Dec 30, 2003 at 03:37 UTC
    From your description, maybe you want something like this:
    #!/usr/bin/perl use strict; use warnings; use File::Find; my $base = "/home/sthoenna/bleadperl/perl"; find(\&wanted, $base); sub wanted { return unless ( -f "$File::Find::name" and $File::Find::name !~ m!/ +Encode/! ) ; my $name = "$File::Find::name"; my $out = join '.', grep defined, reverse( (split(m!/!, $name))[4 +..7] ); print "$out\n"; }
    Added warnings, reversing the names, removing unreached levels. Removed the extra my $out at the beginning; hope you realize that was a completely separate variable from the wanted sub's my $out. Removed the unused FileHandle stuff.

    I like to always say m// for the first parameter to split to remind me that it is a's too easy to do something like split '+' and be surprised when it fails. (The exception is " " (a single blank) which split treats specially, not as a regex.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://317512]
Approved by coreolyn
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2015-11-28 22:00 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (746 votes), past polls