On Fri, Jul 10, 2009 at 2:44 AM, Yitzchak Scott-Thoennes<sthoenna@efn.org> wrote: > On Thu, July 9, 2009 2:34 pm, Aristotle Pagaltzis wrote: >> * Michael G Schwern <schwern@pobox.com> [2009-07-09 23:05]: >>> Yitzchak said: >>> >>>> I don't get why you'd want to do that. If I wanted to count >>>> space-separated fields, I'd do: >>>> >>>> $count = 1 + $thing =~ y/ //; >>> >>> Yitzchak's altnerative approach is very clever but underscores >>> the need for making split in scalar context just DWIM. Because... damn, >>> that's how you're supposed to count fields? >> >> That only works if the field separator is exactly one character, >> anyway. OTOH `1 + $str =~ m//g` works in the general case and is far less >> unintuitive. > > I was trying to give the IMO underappreciated y/// some love. It's hard > to say for sure what I'd really do, since I find it hard to imagine > counting fields without actually wanting the fields: > > @fields = split ...; > $field_count = @fields; > > but if it actually came up in practice, I might just: > > $field_count = () = split ...; > > I'm not completely satisfied that "fixing" scalar context is the right > thing to do; just like with sort, where there are several "right" scalar > context behaviors. > > With my C hat on, I'd expect it to return the first field, like strtok. > Without it, I'd be as inclined to expect the last field as to expect > a count. Considering easy ability and well known ability to get first element [1], it's the right thing to return count. [1] ($first) = split ...; Also, sort and split can not be compared cuz the former can not affect number of elements when another can. -- Best regards, Ruslan.Thread Previous | Thread Next