What is the difference ?

C

Che

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
 
A

AlV

Che said:
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";
 
G

Gunnar Hjalmarsson

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

perldoc perlintro
perldoc perlop
 
T

Tad McClellan

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

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,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top