On Sat, 23 Oct 2004 [EMAIL PROTECTED] wrote:

> I found the logic
>
> use File::Basename;
> $FILENAME="/Developer/view_local/local_nt/FDAFDSAFDSASDFA/ASDFDAFSASDF/NewProcess_date_22-oct-2004.log";
> my $name = basename($FILENAME,'.log');
> print substr($name, 0, 7);

FINALLY you have some code! :-)

You kept up your end of the request, here's my end, and my approach:

    $ cat ~/tmp/test.pl
    #!/usr/bin/perl

    use strict;
    my $file = 
"/Developer/view_local/local_nt/FDAFDSAFDSASDFA/ASDFDAFSASDF/NewProcess_date_22-oct-2004.log";
 -> ( my $name = $file ) =~ s#.*/(.......)[^/]*$#$1#;
    print "$name\n";

    $ perl ~/tmp/test.pl
    NewProc
    $

The key line is the one with the -> marker. There are several ways this 
could be written, but the basic idea is to match & capture the first 
seven characters after the last slash character. I've written it here by 
using a substitution on the $file variable that matches everything up to 
a '/', then captures the next seven characters (it doesn't matter what 
they are), then checks that from there to the end of the line there are 
no further slashes; the captured portion is saved and everything else is 
thrown away, and this gets stored back into the new $name variable.

Make sense? 

The BASENAME approach isn't bad either, but in this case the task is 
simple enough that I think it can safely be done without pulling in the 
extra module to get that functionality. What I'm doing would work with 
that as well though:

    $ cat ~/tmp/test.pl
    #!/usr/bin/perl

    use strict;
    my $file = 
"/Developer/view_local/local_nt/FDAFDSAFDSASDFA/ASDFDAFSASDF/NewProcess_date_22-oct-2004.log";
 -> my $name = basename($file,'.log');
 -> $name =~ s/^(.......).*$/$1/;
    print "$name\n";

    $ perl ~/tmp/test.pl
    NewProc
    $

So here, the basename() function strips out the path information, then I 
modify the substitution to match the first seven characters, no matter 
what they are, and throw out everything after that. 

Make sense?


-- 
Chris Devers

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to