Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2019-11-18 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (90 votes). Check out past polls.

    Notices?