s_gaurav1091 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, What I am doing is that I am storing a value of a enviorment variable $ENV{JBOSS_HOME} into another variable . my $path = "$ENV{JBOSS_HOME} Now $path contains /usr/local/jboss Now depending upon the value of $path I want to perform some operation such that a variable is formed which should contain /usr/local.How should i do that

Replies are listed 'Best First'.
Re: removing the last word
by prasadbabu (Prior) on Sep 21, 2005 at 07:31 UTC

    You can make use of File::Basename to accomplish your job.

    use File::Basename; $dirname= dirname($filepath);

    Prasad

Re: removing the last word
by sh1tn (Priest) on Sep 21, 2005 at 08:57 UTC
    Yes, you can use regexs like:
    my $full = '/usr/local/jboss'; my ($dir, $file) = $full =~ m|(.+)/(.+)|;
    But it is better to use core module if such one exists - File::Basename in this case. Thus you will not make mistake while reinventing the wheel.


Re: removing the last word
by gargle (Chaplain) on Sep 21, 2005 at 07:37 UTC

    Hi,

    Depending on your certainty of always having /usr/local you can do:

    bash-3.00$ perl -e '$p = "/usr/local/whatever"; if ( $p =~ /^\/usr\/lo +cal\/(.+)$/ ) { print $1."\n"; }'

    Output:

    whatever
    --
    if ( 1 ) { $postman->ring() for (1..2); }
Re: removing the last word
by GrandFather (Saint) on Sep 21, 2005 at 07:53 UTC

    If you just want to remove the last "word" then:

    my $full = '/usr/local/jboss'; my ($path, $tail) = $full =~ m|^(.*/)(.*?)$|; print "Path: $path\n"; print "Tail : $tail";

    Perl is Huffman encoded by design.
Re: removing the last word
by ambrus (Abbot) on Sep 21, 2005 at 10:31 UTC

    Can't you just append "/.."?