Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Checking a path

by FFSparky (Acolyte)
on Nov 18, 2009 at 16:58 UTC ( #807954=perlquestion: print w/replies, xml ) Need Help??

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
    This is less code. It appends a trailing backslash only if your string does not have one. You must decide whether it is easier for you to understand than the code you already have:
    $TestStrings{$TestPatternKey} =~ s/\\?$/\\/;
Re: Checking a path
by keszler (Priest) on Nov 18, 2009 at 17:15 UTC
    $TestStrings{$TestPatternKey} .= "\\" if $TestStrings{$TestPatternKey} + !~ /\\$/;
Re: Checking a path
by bellaire (Hermit) on Nov 18, 2009 at 17:17 UTC
    Using substr like you have in your example probably is the most efficient way to go about this. Usually, though, this kind of micro-efficiency isn't something you need to worry about unless you've done profiling and you know that you have good reason to seek an improvement.
      Note that for more concision the last character of the string can be obtained by:
      substr($TestStrings{$TestPatternKey}, -1)
Re: Checking a path
by FFSparky (Acolyte) on Nov 18, 2009 at 17:37 UTC
    Thanks for the wisdom!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://807954]
Approved by Tanalis
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2020-02-26 11:30 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (114 votes). Check out past polls.