in reply to Re: how to count the no of repeats in a string
in thread how to count the no of repeats in a string

Why take the trouble of making the hash increment inside the pattern instead of in the loop? That kind of cheats the loop expression of its task. :-)

my %counts; $counts{$1}++ while $string =~ /([^,]+)/g;

lodin

Replies are listed 'Best First'.
Re^3: how to count the no of repeats in a string
by ikegami (Patriarch) on Nov 14, 2007 at 02:30 UTC

    Too true! I had started with

    our %counts; "$string," =~ /([^,]*),(?{ $counts{$1}++ })/g;

    When that didn't work, I should have taken a step backwards, not a step forward (fixing the //g in scalar context).

      A step forward (for some definition of forward) could've been

      local our %counts; $string =~ /(?:^|,)([^,]*)(?:,|\z)(?{ $counts{$1}++ })(?!)/;
      if one really wants to do it the regex way. ;-)

      lodin