Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

getting the tail of a string

by tos (Deacon)
on Jun 10, 2003 at 15:26 UTC ( [id://264724]=perlquestion: print w/replies, xml ) Need Help??

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

Dear monks,

for cutting off the tail (filename) of a given string i found two solutions.

perl -we 'use English;$x="j:/tmp/lz/ncak.030610.141654";$x=~/.+\// && +print $POSTMATCH' ncak.030610.141654
Unfortunately i can not use $POSTMATCHs shortform $' on command line.
perl -we '$x="j:/tmp/lz/ncak.030610.141654";($m = $_) for split "/",$x +;print $m' ncak.030610.141654
any suggestions for other, perhaps more elegant, timtowdi's ?

greetings

Replies are listed 'Best First'.
Re: getting the tail of a string
by Zaxo (Archbishop) on Jun 10, 2003 at 15:32 UTC
    use File::Basename;

    After Compline,
    Zaxo

Re: getting the tail of a string
by adrianh (Chancellor) on Jun 10, 2003 at 15:32 UTC
    use File::Basename; my $file_name = basename($full_path);

      As he is asking for a way to do this on the command line, I'd be inclined to offer something simple:

      my $m = $x =~ m!([^/]*)$!;

      -sauoq
      "My two cents aren't worth a dime.";
      

        And I'd be inclined to offer something that worked on my Mac OS 9 box (it's still there buzzing away in the corner :-)

        To-may-to. Toe-mah-to.

Re: getting the tail of a string
by TomDLux (Vicar) on Jun 10, 2003 at 15:54 UTC

    The correct solution in this case is using basename, since it is a filepath problem. More generally, it's a split() problem. Only most generally is regexp appropriate. Even then, I would avoid POSTMATCH - while the expense is tolerable, it's still high. Better to actually match the string you want, and remember that.

    $x =~ m|(?:[^/]/)*/(.*)}; print $1;

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

      I wouldn't say that split and or regexp are the most general ways of attacking this problem. The most general (read: efficient) way would be to find the character with rindex and extract the end of the string with substr. The following does the trick:

        my $ext = substr($x, rindex($x,'.')+1);

      This happens to rely on a side-effect of rindex, which is that if the character sought for is not found in the string, -1 is returned. Add 1 (pedant note: length('.') in fact), and instead of stepping over the sought character, it brings -1 up to 0, and hence your substr will hand you back the whole string. This can be construed as a feature :)

      But yes, File::Basename is the way to go in this particular case.

      update: weird transcription error spotted by sauoq: I had -rindex instead of rindex. Curiously enough, that's exactly what I had when I checked the snippet out on the command line. That'll teach me not to cut and paste...

      _____________________________________________
      Come to YAPC::Europe 2003 in Paris, 23-25 July 2003.

Re: getting the tail of a string (fun with command line quotes.)
by sauoq (Abbot) on Jun 10, 2003 at 19:52 UTC
    Unfortunately i can not use $POSTMATCHs shortform $' on command line.

    Sure you can. You just have to be a little more creative about how you get it in there. The way to do it is to end the single quoted portion of the command line, put another single quote but escape it, and continue on if necessary with another single quote. with the example you gave, that would be:

    perl -we '$x="j::/tmp/lz/ncak.030610.141654";$x=~/.+\// && print $'\'
    A shorter example for illustration:
    perl -le '$_="abc";/b/;print $'\' | ^--- the single quote part of $' +--- the dollar part of $'
    And to illustrate putting more after that...
    perl -le '$_="abc";/b/;print $'\'';print "more"'

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: getting the tail of a string
by DrHyde (Prior) on Jun 10, 2003 at 18:53 UTC
    Whilst others are correct in pointing out that the best perl solution for the task at hand involves basename, those suggestions don't point out what your problem here is - which will bite you in the arse whenever you try to use $' and similar on the command line. That is, you need to understand the black art of shell quoting. This:
    perl -we '$x="/foo/bar/ncak.030610.141654";$x=~/.+\// && print $'\'
    works, at least with the bash installed on this 'ere OS X box. What it does is single-quote all your perl up to and including the last $. It then ends the single-quoting, and immediately - with no intervening space - provides a single-quote. The single-quote is escaped so that the shell doesn't try to start a new quoted string and complain when it can't find the closing delimiter.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 17:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found