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

In below program I am expecting the value like 3, 6,6,10, 10 can anyone help

use strict; use warnings; sub min_and_max { my (@numbers); @numbers = @_; my ($min, $max, @ret1); $min = $numbers[0]; $max = $numbers[0]; foreach my $i (@numbers) { if ($i >= $max) { return push my(@ret1), $i; } } return (@ret1); } my (@test_array); @test_array = (3,6,,7,6,3,10,5,6,2, 10); my @ret = min_and_max(@test_array);

In this program I am expecting the value like 3, 6,6,10, 10 can anyone help

Replies are listed 'Best First'.
Re: how to choose the greater or equal number in array
by Discipulus (Canon) on Oct 12, 2017 at 19:20 UTC
    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.
Re: how to choose the greater or equal number in array
by 1nickt (Canon) on Oct 12, 2017 at 18:16 UTC

    Use List::Util's min and max:

    perl -Mstrict -MList::Util=min,max -E ' my @test_array = (3,6,,7,6,3,10,5,6,2, 10); say "Min: " . min @test_array; say "Max: " . max @test_array; '
    Output:
    Min: 2 Max: 10

    Hope this helps!

    Please edit the title of your post as you seem to mean "array" not "area", thanks.


    The way forward always starts with a minimal test.
Re: how to choose the greater or equal number in array
by Your Mother (Archbishop) on Oct 13, 2017 at 16:56 UTC

    Another one for fun. :P

    my @test_array = ( 3,6,,7,6,3,10,5,6,2,10 ); my @sorted = sort { $a <=> $b } grep /\d/, @test_array; print "Min: $sorted[0]", $/; print "Max: $sorted[-1]", $/;
Re: how to choose the greater or equal number in array
by thanos1983 (Parson) on Oct 12, 2017 at 20:49 UTC

    Hello lakshu,

    Welcome to the Monastery. Although the fellow Monks have answered your question I would like to add something small here.

    Just for reference your question was asked before Finding the max()/min(). If you are interested in efficiency between List::Util core module and also the List::MoreUtils which is not a core module. Sample bellow:

    #!/usr/bin/perl use strict; use warnings; use List::Util qw( min max ); use List::MoreUtils qw( minmax ); # use Benchmark qw(:all) ; # WindowsOS use Benchmark::Forking qw( timethese cmpthese ); # UnixOS sub simpleUtil { my (@numbers) = @_; my $min = min @numbers; my $max = max @numbers; return $min, $max; } sub moreUtil { my (@numbers) = @_; my ($min, $max) = minmax @numbers; return $min, $max; } my @test_array = (3,6,7,6,3,10,5,6,2,10); my $results = timethese(1000000000, { SimpleUtil => simpleUtil(@test_a +rray), MoreUtil => moreUtil(@test_array), }, 'none'); cmpthese( $results ); __END__ $ perl test.pl Rate 10 2 SimpleUtil 10 278551532/s -- -31% -57% 2 406504065/s 46% -- -37% SimpleUtil 645161290/s 132% 59% --

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: how to choose the greater or equal number in array
by Anonymous Monk on Oct 13, 2017 at 14:07 UTC
    The correct answer to your obviously homework question is that you must run through the entire loop, not leave early.