Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > string manipulation

Reply
Thread Tools

string manipulation

 
 
a
Guest
Posts: n/a
 
      01-27-2006
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


 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      01-27-2006
"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


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.

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com

 
Reply With Quote
 
 
 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      01-27-2006
a wrote:
> 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__

 
Reply With Quote
 
James Taylor
Guest
Posts: n/a
 
      01-27-2006
a <> wrote:

> 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
}

--
James Taylor
 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      01-27-2006
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
>
> 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.
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-27-2006
"a" <> wrote in news:FYiCf.454345$ki.271648@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 <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      01-27-2006
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


 
Reply With Quote
 
Xicheng
Guest
Posts: n/a
 
      01-27-2006
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

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      01-27-2006
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

 
Reply With Quote
 
axel@white-eagle.invalid.uk
Guest
Posts: n/a
 
      01-27-2006
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?


perldoc -f substr ... look for the bit about negative
OFFSETs in the first paragraph.

Axel


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String manipulation Chris ASP .Net 1 05-24-2004 02:19 PM
Advanced String Manipulation (C#) Aaron ASP .Net 2 01-02-2004 06:50 PM
String Manipulation Aaron ASP .Net 3 12-31-2003 03:39 PM
Perl string manipulation jared in ecs Perl 2 10-22-2003 05:36 PM
String Array Manipulation Problem Garfield ASP .Net 5 08-27-2003 10:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57