Forum Moderators: coopster & phranque

Message Too Old, No Replies

Ternary operator

         

csdude55

3:10 am on Oct 12, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've been coding in Perl for years, but have gotten out of practice lately :-( I'm hoping a second set of eyes can help me find my error.

This is what I have:

$comment = 'this is the w.o.r.d<br>OK';

$stripped = $comment =~ s#<.+?>##sg;
$workaround = $stripped =~ s#[^\w]##sg;

$return = $workaround =~ /word/i ? 1 : 2;

I thought this would work but it's returning "2" when I'm expecting "1".

Can you guys see where I'm messing up? TIA!

phranque

5:36 am on Oct 12, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



$stripped = $comment =~ s#<.+?>##sg;

are you trying to strip out the periods here?

$return = $workaround =~ /word/i ? 1 : 2;

have you checked the value of $workaround to verify it is what you expected?

csdude55

8:02 am on Oct 12, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



are you trying to strip out the periods here?


No, that line is removing any HTML code; eg, <font color='red'>this</font> would come back as this. Am I correct in understanding that in the next line, [^\w] would match the period?


have you checked the value of $workaround to verify it is what you expected?


Unfortunately, no. I'm working with a live program, and testing takes a little more time than I would like. I can, of course, but I was hoping someone here might look at it and see an obvious error before I went to that trouble :-)

phranque

9:53 am on Oct 12, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



No, that line is removing any HTML code

i misread that regexp on a small screen but upon rereading, everything there looks correct to me.

$workaround = $stripped =~ s#[^\w]##sg;

this is where your problem is.
the =~ operator binds a scalar expression to a pattern match and returns true if it matches.
therefore the value of $workaround will be "1" rather than the "stripped" string.

csdude55

10:21 am on Oct 12, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oh, I see. I was trying to combine 2 lines:

$workaround = $stripped;
$workaround =~ s#[^\w]##sg;

I thought that I had done it that way before, but it's been awhile so I could be mistaken :-(

Thanks, Phranque!