@list = qw(a b c d e);
#to get even numbers
#method 1:
my $t = 1;
my @test = grep { $t^=1 } @list;
print "@test\n\n";
#Method 2:
@list = qw(a b c d e);
my $t = 1;
my @test = grep { $t = 1-$t } @list;
print "@test\n\n";
#Method 3:
my $t = 1;
my @test = grep { ++$t % 2 } @list;
print "@test\n\n";
#perl -le "$t=1; print grep { $t^=1 } qw( a b c d e );
#perl -le"@a = qw( a b c d e ); print map { $_%2 ? $a[$_] : () } 0..$#
#to get odd numbers
#method 1:
@list = qw(a b c d e);
my $t = 0;
my @array = grep { $t^=1 } @list;
print "@array";
#perl -le"$t=0; print grep { $t^=1 } qw( a b c d e );"
#to get even numbers
#method 1:
my $t = 1;
my @test = grep { $t^=1 } @list;
print "@test\n\n";
#Method 2:
@list = qw(a b c d e);
my $t = 1;
my @test = grep { $t = 1-$t } @list;
print "@test\n\n";
#Method 3:
my $t = 1;
my @test = grep { ++$t % 2 } @list;
print "@test\n\n";
#perl -le "$t=1; print grep { $t^=1 } qw( a b c d e );
#perl -le"@a = qw( a b c d e ); print map { $_%2 ? $a[$_] : () } 0..$#
#to get odd numbers
#method 1:
@list = qw(a b c d e);
my $t = 0;
my @array = grep { $t^=1 } @list;
print "@array";
#perl -le"$t=0; print grep { $t^=1 } qw( a b c d e );"
No comments:
Post a Comment