monkfan has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have the following problem, given a list of array perform this rule:

"if the length of element being evaluated is equal to 2 then skip next 2 element"

So it gives this result: AAA,EEEEE,FFF,GGGG
Currently the code I have is this, which is incorrect.
#! /usr/bin/perl -w use strict; use Data::Dumper; my @Array = ('AAA','BB','CCCC','DD','EEEEE','FFF','GGGG'); my $num_of_skip =2; for (0..$#Array){ #How can i make use of $num_of_skip here? print "$Array[$_]\n" if (length $Array[$_] !=2); }
Thanks so much again beforehand.
Regards,
Edward

Replies are listed 'Best First'.
Re: Skipping Elements in an Array
by vladdrak (Monk) on Apr 07, 2005 at 08:34 UTC
    You could do this:
    #!/usr/bin/perl use strict; use Data::Dumper; my @Array = ('AAA','BB','CCCC','DD','EEEEE','FFF','GGGG'); my $num_of_skip =2; for (my $i; $i <= $#Array; $i++) { if (length $Array[$i] == $num_of_skip) { $i=$i+2; } else { print "$Array[$i]\n"; } }
Re: Skipping Elements in an Array
by reasonablekeith (Deacon) on Apr 07, 2005 at 08:36 UTC
    I'm not sure exactly what you're trying to do here, but you probably just need to just have an explicit index you can manipulate. Does this do what you're after?
    my @Array = ('AAA','BB','CCCC','DD','EEEEE','FFF','GGGG'); my $num_of_skip =2; for (my $index =0; $index <= $#Array; $index++){ if (length($Array[$index]) == 2) { $index += $num_of_skip; } else { print "$Array[$index]\n"; } }
Re: Skipping Elements in an Array
by Zaxo (Archbishop) on Apr 07, 2005 at 08:48 UTC

    You can keep a flag to say that an element should be skipped. It appears that you also want to skip the triggering element, and that a skipped length-two element does not trigger. Those properties will be controlled by the order of test and flag manipulations.

    #! /usr/bin/perl -w use strict; my @Array = ('AAA','BB','CCCC','DD','EEEEE','FFF','GGGG'); my $num_of_skip =2; my $skipping = 0; for (@Array) { $skipping++, next if $skipping and not $skipping > $num_of_skip; $skipping++, next if 2 == length; print $_, $/; $skipping = 0; }

    After Compline,
    Zaxo

Re: Skipping Elements in an Array
by gube (Parson) on Apr 07, 2005 at 09:01 UTC

    Simple try this,

    my @a = ('AAA','BB','CCCC','DD','EEEEE','FFF','GGGG'); for (my $i; $i <= $#a; $i++) { if (length $a[$i] == 2) { $i=$i+2; } else { print "$a[$i]\n"; } }

    Regards,
    Gubendran.L
Re: Skipping Elements in an Array
by Anonymous Monk on Apr 07, 2005 at 10:11 UTC
    Golf?
    print+grep+($c=$c<1&&/^..$/?3:$c)--<1,@Array # 44 chars.
      /.../&&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.
        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.
      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/
Re: Skipping Elements in an Array
by Taulmarill (Deacon) on Apr 07, 2005 at 08:29 UTC
    how about this?
    for (0..$#Array){ next if length $Array[$_] == $num_of_skip; print "$Array[$_]\n"; # do some other cool stuff }
      Hi Taumarill,

      Thanks for the reply. But:

      1. In your snippet each element is "still evaluated", the intention is "not" to evaluate skipped element.

      2. Length of array element may or "may not" be equal to $num_of_skip.
      Regards,
      Edward
Re: Skipping Elements in an Array
by sh1tn (Priest) on Apr 07, 2005 at 08:57 UTC
    { local @_ = ('AAA','BB','CCCC','DD','EEEEE','FFF','GGGG'); local $_ = 0; for ( $_..$#_ ){ length($_[$_]) == 2 and next; length($_[$_-1]) == 2 and next; print $_[$_],$/ } } __END__ AAA FFF GGGG

    Update: if you mean 3 (not 2) elements:
    { local @_ = ('AAA','BB','CCCC','DD','EEEEE','FFF','GGGG'); local $_ = 0; while ( $_ <= $#_ ){ length($_[$_]) == 2 and $_+=3; print+($_[$_],$/) and $_++ } } __END__ AAA EEEEE FFF GGGG


Re: Skipping Elements in an Array
by ysth (Canon) on Apr 07, 2005 at 16:49 UTC
    Yet another way:
    my @Array = ('AAA','BB','CCCC','DD','EEEEE','FFF','GGGG'); my $num_of_skip =2; my $skip; for (@Array) { next if $skip-- > 0 || 2==length && ($skip=$num_of_skip); ... }
Re: Skipping Elements in an Array
by tweetiepooh (Hermit) on Apr 07, 2005 at 09:33 UTC
    This seems to do what you need
    #!/usr/local/bin/perl my @array = qw(AAA BB CCCC DD EEEEE FFF GGGG); my $flag = 0; foreach (@array) { next if $flag-- > 0; if (length($_) == 2) { $flag=1; next; } print "$_\n"; }
      change the flag to 2 to skip 2 elements
      if ... $flag=2; ...
Re: Skipping Elements in an Array
by dmorelli (Scribe) on Apr 07, 2005 at 13:20 UTC
    #! /usr/bin/perl -w use strict; my @Array = ('AAA','BB','CCCC','DD','EEEEE','FFF','GGGG'); my $num_of_skip = 2; # Keep and increment your own index. for changes the loop var on you my $i = 0; while ($i < @Array) { # Adjust the index if necessary # Q: How can i make use of $num_of_skip here? A: Like this: $i += $num_of_skip + 1 if (length ($Array[$i]) == 2); # Notice we're post-incrementing the index here too print "$Array[$i++]\n"; }
      You need a next in your if block.
      if (length ($Array[$i]) == 2) { $i += $num_of_skip + 1; next; }
      This will do 2 things for you: it will keep you from printing an element were length == 2 and will keep you from having to worry about the post increment.

      -- gam3
      A picture is worth a thousand words, but takes 200K.