Actual Location: \\Server\Volume\Text\001\ABC001.txt
Server Location Variable, $A contains \\Server\Volume\LoadFiles
FilePath Variable, $B contains E:\Volume\Text\001\ABC001.txt
I want to modify $A and store the replacement value for $B so that $A\$BPrime would give me the file.
$BMinus in this case should be E:\
$BPrime is Text\001\ABC001.txt
$A is \\Server\Volume
What I have is this: I would love to get some feedback as to what I might have done differently or alternatives. Cause this still seems a bit cludgy.
my $BMinus = substr ($B, 0, (index ($B,&CheckFile(\$A,$B)) - 1));
sub CheckFile {
my ($P_ref,$T) = @_;
&CrawlForward($P_ref,\$T);
while (!(-s "$$P_ref\\$T") || (-s $T)) {
$$P_ref =~ s/\A(.+)\\[^\\]+\z/$1/;
&CrawlForward($P_ref,\$T);
}
return $T;
}
sub CrawlForward {
my ($C_ref,$T_ref) = @_;
my $FHold = $$T_ref;
while (!-s "$$C_ref\\$$T_ref") {
unless ($$T_ref =~ s/\A[^\\]+\\(.+)\z/$1/) {
$$T_ref = $FHold;
last;
}
}
return 1;
}
Update: I changed the
.+? to
[^\\]+ to be more self documenting as that is what I'm actually expecting but was relying on the greediness on
.+ and laziness on
.+? to get what I wanted, so this makes it easier to read.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.