Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Problems? Is your data what you think it is?
 
PerlMonks  

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:

net.somedomain......

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 somedomain.net and if I have something like /us/pa/k12 it will look like somadomain.k12.pa.us 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.

Sunadmn
USE PERL
#!/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
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.


    ---
    demerphq

      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 regex...it'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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (14)
As of 2014-04-16 11:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (424 votes), past polls