Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Finding out if a string has a trailing slash

Reply
Thread Tools

Finding out if a string has a trailing slash

 
 
Stan Brown
Guest
Posts: n/a
 
      10-28-2003
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?

--
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
-- Benjamin Franklin
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      10-28-2003
Stan Brown <(E-Mail Removed)> wrote:
> 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

--
I've seen things you people wouldn't believe: attack ships on fire off the
shoulder of Orion; I've watched C-beams glitter in the darkness near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die. |-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-| http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      10-28-2003
Stan Brown wrote:
> 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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-28-2003
Stan Brown <(E-Mail Removed)> wrote:
> 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?


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
David Efflandt
Guest
Posts: n/a
 
      10-29-2003
On Tue, 28 Oct 2003 20:23:28 +0000 (UTC), Stan Brown <(E-Mail Removed)> wrote:
> 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";
}


--
David Efflandt - All spam ignored http://www.de-srv.com/
http://www.autox.chicago.il.us/ http://www.berniesfloral.net/
http://cgi-help.virtualave.net/ http://hammer.prohosting.com/~cgi-wiz/
 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      10-29-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Stan Brown <(E-Mail Removed)> wrote in
news:bnmj80$2kq$(E-Mail Removed):

> 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/AwUBP59E3GPeouIeTNHoEQKrWQCfYMH6HJiQW9OIlYWjqTypxJ UCC/cAoNZ2
LHBq5hT4Kwr4HBbC7HO+Vfiu
=bLV3
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Stan Brown
Guest
Posts: n/a
 
      10-29-2003
In <bnmjsa$kpl$(E-Mail Removed)> Ben Morrow <(E-Mail Removed)> writes:

>Stan Brown <(E-Mail Removed)> wrote:
>> 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'?



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.


--
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
-- Benjamin Franklin
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET inserts trailing slash in tag Harlan Messinger ASP .Net 4 02-19-2008 01:15 PM
Multiline code - trailing slash usage abcd Python 7 03-15-2007 04:27 PM
path.py and directory naming: trailing slash automatic? metaperl.mogd@gmail.com Python 1 11-10-2006 05:03 PM
HTTP::Request, trailing slash Sebastian Bauer Perl Misc 9 07-01-2004 06:19 PM
trailing slash issue in Find.find Jeff Mitchell Ruby 0 08-23-2003 11:30 PM



Advertisments