Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Check last character of a string

by theroninwins (Friar)
on Aug 06, 2008 at 09:35 UTC ( #702579=note: print w/replies, xml ) Need Help??

in reply to Check last character of a string

This is what I treid before:
my $var = substr($location,length($location)-1,1); print "\n\nVAR::: $var\n\n"; if ($var == "\\"){ print "HIER IS ES\n\n"; } else{ $location = $location+"\\"; print "NEU:: $location\n\n" ; }

Replies are listed 'Best First'.
Re^2: Check last character of a string
by Corion (Patriarch) on Aug 06, 2008 at 09:38 UTC

    Did you see any warnings? Warnings help you spot problematic points in your code.

    Perl does not compare strings for equality using ==. Maybe you want to use eq?

    Perl does not use + to concatenate strings. Maybe you want to use . ?

Re^2: Check last character of a string
by dHarry (Abbot) on Aug 06, 2008 at 09:44 UTC

    The "==" hurts my eyes;-) If you change it into "eq" it will work my son.

Re^2: Check last character of a string
by LesleyB (Friar) on Aug 06, 2008 at 09:46 UTC


    I'm still plodding along in 5.8 not even tried 5.10 yet so if ($var == "\\") doesn't even look like a regular expression to me and I know that I use strncmp in C.

    And the logic is wrong; even using eq you'll find that logic will test if your string is equal to "\" and you originally said you wanted to test whether the end of the string contained '\'.

    Try using if ($var =~ /\\$/){ instead.

    See perlrequick for an explanation of the $.

    Update: Sorry didn't read the code properly to realise you were grabbing the last character of the string before checking it. The '==' threw me, Your Honour! I don't think it is necessary to do that. The above regexp should work for any string and should detect a '\' at the end of the whole string. It should work anyway but you don't need to strip that char out to examine it.

      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" ; }

        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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://702579]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2022-01-19 02:35 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (55 votes). Check out past polls.