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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.