in reply to check if string is valid with special cases
G'day ovedpo15,
You can do your validation with this condition:
$string =~ y/,/,/ == -1 + grep length y/ //dr, split /,/, $string
I ran these tests based on the ever-shifting goal posts throughout this thread. :-)
$ alias perle alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -E' $ perle 'my $x = "a,b,c"; say +($x =~ y/,/,/ == -1 + grep length y/ // +dr, split /,/, $x) ? "Y" : "N"' Y $ perle 'my $x = "a,b ,c"; say +($x =~ y/,/,/ == -1 + grep length y/ / +/dr, split /,/, $x) ? "Y" : "N"' Y $ perle 'my $x = "a, ,c"; say +($x =~ y/,/,/ == -1 + grep length y/ / +/dr, split /,/, $x) ? "Y" : "N"' N $ perle 'my $x = "a, ,c"; say +($x =~ y/,/,/ == -1 + grep length y/ // +dr, split /,/, $x) ? "Y" : "N"' N $ perle 'my $x = "a,,c"; say +($x =~ y/,/,/ == -1 + grep length y/ //d +r, split /,/, $x) ? "Y" : "N"' N
— Ken
|
---|
In Section
Seekers of Perl Wisdom