Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

"..I have to create the subroutine that will take the even and odd numbers from an array.."

A good compact solution has been showed by tobyink.
However, you might find the following also useful:

use warnings; use strict; my $numbers = [ 1 .. 10 ]; print even($numbers); ## call even number generator subroutine print $/; print odd($numbers); ## call odd number generator subroutine print $/; ## call combined even and odd number generator subroutine my %even_n_odd_numbers = %{ even_odd($numbers) }; for ( keys %even_n_odd_numbers ) { print $_, " ", join " ", @{ $even_n_odd_numbers{$_} }, $/; } sub even { my ($numbers) = @_; my @even_numbers; for (@$numbers) { push @even_numbers, $_ if $_ % 2 == 0; } return @even_numbers if wantarray; } sub odd { my ($numbers) = @_; my @odd_numbers; for (@$numbers) { push @odd_numbers, $_ if $_ % 2 == 1; } return @odd_numbers if wantarray; } sub even_odd { my ($numbers) = @_; my %even_odd = ( 'even' => undef, 'odd' => undef, ); for (@$numbers) { if ( $_ % 2 == 0 ) { push @{ $even_odd{'even'} }, $_; } else { push @{ $even_odd{'odd'} }, $_; } } return \%even_odd; }
The combined subroutine is a lot better, because you don't have to repeat codes lines like one did in two different subroutines ( sub even{...} and sub odd{...}) having just only one line that is different from one another.

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

In reply to Re: Subroutine Even/Odd by 2teez
in thread Subroutine Even/Odd by perlguru22

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (5)
    As of 2018-06-23 20:24 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (125 votes). Check out past polls.