split string after third forwardslash

M

mike

Hi,

How can I use perl to get the following substring:

C:\cc_views\wong

from this string:

C:\cc_views\wong\mbv_admin\tools\scripts\deliver\delivery.pl

br,

//mike
 
J

Josef Moellers

mike said:
Hi,

How can I use perl to get the following substring:

C:\cc_views\wong

from this string:

C:\cc_views\wong\mbv_admin\tools\scripts\deliver\delivery.pl

What have you tried so far? Where did it fail to meet your expectations?

There are several ways to do so:
- use a regular expression (twice a path component followd by a
backslash, then another path component)
- use split and then an array slice.

There are bound to be other methods,

Josef
 
T

Tad J McClellan

mike said:
Hi,

How can I use perl to get the following substring:

C:\cc_views\wong

from this string:

C:\cc_views\wong\mbv_admin\tools\scripts\deliver\delivery.pl


If the full path is in $full, then use split() along with a "list slice":

my $partial = join '\\', (split /\\/, $full)[0..2];
 
C

ccc31807

Hi,

How can I use perl to get the following substring:

C:\cc_views\wong

from this string:

C:\cc_views\wong\mbv_admin\tools\scripts\deliver\delivery.pl

In most situations where you need to separate a string with values
delimited by some character(s), you can split the string into an array
variable by using split(), like:

$string = 'C:\cc_views\wong\mbv_admin\tools\scripts\deliver
\delivery.pl';
my @array = split(/\\/, $string);

This gives @array values like:
$array[0] = C:
$array[1] = cc_views
$array[2] = wong

And so on.

You can create a string using several methods, one of which is sprintf
(), like this:
$target = sprintf("%s\%s\%s",$array[0],$array[1],$array[2]);

This isn't the most concise way, but it's easy and understandable.
CC
 
J

Jürgen Exner

ccc31807 said:
$target = sprintf("%s\%s\%s",$array[0],$array[1],$array[2]);

Ouch, this hurts. Are you programming Perl or C?
This isn't the most concise way, but it's easy and understandable.

Well, why bother with sprintf() and its antics? IMO a good old plain
$target = "$array[0]\\$array[1]\\$array[2]";
is way easier and more understandable.

Of course a
$target = join '\', @array[0..2];
is probably more perlish.

jue
 
C

ccc31807

Ouch, this hurts. Are you programming Perl or C?

Personally, I favor sprintf.

If you want to compare it to something that REALLY causes pain, look
at Lisp's format function:
(format t "~{~{~a:~10t~a~%~}~%~}" *db*))
Well, why bother with sprintf() and its antics? IMO a good old plain
        $target = "$array[0]\\$array[1]\\$array[2]";
is way easier and more understandable.

or even $target = $array[0].'\'.$array[1].'\'.$array[2]

Does the solidus escape the following character with single quotes?

CC
 
C

ccc31807

What happened when you tried it?

I didn't try it, I just threw it out there. I'm just killing time
between a series of DB queries at work and one just popped so I hit
'Send.' Obviously, I'd find out what happened if I needed to write it,
but since I didn't I didn't.
(It escapes the delimiter or backslash itself. Other \X sequences are
passed through unchanged.)

Yeah, seems that I remember that now.

CC
 
D

Dr.Ruud

Tad said:
mike said:
Hi,

How can I use perl to get the following substring:

C:\cc_views\wong

from this string:

C:\cc_views\wong\mbv_admin\tools\scripts\deliver\delivery.pl


If the full path is in $full, then use split() along with a "list slice":

my $partial = join '\\', (split /\\/, $full)[0..2];

There is no need to repeat the split string:

my $partial = join "", (split /(\\)/, $full)[0..2*2];
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top