Nemana, Satya wrote:
> Hi
>
> I have written a small program like this to just print file2 from the
> second element of the array by removing the .template from the entry (the
> name file2 can change and can be longer or shorter)
>
> use strict;
> use Data::Dumper;
> use warnings;
>
> my @templates = (
>     "/a/b/c/d/e/f/file1.template",
>     "/a/b/c/d/e/f/file2.template"
>                 );
>
> my @tokens=split( /\//, $templates[1]);
> print("\n".substr($tokens[$#tokens],0,-9));
>
> However I want this to be more efficient and want to do this in a single
> line as I have to do this several times.
> How can I do that?
>
> TIA,
>
> Regards,
> Satya

I assume you're working with file paths and you want to extract the
filename without the ext.

You can use a regex, but I'd probably prefer to use the File::Basename
module.
http://search.cpan.org/~rjbs/perl-5.16.0/lib/File/Basename.pm

#!/usr/bin/perl

use strict;
use warnings;
use File::Basename;
use Data::Dumper;

my @templates = (
    "/a/b/c/d/e/f/file1.template",
    "/a/b/c/d/e/f/file2.template"
);

my ($name,$path,$suffix) = fileparse($templates[1], '.template');
print Dumper ($name,$path,$suffix);

outputs:
$VAR1 = 'file2';
$VAR2 = '/a/b/c/d/e/f/';
$VAR3 = '.template';

-- 
Ron Bergin



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to