Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Uploading Time (getting last element of a variable)

by thatguy (Parson)
on Jun 13, 2002 at 23:27 UTC ( #174372=note: print w/replies, xml ) Need Help??

in reply to Uploading Time

sure it's a cludge and there's a better way, but why split when you can just do:
$file="C:/aplle/index.htm"; $file=~ s/(.| )*\///; # delete everything(!) upto the last / print $file;

and to answer your question there AM, you have to escape a / if you use it in a regex because there are /'s as the separator. Naturally if your slash goes the other way (and there is no shame in that) you still have to escape it because perl thinks you are escaping the character after the \.

Replies are listed 'Best First'.
Re: Re: Uploading Time (getting last element of a variable)
by SarahM (Monk) on Jun 13, 2002 at 23:46 UTC
    Since he is working on a file upload to a webserver, the slash could be either way, it depends on how the client sends it. But this should work for either
    $file="C:/aplle/index.htm"; $file=~ s/.*[\/\\]//; # delete everything(!) upto the last / or \ print $file;
    P.S. Why did you use (.| )* instead of .* ?
      no reason other than just not having used said .*

      technically, i should not have used that at all, but I thought I would throw another perspective in. and it does work, just as long as the input data never changes, etc (it's what drives my archaic website)

        technically, i should not have used that at all

        Not true. In this case, you want the greediness. .* really is what you mean.

Re^2: Uploading Time (getting last element of a path)
by Aristotle (Chancellor) on Jun 14, 2002 at 08:47 UTC
    If one really wants to do it with a regex, a much better way would be
    $fullname = "C:\\apple\\index.htm"; my ($file) = $fullname =~ m!([^/\\]+)$!; print $file;
    Zero backtracking here. However, Macs are still going to be a problem (paths are separated with doublecolons there, right?), so File::Basename is what one should use.
    use File::Basename; # I ain't afraid of no ghosts.. $fullname = "C:\\apple\\index.htm"; my $file = basename $fullname; print $file;
    Makeshifts last the longest.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://174372]
[1nickt]: folks, would you say that 1.0 is an integer ?
[Lady_Aleena]: 1nickt, I wouldn't, but I'm not normal.
LanX aggrees, LA isn't normal ;-P
[Lady_Aleena]: LanX, thanks bunches.
[Lady_Aleena]: Rigth now I'm ranting in my head about an old subject.
[1nickt]: Hm, that is, how to prevent Perl from turning 1.0 into 1? I feel sure this must be an faq, but am reading perlnum and not finding the answer ...
[Lady_Aleena]: 1nickt, why does it matter in this case?
[1nickt]: Hm, perhaps this? "Operators which expect an integer force the argument into the integer format."
[1nickt]: Can this be? So print
[1nickt]: ... "expects an integer" ?

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2017-05-24 18:31 GMT
Find Nodes?
    Voting Booth?