Respected Monks,

Thank you for all your answers.

After seeing all the replies here, I too had the urge to try it out my way, so I threw away all the restrictions that the example had put and tried it my way, so that it will work for any numbers, positive or negative, decimal/hex/octal/binary, and came up with this.

use strict; use warnings; my @numbers = (10, 110, 24, 30, -17,-9,12,14,-011,-0xF,0b111,0); sub biggest_odd { my @odd_only = grep {$_ % 2 != 0} @_; if (!@odd_only) { print "No odd numbers at all\n"; return; } my $biggest_odd_num = shift @odd_only; foreach my $odd_num(@odd_only) { if ($odd_num > $biggest_odd_num) { $biggest_odd_num = $odd_num; } } print "Biggest odd is $biggest_odd_num\n"; } &biggest_odd(@numbers);

And here is the output:

C:perlscripts>perl jg_find_biggest_odd.pl Biggest odd is 7

Like I said earlier, I really love Perl, but I find the John Guttag book so interesting that I've started learning Python just so that I can understand the concepts given in the book. I don't know how much successful I will be learning two languages simultaneously, especially given the fact that I love the Perl way of solving problems...

Thinkpad T430 with Ubuntu 16.04.2 running perl 5.24.1 thanks to plenv!!

In reply to Re: John Guttag's book - 2nd exercise. My attempt in Perl. by pritesh_ugrankar
in thread John Guttag's book - 2nd exercise. My attempt in Perl. by pritesh_ugrankar

Title:
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, details, 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, summary, 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.