develooper Front page | perl.beginners | Postings from July 2012

Re: one liner for removing string from an element of string array

Thread Previous | Thread Next
From:
Shlomi Fish
Date:
July 13, 2012 08:59
Subject:
Re: one liner for removing string from an element of string array
Message ID:
20120713185948.2b0867b7@lap.shlomifish.org
Hi Satya,

On Fri, 13 Jul 2012 15:38:20 +0000
"Nemana, Satya" <snemana@sonusnet.com> 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?
> 

See File::Basename - http://perldoc.perl.org/File/Basename.html . In the
future, you may wish to use regular expressions for similar tasks:

http://perl-begin.org/topics/regular-expressions/

Regarding achieving stuff in one line, see:
http://www.shlomifish.org/humour/fortunes/show.cgi?id=newline-prices
and if you're so concerned about that, you can always write subroutines to
encapsulate commonly done tasks.

Regards,

	Shlomi Fish 


> TIA,
> 
> Regards,
> Satya
> 
> 
> 



-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Escape from GNU Autohell - http://www.shlomifish.org/open-source/anti/autohell/

Beliefs are what divide people. Doubt unites them.
    — http://en.wikiquote.org/wiki/Peter_Ustinov

Please reply to list if it's a mailing list post - http://shlom.in/reply .

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About