1: #>To get every nth element out of a list, use grep (one of the most under-appreciated built-ins?).</p>
   2: 
   3: (For every fourth element in @List)
   4: grep {not ++$i % 4} @List;
   5: 
   6: 
   7: #(To skip every fifth element)
   8: grep {++$i % 5} (1..50)</code>
   9: 
  10: #Simple, but it makes a cool one-liner if you need it...

Replies are listed 'Best First'.
RE: Get every nth element
by Adam (Vicar) on May 05, 2000 at 00:34 UTC
    #For every $n element in @List grep {not ++$i % $n} @List;
    This is kind of cool. You are right, grep is probably under used. I also like the use of pre-increment which will, as a bit of perl magic, initialize $i for you. (actually so will post increment but as it turns out, aside from the offset issue here, preincrement is generally more efficiant for the compiler).
      the pre-increment also serves to shift the effective array indexing from zero-based to one-based, allowing the modulus arithmetic to return 0 on the correct indices. if you used postincrement, then the modulus would be evaluated before the increment, causing the wrong elements to be chosen.
RE: Get every nth element
by Anonymous Monk on May 04, 2000 at 11:10 UTC
    Argh! Why doesn't preview show what it will *really* look like? Sheesh...