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. Advertising

  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. Advertising

  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. 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. jakk
    Replies:
    4
    Views:
    12,635
  2. Nick Timkovich
    Replies:
    0
    Views:
    86
    Nick Timkovich
    Feb 25, 2014
  3. Skip Montanaro
    Replies:
    0
    Views:
    84
    Skip Montanaro
    Feb 25, 2014
  4. Peter Otten
    Replies:
    1
    Views:
    103
    Mark Lawrence
    Feb 25, 2014
  5. Peter Otten
    Replies:
    0
    Views:
    75
    Peter Otten
    Feb 25, 2014
Loading...

Share This Page