What is the difference ?

Discussion in 'Perl Misc' started by Che, May 23, 2004.

  1. Che

    Che Guest

    Hi:
    What is the difference between
    my $str_3 = "c:\windows"; <---- (1)
    my $str_3 = 'c:\windows'; <---- (2)

    If I use (1) the "if" condition fails and if i use (2) the "if" condition is true.


    (1) or (2) here followe by this code.
    if( $str_3 =~ m!c:\\windows! )
    {
    print"\n matches";
    }
    else
    {
    print"\n It doesn't match";
    }

    Thakns
     
    Che, May 23, 2004
    #1
    1. Advertisements

  2. Che

    AlV Guest

    Che wrote:
    > Hi:
    > What is the difference between


    Should be obvious from the below example...

    my $str_1 = "c:\windows";
    my $str_2 = "c:\\windows";
    my $str_3 = 'c:\windows';

    if( $str_1 =~ m!c:\\windows! )
    {
    print"\n matches";
    }
    else
    {
    print"\n It doesn't match";
    }

    if( $str_2 =~ m!c:\\windows! )
    {
    print"\n matches";
    }
    else
    {
    print"\n It doesn't match";
    }

    if( $str_3 =~ m!c:\\windows! )
    {
    print"\n matches";
    }
    else
    {
    print"\n It doesn't match";
    }

    print "\n";
     
    AlV, May 23, 2004
    #2
    1. Advertisements

  3. Che wrote:
    > What is the difference between
    > my $str_3 = "c:\windows"; <---- (1)
    > my $str_3 = 'c:\windows'; <---- (2)


    perldoc perlintro
    perldoc perlop

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, May 23, 2004
    #3
  4. Che wrote:
    > Hi:
    > What is the difference between
    > my $str_3 = "c:\windows"; <---- (1)
    > my $str_3 = 'c:\windows'; <---- (2)


    One backslash.

    jue
     
    Jürgen Exner, May 24, 2004
    #4
  5. Che <> wrote:


    > What is the difference between



    print() the values and see what the difference is for yourself!

    You don't really need the help of hundreds of people around the world
    to do that you know.


    > my $str_3 = "c:\windows"; <---- (1)



    print "[$str_3]\n";


    > my $str_3 = 'c:\windows'; <---- (2)



    print "[$str_3]\n";

    >
    > If I use (1) the "if" condition fails



    The string contains no backslash character, the pattern requires a
    backslash character. The match must fail.


    If you had asked perl for some help, then perl would have
    happily given you some help, and you would have discovered
    the cause of the problem in a few microseconds.

    How many microseconds have you spent on it so far?

    Don't waste human time on a machine's job, just ask the
    machine to do the job for you:


    use warnings;


    > and if i use (2) the "if" condition is true.



    As it should be.


    > (1) or (2) here followe by this code.
    > if( $str_3 =~ m!c:\\windows! )
    > {
    > print"\n matches";
    > }
    > else
    > {
    > print"\n It doesn't match";
    > }




    You don't have to use \silly\ slashes unless it is destined for
    a Win command interpreter. /sane/ slashes work fine on Windows
    and it avoids these kinds of problems...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, May 24, 2004
    #5
    1. Advertisements

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. Mark Kamoski
    Replies:
    3
    Views:
    16,523
    Jay B. Harlow [MVP - Outlook]
    Aug 9, 2003
  2. Gopal Prabhakaran

    Difference between HtmlControls and server controls

    Gopal Prabhakaran, Jan 26, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,588
    Rainer Maurer [MS]
    Jan 26, 2004
  3. hclarius
    Replies:
    1
    Views:
    2,506
    Ed Kaim [MSFT]
    Jul 4, 2004
  4. walala
    Replies:
    3
    Views:
    1,173
    Egbert Molenkamp
    Sep 4, 2003
  5. jakk
    Replies:
    4
    Views:
    13,929
  6. Peter Otten
    Replies:
    1
    Views:
    216
    Mark Lawrence
    Feb 25, 2014
  7. Tim Chase
    Replies:
    5
    Views:
    145
    Tim Chase
    Feb 26, 2014
  8. MRAB
    Replies:
    1
    Views:
    119
    Steven D'Aprano
    Feb 26, 2014
Loading...