FFSparky has asked for the wisdom of the Perl Monks concerning the following question:
Just curious to know if there would be a more efficient way to interrogate a string that contains a path to ensure the last character contains a slash.
Please excuse the full source code below, I have a shell I use when testing out things, the real code I have to do what I am seeking is:
first checking the last character
if (substr($TestStrings{$TestPatternKey},length($TestStrings{$TestPatternKey})-1,1) ne "\\")
And then if necessary adding the slash
$TestStrings{$TestPatternKey} .= "\\";".
Full Code
use warnings; use strict; use File::Basename; my $Script = $0; my $ScriptPath = dirname($Script); my $ScriptName = basename($Script, ".pl").".pl"; my $ScriptVersion = "1.0.0"; my %TestStrings = ("00001" => "C:\\Test1", "00002" => "C:\\Test2\\", ); my $TestPatternKey = ''; print "\n$ScriptName \n\n"; print "\n Loop through the test Strings in the \%TestStrings Hash:"; foreach $TestPatternKey (sort keys %TestStrings) { if (length($TestStrings{$TestPatternKey}) > 0) { printf("\n\n \$TestPatternKey: %-6s \$TestPatternKeyValue: + %-40s \n",$TestPatternKey,$TestStrings{$TestPatternKey}); if (substr($TestStrings{$TestPatternKey},length($TestStrings{$ +TestPatternKey})-1,1) ne "\\") { $TestStrings{$TestPatternKey} .= "\\"; print " Corrected: +$TestStrings{$TestPatternKey} \n\n"; } else { print "\n"; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Checking a path
by toolic (Bishop) on Nov 18, 2009 at 17:17 UTC | |
|
Re: Checking a path
by keszler (Priest) on Nov 18, 2009 at 17:15 UTC | |
|
Re: Checking a path
by bellaire (Hermit) on Nov 18, 2009 at 17:17 UTC | |
by AnomalousMonk (Archbishop) on Nov 18, 2009 at 17:46 UTC | |
|
Re: Checking a path
by FFSparky (Acolyte) on Nov 18, 2009 at 17:37 UTC |