[Israel.pm] Case Study: Removing all Periods ("."'s) but the Last

Shlomi Fish shlomif at iglu.org.il
Wed Dec 8 01:59:55 PST 2004


On Wednesday 08 December 2004 08:24, Jesse P Kretschmer wrote:
> I really like a couple of those options, but I have never seen
> ($c++)?"":"foo".  I also see you have a (--$count)?"":"foo" .  Could you
> explain these?  I would really like to understand this. 

Sure!

First of all the expression $c++ increments the value of the $c variable by 
one and returns its value before it is incremented. In our case:

<<<
sub sexeger
{
    my $s = shift;
    $s=reverse($s);
    my $c = 0;
    $s=~s!\.!($c++)?"":"."!ge;
    return reverse($s);
}
>>>

$c is 0 initially, so the first time this sub-expression will return 0, and it 
will return 1, 2, 3, etc. in subsequenet times. Now the 
"$cond ? $if_part : $else_part" is a mini-conditional. It will evaluate to 
$if_part if $cond is true and $else_part if it is false. Since $c++ is false 
only the first time, then ($c++)?"":"." will be evaluated to "." in the first 
time and to the empty string all other times.

The (--$count) thing is similar only that it will be evaluated to false only 
at the $count'th time, and to true all time before (or after, when $count is 
negative). --$count returns the expression after decrementation.

Regards,

	Shlomi Fish

---------------------------------------------------------------------
Shlomi Fish      shlomif at iglu.org.il
Homepage:        http://www.shlomifish.org/

Knuth is not God! It took him two days to build the Roman Empire.



More information about the Perl mailing list