Extrating String using Split

A

Ashwin

Hi All
I have following code,

my $Loc="C:\\Temp\\testing_disp\\Internal";
my
@alldir=("C:\\Temp\\testing_disp\\Internal\\A","C:\\Temp\\testing_disp\\
Internal\\B","C:\\Temp\\testing_disp\\Internal\\C");

where A,B,C are deeper directory tree what I need, is to split so that
I get directory tree A,

The problem if I use below code it complains
my @finaldir=map((split(/$Loc/))[1],@alldir);
Unrecognized escape \T passed through before HERE mark in regex m/C:\T
<< HERE emp\testing_disp\Internal/

even
@finaldir=map(/$Loc(.*)/,@alldir);
fails with the same error
even if I remove reference to "c:" I still cannot split

Question is how to extract rest of tree;



--
 
P

Paul Lalli

Ashwin said:
Hi All
I have following code,

my $Loc="C:\\Temp\\testing_disp\\Internal";
my
@alldir=("C:\\Temp\\testing_disp\\Internal\\A","C:\\Temp\\testing_disp\\
Internal\\B","C:\\Temp\\testing_disp\\Internal\\C");

Don't do that to yourself. Use single quotes, and sane (ie, normal,
front) slashes. Yes, Windows understands them, I promise.

my $Loc = 'C:/Temp/testing_disp/Internal';
my @alldir = map { 'C:/Temp/testing_disp/Internal/' . $_ } qw/A B C/;
where A,B,C are deeper directory tree what I need, is to split so that
I get directory tree A,

I don't understand what you're saying here, but I'm guessing
File::Basename and File::Spec could be very helpful to you.

Check them out at http://search.cpan.org
The problem if I use below code it complains
my @finaldir=map((split(/$Loc/))[1],@alldir);

Gah. Whitespace doesn't cost you anything. Consider using it to
improve readability.
Unrecognized escape \T passed through before HERE mark in regex m/C:\T
<< HERE emp\testing_disp\Internal/

Your problem is that a pattern match imposes double-quotish
interpolation before passing the pattern to the regular expression
engine. Therefore, the backslashes you had in your strings are being
seen as a \T character, instead of a backslash followed by a T. If you
use / as I suggested above, you will bypass this problem entirely.

However, the general solution is to make sure you escape all meta
characters in any variables you use in a pattern match. You can do
this by surrounding the variable with \Q...\E:

my @finaldir = map { (split /\Q$Loc\E/)[1] } @alldir;
even
@finaldir=map(/$Loc(.*)/,@alldir);
fails with the same error
even if I remove reference to "c:" I still cannot split

That's because your problem has nothing to do with split(), but with
the pattern you're passing to the pattern match.

See also:
perldoc -f quotemeta
Question is how to extract rest of tree;

You should almost definately be using File::Spec's splitpath() method,
rather than trying to get at these directory names on your own.
http://search.cpan.org/~kwilliams/File-Spec-0.90/lib/File/Spec.pm

Paul Lalli
 
A

Ashwin

Paul said:
Ashwin said:
Hi All
I have following code,

my $Loc="C:\\Temp\\testing_disp\\Internal";
my
@alldir=("C:\\Temp\\testing_disp\\Internal\\A","C:\\Temp\\testing_di
sp\\ Internal\\B","C:\\Temp\\testing_disp\\Internal\\C");

Don't do that to yourself. Use single quotes, and sane (ie, normal,
front) slashes. Yes, Windows understands them, I promise.

my $Loc = 'C:/Temp/testing_disp/Internal';
my @alldir = map { 'C:/Temp/testing_disp/Internal/' . $_ } qw/A B C/;
where A,B,C are deeper directory tree what I need, is to split so
that I get directory tree A,

I don't understand what you're saying here, but I'm guessing
File::Basename and File::Spec could be very helpful to you.

Check them out at http://search.cpan.org
The problem if I use below code it complains
my @finaldir=map((split(/$Loc/))[1],@alldir);

Gah. Whitespace doesn't cost you anything. Consider using it to
improve readability.
Unrecognized escape \T passed through before HERE mark in regex
m/C:\T << HERE emp\testing_disp\Internal/

Your problem is that a pattern match imposes double-quotish
interpolation before passing the pattern to the regular expression
engine. Therefore, the backslashes you had in your strings are being
seen as a \T character, instead of a backslash followed by a T. If
you use / as I suggested above, you will bypass this problem entirely.

However, the general solution is to make sure you escape all meta
characters in any variables you use in a pattern match. You can do
this by surrounding the variable with \Q...\E:

my @finaldir = map { (split /\Q$Loc\E/)[1] } @alldir;
even
@finaldir=map(/$Loc(.*)/,@alldir);
fails with the same error
even if I remove reference to "c:" I still cannot split

That's because your problem has nothing to do with split(), but with
the pattern you're passing to the pattern match.

See also:
perldoc -f quotemeta
Question is how to extract rest of tree;

You should almost definately be using File::Spec's splitpath() method,
rather than trying to get at these directory names on your own.
http://search.cpan.org/~kwilliams/File-Spec-0.90/lib/File/Spec.pm

Paul Lalli


Thanks Lalli,
I don't understand what you're saying here, but I'm guessing
What I ment was A is like directory tree as
C:\\Temp\\testing_disp\\Internal\\A was actually
C:\\Temp\\testing_disp\\Internal\\Ringtone\\Internal\Src
what I needed was just Ringtone\\Internal\Src

but surrounding with \Q and \E I did manage to extract
thanks again


--
 
X

xhoster

Ashwin said:
Hi All
I have following code,

my $Loc="C:\\Temp\\testing_disp\\Internal";
my
@alldir=("C:\\Temp\\testing_disp\\Internal\\A","C:\\Temp\\testing_disp\\
Internal\\B","C:\\Temp\\testing_disp\\Internal\\C");

where A,B,C are deeper directory tree what I need, is to split so that
I get directory tree A,

The problem if I use below code it complains
my @finaldir=map((split(/$Loc/))[1],@alldir);
Unrecognized escape \T passed through before HERE mark in regex m/C:\T
<< HERE emp\testing_disp\Internal/

even
@finaldir=map(/$Loc(.*)/,@alldir);
fails with the same error
even if I remove reference to "c:" I still cannot split


Read about \Q in perldoc perlretut.
Question is how to extract rest of tree;

something like: /\Q$Loc\E(.*)/

Xho
 

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
474,379
Messages
2,571,945
Members
48,805
Latest member
CeceliaWri

Latest Threads

Top