in reply to Re: Regex help
in thread Regex help

I can't believe you missed out on this opportunity to wave (--) the magic wand variable ($|) to get only the odd or even elements of a list:
print grep --$|,($|||=1)&& /\b(\w+)\b(?=(.*?)\b\1\b)/g'

Replies are listed 'Best First'.
Re: Re: Re: Regex help
by BUU (Prior) on Jan 30, 2004 at 08:03 UTC
    What the good lord does that code do? Or rather, how does it work? Whats the $|||=1 bit, etc?
      That bit is just charging up the magic wand. It can just as well be a separate statement:
      $| = 1; print grep --$|, /\b(\w+)\b(?=(.*?)\b\1\b)/g'
      To get even elements instead of odd elements, you just start with the magic wand in the other position:
      print grep --$|,($|&&=0)|| /\b(\w+)\b(?=(.*?)\b\1\b)/g'
      or:
      $| = 0; print grep --$|, /\b(\w+)\b(?=(.*?)\b\1\b)/g'
      Why ||= and &&= instead of = is left as an excercise for the reader. Hope that clears things up for you :)