Concatenation: $i.$j different from "$i$j"

O

Occidental

{
my $i = "TT";
my $j = "AT";

if ("$i$j" =~ /X/)
{
print "$i$j matched\n";
}
else
{
print "$i$j not matched\n";
}
}

{
my $i = "TT";
my $j = "AT";

if ($i.$j =~ /X/)
{
print $i.$j . " matched\n";
}
else
{
print $i.$j . " not matched\n";
}
}

gives

TTAT not matched
TTAT matched

Can anyone explain?
 
X

xhoster

Occidental said:
if ($i.$j =~ /X/)
Can anyone explain?

Precedence can. =~ is two lines above . in the perlop list of
precedences.

$ perl -MO=Deparse,-p -e ' "x"."y" =~ /f/'
('x' . ('y' =~ /f/));
-e syntax OK

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
J

John W. Krahn

Occidental said:
{
my $i = "TT";
my $j = "AT";

if ("$i$j" =~ /X/)
{
print "$i$j matched\n";
}
else
{
print "$i$j not matched\n";
}
}

{
my $i = "TT";
my $j = "AT";

if ($i.$j =~ /X/)
{
print $i.$j . " matched\n";
}
else
{
print $i.$j . " not matched\n";
}
}

gives

TTAT not matched
TTAT matched

Can anyone explain?

Yes, perl can:

$ perl -MO=Deparse,-p -e'
{
my $i = "TT";
my $j = "AT";

if ("$i$j" =~ /X/)
{
print "$i$j matched\n";
}
else
{
print "$i$j not matched\n";
}
}

{
my $i = "TT";
my $j = "AT";

if ($i.$j =~ /X/)
{
print $i.$j . " matched\n";
}
else
{
print $i.$j . " not matched\n";
}
}
'
{
(my $i = 'TT');
(my $j = 'AT');
if (("$i$j" =~ /X/)) {
print("$i$j matched\n");
}
else {
print("$i$j not matched\n");
}
}
{
(my $i = 'TT');
(my $j = 'AT');
if (($i . ($j =~ /X/))) {
print((($i . $j) . " matched\n"));
}
else {
print((($i . $j) . " not matched\n"));
}
}
-e syntax OK


if (("$i$j" =~ /X/)) {

Is false because there is no 'X' in 'TTAT'.

if (($i . ($j =~ /X/))) {

Is true because the string from the expression ($i . ($j =~ /X/)) is true.



John
 
D

Dr.Ruud

Occidental schreef:
if ("$i$j" =~ /X/)
if ($i.$j =~ /X/)

I heard somewhere that the next version of Python will make

1+2 * 3

behave differently from

1 + 2*3

;)
 
D

Dr.Ruud

Abigail schreef:
Dr.Ruud:

If you had written 'Perl6' instead of 'Python', I would have assumed
you were serieus.

Hey, I was just aiming for a bigger publiek.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top