in reply to Skipping Elements in an Array

Golf?
print+grep+($c=$c<1&&/^..$/?3:$c)--<1,@Array # 44 chars.

Replies are listed 'Best First'.
Re^2: Skipping Elements in an Array
by sh1tn (Priest) on Apr 07, 2005 at 11:18 UTC
    /.../&&print||splice@_,0,2for@_ #31 chars


      My [update: broken] entry:
      (/^..$/...$_)||print for@Array # 30 chars, with named array

      Caution: Contents may have been coded under pressure.
        my @Array = ('AAA','BB','CCCC','DD','EEEEE','FFF','GGGG'); (/^..$/...$_)||print for@Array; __END__ AAAFFFGGGG
        But according to the OP, it should not exclude the 'EEEEE'. Your solution excludes the entry that has 2 characters, and the next one (or rather, until the next one that is true). The result of your program will change if you replace 'CCCC' with '0', or 'DD' with 'DDD'.
        Please, recheck your code - it is skipping more elements.

        Another 31 chars:
        print split' ..( \S+){3}',"@_"


      Problems:
      1. The array is called @Array, not @_.
      2. Modifying an array you're looping over isn't well defined.
      3. It will attempt to skip over 2 elements for all strings less than 3 characters, not just strings of exactly 2 characters.
        my @Array = ('AAA','BB','CCCC','DD','EEEEE','FFF','GGGG'); my @Skipped = split' ..( \S+){3}',"@Array"; print "@Skipped" __END__ STDOUT: AAA EEEEE FFF GGGG Right?


Re^2: Skipping Elements in an Array
by holli (Abbot) on Apr 07, 2005 at 11:19 UTC
    oops. And I thought I got it short.
    use strict; use warnings; my $num_of_skip = 2; my $skip = 0; my @Array = ('AAA','BB','CCCC','DD','EEEEE','FFF','GGGG'); my @Skipped = grep {$skip-- if $skip; $skip = $num_of_skip + 1 if !$sk +ip and length($_) == 2; !$skip; } @Array; print "original: @Array\n"; print "wanted: AAA EEEEE FFF GGGG\n"; print "output: @Skipped\n";

    so,
    grep{$skip--if$skip;$skip=3if!$skip&&length($_)==2;!$skip;}@_ #61 char +s


    holli, /regexed monk/