Finding out if a string has a trailing slash

Discussion in 'Perl Misc' started by Stan Brown, Oct 28, 2003.

  1. Stan Brown

    Stan Brown Guest

    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
     
    Stan Brown, Oct 28, 2003
    #1
    1. Advertising

  2. Stan Brown

    Ben Morrow Guest

    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/\/$/ )


    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. |-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-|
     
    Ben Morrow, Oct 28, 2003
    #2
    1. Advertising

  3. 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
     
    Gunnar Hjalmarsson, Oct 28, 2003
    #3
  4. 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.



    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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 28, 2003
    #4
  5. On Tue, 28 Oct 2003 20:23:28 +0000 (UTC), 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. 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/
     
    David Efflandt, Oct 29, 2003
    #5
  6. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Stan Brown <> wrote in
    news:bnmj80$2kq$:

    > 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-----
     
    Eric J. Roode, Oct 29, 2003
    #6
  7. Stan Brown

    Stan Brown Guest

    In <bnmjsa$kpl$> Ben Morrow <> writes:

    >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/\/$/ )


    >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
     
    Stan Brown, Oct 29, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    319
    Max Erickson
    Nov 10, 2006
  2. abcd
    Replies:
    7
    Views:
    350
    Ben Finney
    Mar 15, 2007
  3. Harlan Messinger

    ASP.NET inserts trailing slash in tag

    Harlan Messinger, Feb 18, 2008, in forum: ASP .Net
    Replies:
    4
    Views:
    477
    Harlan Messinger
    Feb 19, 2008
  4. Jeff Mitchell

    trailing slash issue in Find.find

    Jeff Mitchell, Aug 24, 2003, in forum: Ruby
    Replies:
    0
    Views:
    110
    Jeff Mitchell
    Aug 24, 2003
  5. Sebastian Bauer

    HTTP::Request, trailing slash

    Sebastian Bauer, Jun 29, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    189
    Sebastian Bauer
    Jul 1, 2004
Loading...

Share This Page