in reply to Re^2: Check last character of a string
in thread Check last character of a string

For the sake of completeness the code I used to verify that it worked with the "eq" operator. And for the record: I'd prefer a RegExp myself in this situation;-)

use strict; use warnings; my $location = "some location\\"; my $var = substr($location,length($location)-1,1); if ($var eq "\\"){ print "HIER IS ES\n\n"; } else{ $location = $location."\\"; print "NEU:: $location\n\n" ; }

Replies are listed 'Best First'.
Re^4: Check last character of a string
by LesleyB (Friar) on Aug 07, 2008 at 11:48 UTC

    For the sake of a shorter program, one less function call, a negated regexp and completely ignoring the one-liner posted Corion very early in this thread

    use strict; use warnings; my $location = "some location"; my $dirchar = "\\"; $location = $location.$dirchar if ($location !~ /{$dirchar}$/); print "Hier ist es : ".$location."\n";

    I may have approached the problem the same way as you are when I first started learning Perl. Now I would tend to use the above. I don't find regexp's easy but perlrequick is a good starting point before moving on to doc::/perlre and Mastering Regular Expressions