Finding out if a string has a trailing slash

S

Stan Brown

I need to check a user suplied string to see if it has a trailing slash, or
not.

I tried seomthing like:

if( $dirname =~ m/\/$/ )


But that's not working. What am I doing wrong?
 
B

Ben Morrow

Stan Brown said:
I need to check a user suplied string to see if it has a trailing slash, or
not.

I tried seomthing like:

if( $dirname =~ m/\/$/ )

You want to use m|/$| or so, here, to avoid Leaning Toothpick
Syndrome. That's the whole point of Perl's choose-your-own-quotes.
But that's not working. What am I doing wrong?

Worksforme:

% perl -le'print ("foo/" =~ m/\/$/)'
1
%

What else does your script say, and what is it doing that you consider
it to be 'not working'?

Ben
 
G

Gunnar Hjalmarsson

Stan said:
I need to check a user suplied string to see if it has a trailing
slash, or not.

I tried seomthing like:

if( $dirname =~ m/\/$/ )

But that's not working.

It works fine for me. What makes you say it's "not working"?
What am I doing wrong?

How would we know? If you post the code you tried, what you expect it
to do, and what it actually does, somebody here may be able to tell
you what's wrong with it.
 
T

Tad McClellan

Stan Brown said:
I need to check a user suplied string to see if it has a trailing slash, or
not.

I tried seomthing like:

if( $dirname =~ m/\/$/ )


But that's not working.


Yes it is.

-------------------
#!/usr/bin/perl
use strict;
use warnings;

my $dirname = 'foobar/';
if( $dirname =~ m/\/$/ ) {
print "matched\n";
}
-------------------


Works for me.

What am I doing wrong?


Not providing a short and complete program that we can run, as
suggested in the Posting Guidelines that are posted here twice
each week, and as suggested directly to you in previous followups.

Are you trying for your own personal scorefile entry?
 
D

David Efflandt

I need to check a user suplied string to see if it has a trailing slash, or
not.

I tried seomthing like:

if( $dirname =~ m/\/$/ )


But that's not working. What am I doing wrong?

Perhaps $dirname ends with something invisible like a carriage return on
an OS that does not recognize that as part of newline. See if this works:

$dirname =~ s/\s*$//;
if ($dirname =~ m|/$|) {
print "trailing slash\n";
}
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I need to check a user suplied string to see if it has a trailing
slash, or not.

I tried seomthing like:

if( $dirname =~ m/\/$/ )


But that's not working. What am I doing wrong?

"Not working" is amazingly vague and non-informative. If you want people
to help you, you must provide more detail. Do you take your car to the
mechanic and say "It's not working. What's wrong"?

Come on now.

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP59E3GPeouIeTNHoEQKrWQCfYMH6HJiQW9OIlYWjqTypxJUCC/cAoNZ2
LHBq5hT4Kwr4HBbC7HO+Vfiu
=bLV3
-----END PGP SIGNATURE-----
 
S

Stan Brown

You want to use m|/$| or so, here, to avoid Leaning Toothpick
Syndrome. That's the whole point of Perl's choose-your-own-quotes.

% perl -le'print ("foo/" =~ m/\/$/)'
1
%
What else does your script say, and what is it doing that you consider
it to be 'not working'?


Thanks for the help, it was a stupid msitake on my part. I had 2 similar
variable names, and I confised them.

Thanks, again.

And, yes I changed the quotes to make it clearer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top