Bob Showalter wrote:
Bret Goodfellow wrote:
I am trying to evaluate a string, and determine if the last charater
of the string has a backslash '\' . The piece of code I am using
doesn't appear to work. What I've found with this peice of code is
that if the string does contain a \, then the following code still
adds another slash. If there is not backslash in the string, the the
code appears to work fine by adding a backslash. Any ideas?
print "Deleting old directory $dir_no: $Dir_name\n";
$arg_length = length($ARGV[0]);
$arg_lastchar = substr($ARGV[0], $arg_length-1, 1);
Why did you switch from $Dir_name to $ARGV[0]?
Anyway, this can be simplified to
$arg_lastchar = substr($ARGV[0], -1);
print "last char is: $arg_lastchar\n";
################################################
# add a \ if one was not present in argument #
################################################
if ($arg_lastchar ne '\\' or $arg_lastchar ne '/') {
Logic flaw. This will always be TRUE. (*any* character will either be not a
backslash or not a forward slash.)
You want "and" (or "&&") instead of "or".
You can also use a regex and do the whole thing in one line:
$Dir_name .= '\\' unless ($Dir_name =~ m![\\/]\z!);
Or even:
$Dir_name =~ s|(?<![\\/])\z|\\|;
:-)
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>