Sunday, 11 August 2013

Getting odd numbers from the array

Getting odd numbers from the array

To get the odd numbers from the array i got this code but didn't
understand one syntax. Code is as below
#!/usr/bin/perl
use strict;
use warnings;
# initialize an array
my @array = qw(3 4 5 6 7 8 9);
my @subArray = ();
foreach (@array) {
push @subArray, $_ if $_ & 1;
}
print "@subArray\n";
# displays: 3 5 7 9
What is "if $_ & 1" This is working. But i didn't get the syntax "if $_ & 1".
Similarly grep can be used like "my @subArray = grep $_ & 1, @array; "
Please help me understanding the syntax

No comments:

Post a Comment