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"; } } }
In reply to Checking a path by FFSparky
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |