Hello lakshu and welcome to the monastery and to the wonderfull world of Perl!

1nickt gave you a good answer using a core module. It is always good to know them and using them at your and their best.

It is also good to know the function you use: push is used in the wrong way in you sub. Infact push Returns the number of elements in the array following the completed push so at the first iteration you return 1 from the sub and all end.

You are using my @test_array = (3,6,,7,6,3,10,5,6,2, 10); (declaring my (@test_array); in a separate line is inutil..) and you pass it to your sub.

Let's follow what happens:

The array enter the sub as @_ and you assign the whole to it: my @numbers = @_; it's enough. then you initializze some empty vars: my ($min, $max, @ret1); here parens are needed. after you set $min and $max with the value of 3, the first numer entering the sub.

At the first iteration of the foreach loop $i is 3 so if ($i >= $max) is true because $max is still 3 so the block is executed: return push my(@ret1), $i; immediately ends the sub returning the final number of elements present in @ret1 after pushing 3 to it. ie 1 element. No other numbers are processed.

Translated in english it goes like: given a num list, when a number is greater or equal to the first, put it on a list and return how many items the ending list contains. Obviously this is true every time for the first number you pass.

Remeber that print is your friend and is the first powerfull debugging tool you have

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: how to choose the greater or equal number in array by Discipulus
in thread how to choose the greater or equal number in array by lakshu

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.