|
From: A. Sinan Unur on 27 Jan 2006 06:10 "a" <a(a)mail.com> wrote in news:FYiCf.454345$ki.271648(a)pd7tw2no: > I have an input string. It is a full path of a file. > eg. //server/dirA/dirB/file.txt > How can I parse it into directory and file? > i.e //server/dirA/dirB/ and file.txt This is not just string manipulation. It is path and file name manipulation. As such, you'd better served by using File::Spec. Using the module would help make your script more portable. > Also, I have a directory as an input. > e.g //server/dirA/dirB/ > How can I check the last character is a / or not? If you use File::Spec->catfile, you need not worry about that. Sinan -- A. Sinan Unur <1usa(a)llenroc.ude.invalid> (reverse each component and remove .invalid for email address) comp.lang.perl.misc guidelines on the WWW: http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
From: J?rgen Exner on 27 Jan 2006 08:09 a wrote: > Hi, > I have an input string. It is a full path of a file. > eg. //server/dirA/dirB/file.txt > How can I parse it into directory and file? > i.e //server/dirA/dirB/ and file.txt As usual: use File::Basename > Also, I have a directory as an input. > e.g //server/dirA/dirB/ > How can I check the last character is a / or not? As usual there are different ways: - you could use substr() to extract the last character of the string and eq it with '/' - you could use m// and anchor the RE to the end of the string - ... jue
From: Xicheng on 27 Jan 2006 09:43 a wrote: > Hi, > I have an input string. It is a full path of a file. > eg. //server/dirA/dirB/file.txt > How can I parse it into directory and file? > i.e //server/dirA/dirB/ and file.txt unless you have slash '/' in your filename, you can try: $str='//server/dirA/dirB/file.txt'; ($dir,$file)=$str=~m{^(.*?)([^/]*)$}; print "$dir\n$file"' > Also, I have a directory as an input. > e.g //server/dirA/dirB/ > How can I check the last character is a / or not? if $str =~ m{/$} { #the last char is / } Xicheng
From: Paul Lalli on 27 Jan 2006 10:15 Xicheng wrote: > a wrote: > > Also, I have a directory as an input. > > e.g //server/dirA/dirB/ > > How can I check the last character is a / or not? > if $str =~ m{/$} { > #the last char is / > } no need to invoke the regexp engine for such a simple task. (Oh, and what you typed is a syntax error). if (substr($str, -1) eq '/') { print "Last char is a slash\n"; } Paul Lalli
From: axel on 27 Jan 2006 10:27 a <a(a)mail.com> wrote: > Also, I have a directory as an input. > e.g //server/dirA/dirB/ > How can I check the last character is a / or not? perldoc -f substr ... look for the bit about negative OFFSETs in the first paragraph. Axel
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Check subroutine-specific requirements on module import Next: Java sucks, Perl Rules. |