string manipulation

A

a

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

Also, I have a directory as an input.
e.g //server/dirA/dirB/
How can I check the last character is a / or not?
Thanks a lot
 
J

John Bokma

a said:
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
File::Spec

Also, I have a directory as an input.
e.g //server/dirA/dirB/
How can I check the last character is a / or not?

Why? Also, might that File::Spec doesn't make this test necessary.
 
U

usenet

a said:
I have an input string. It is a full path of a file.
How can I parse it into directory and file?

Hmm. Someone else just asked this same question in a different group...
the answer is the same; let File::Basename (a built-in Perl module) do
it for you:

#!/usr/bin/perl
use strict; use warnings;

use File::Basename; # Standard module included in Perl

my $filename = '//server/dirA/dirB/file.txt';

my ($name, $path) = fileparse($filename);
print "$path\n$name\n";

__END__
 
J

James Taylor

a said:
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

If you don't want to use a separate module for such a simple task and
you know there will be at least one slash directory separator, then a
simple regex should be good enough.

my ($dir, $leaf) = $path =~ m|^(.*)/(.*)|;

If you can't be sure of getting at least one slash then you could prefix
one to the $path first, or just check for this situation and assign the
rest to the $leaf.
Also, I have a directory as an input.
e.g //server/dirA/dirB/
How can I check the last character is a / or not?

Simple:

if ( $path =~ m|/$| ) {
# has trailing slash
} else {
# no trailing slash
}
 
J

Josef Moellers

a said:
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

Also, I have a directory as an input.
e.g //server/dirA/dirB/
How can I check the last character is a / or not?

You seem to be very ignorant of anything other people write.
In your thread "array question", you have been asked to show code that
you wrote and that dowsn't work. You have been asked to read the posting
guidelines for this group.

Yet you post another question showing that you do not want to help us
help you.

You're steering straight into a lot of people's killfiles.
 
A

A. Sinan Unur

a said:
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
 
J

Jürgen Exner

a said:
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
 
X

Xicheng

a said:
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
 
P

Paul Lalli

Xicheng said:
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
 
A

axel

a said:
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
 
X

Xicheng

Paul said:
no need to invoke the regexp engine for such a simple task. (Oh, and
what you typed is a syntax error).
yep, you are right, I forgot to add parenthesis to the 'if' clause:(

Xicheng
 
D

DJ Stunks

a said:
<first question snipped>

Also, I have a directory as an input.
e.g //server/dirA/dirB/
How can I check the last character is a / or not?
Thanks a lot

I assume you only want to check for that trailing slash in order to
know whether or not it's actually a directory, so just test for that.

print "$_ is a directory" if ( -d );

-jp
 
J

James Taylor

Tad McClellan said:
^
^

That anchor serves no useful purpose, so it probably shouldn't be there.

My newsreader doesn't have a monospaced font (I must fix that) so your
caret appears to point to the dollar singin front of $path. I must
assume though that you were pointing at the beginning of line anchor in
my regex. The reason it is there is to ensure that the match fails in
linear time if there is no slash in the $path.
 
C

Ch Lamprecht

James said:
The reason it is there is to ensure that the match fails in
linear time if there is no slash in the $path.

However, for strings without slashes, the pattern with the anchor will
be (a little) slower than without the anchor.

(If anybody want's to know ;) )


use warnings;
use strict;
use Benchmark qw(cmpthese) ;

my $string = "a_string_with_or_without_slashes";
my $string2 = "a_string_with_or/without_slashes";
cmpthese(1000000, {
'anchor' => sub{my($foo,$bar)=$string =~ m|^(.*)/(.*)|},
'no_anchor' => sub{my($foo,$bar)=$string =~ m|(.*)/(.*)|},
'an+/' => sub{my($foo,$bar)=$string2 =~ m|^(.*)/(.*)|},
'no_an+/' => sub{my($foo,$bar)=$string2 =~ m|(.*)/(.*)|} });


prints:

Rate no_an+/ an+/ anchor no_anchor
no_an+/ 275028/s -- -1% -83% -84%
an+/ 278164/s 1% -- -83% -84%
anchor 1636661/s 495% 488% -- -5%
no_anchor 1721170/s 526% 519% 5% --

Btw: Is there an expression like 'peacounting' in english ??

Christoph
 
C

Ch Lamprecht

James said:
The reason it is there is to ensure that the match fails in
linear time if there is no slash in the $path.

However, for strings without slashes, the pattern with the anchor will
be (a little) slower than without the anchor.

(If anybody want's to know ;) )


use warnings;
use strict;
use Benchmark qw(cmpthese) ;

my $string = "a_string_with_or_without_slashes";
my $string2 = "a_string_with_or/without_slashes";
cmpthese(1000000, {
'anchor' => sub{my($foo,$bar)=$string =~ m|^(.*)/(.*)|},
'no_anchor' => sub{my($foo,$bar)=$string =~ m|(.*)/(.*)|},
'an+/' => sub{my($foo,$bar)=$string2 =~ m|^(.*)/(.*)|},
'no_an+/' => sub{my($foo,$bar)=$string2 =~ m|(.*)/(.*)|} });


prints:

Rate no_an+/ an+/ anchor no_anchor
no_an+/ 275028/s -- -1% -83% -84%
an+/ 278164/s 1% -- -83% -84%
anchor 1636661/s 495% 488% -- -5%
no_anchor 1721170/s 526% 519% 5% --

Btw: Is there an expression like 'peacounting' in english ??

Christoph
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top