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

Ok folks, before anyone calls me on it, yes this is homework. I'm trying to extract all odd and even numbers to separate lists. I thought something like this would work, but it is not:
foreach (@nums){ push (@even, $_) if /\/2/; #match all numbers that are even push (@odd, $_) if /^\/2/; #match all odd numbers }
Any help is appreciated.

Replies are listed 'Best First'.
Re: Match all Odd and Even Numbers
by Bilbo (Pilgrim) on Jun 17, 2003 at 13:01 UTC

    What you have written is

    foreach (@nums){ push (@even, $_) if /\/2/; #match strings that contain '/2' push (@odd, $_) if /^\/2/; #match strings that start '/2' }

    Try looking at the % (modulo) operator.

Re: Match all Odd and Even Numbers
by broquaint (Abbot) on Jun 17, 2003 at 13:01 UTC
    Take a look into the modulus operator and that should start you on the right track.
    HTH

    _________
    broquaint

Re: Match all Odd and Even Numbers
by diotalevi (Canon) on Jun 17, 2003 at 13:46 UTC

    I'm posting this just because the idiom is nifty, not because I want to help with your homework.

    push @{$_ & 1 ? \ @odd : \ @even }, $_ for @nums;

    Added

    @nums = 1..20; /.*(?=.)([13579]$)?(?(1)(?{${"\@nums::${_}::"}++})|(?{${"\@nums::${_}: +:"}--}))/ for @nums; printf "%d %s\n", $_, ${"\@nums::$_"} > 0 ? odd : even for keys %{"\@n +ums::"};
Re: Match all Odd and Even Numbers
by particle (Vicar) on Jun 17, 2003 at 12:58 UTC

    you'll probably find perl's arithmetic operators more handy than regular expressions. look in perlop for more info. you're on the right track, anyway.

    ~Particle *accelerates*

Re: Match all Odd and Even Numbers
by gellyfish (Monsignor) on Jun 17, 2003 at 14:02 UTC

    Just for amusement you might be interested in reading This thread in CLPM that gives this subject a thorough examination.

    /J\
    
Re: Match all Odd and Even Numbers
by hardburn (Abbot) on Jun 17, 2003 at 14:03 UTC

    Just contributing a little golf.

    my @nums = (0 .. 99); my $odd_even = [ ]; push @{ $odd_even->[$_ & 1] }, $_ for (@nums);

    The above actually has been tested. $odd_even->[0] contains a list of even numbers, and $odd_even->[1] contains the odd numbers. You can split them into seperate arrays like this:

    my @even = @{ $odd_even->[0] }; my @odd = @{ $odd_even->[1] };

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: Match all Odd and Even Numbers
by benn (Vicar) on Jun 17, 2003 at 13:14 UTC
    ++Dru for (a) admitting it's homework, and (b) posting code that you'd already tried. A good example to many...:)

    Cheers, Ben.

Re: Match all Odd and Even Numbers
by Dr. Mu (Hermit) on Jun 18, 2003 at 06:49 UTC
    ++ to all who gave encouragement and/or pointers to additional info. -- to all who posted an answer. Hey guys, sometimes pedagogy needs to take precedence over ego!
      Thanks for all your guidance. I got it working using this code:
      for (@nums){ if ($_ % 2) { push (@odd, $_); } else { push (@even, $_); } }

        Do you mind to use below code

        my $num= [1 .. 100]; my (@even,@odd); map{0==$_%2 ? push(@odd,$_):push(@even,$_)} @$num; print "@even and @odd";