Welcome to the Monastery PerlMonks

### Re^2: Finding the max()/min()

by sleepingsquirrel (Hermit)
 on Nov 11, 2004 at 20:03 UTC ( #407168=note: print w/replies, xml ) Need Help??

in reply to Re: Finding the max()/min()

```\$x=1;   \$y=0;

\$x=-1;  \$y=2;

\$x=1.2; \$y=2;

-- All code is 100% tested and functional unless otherwise noted.

Replies are listed 'Best First'.
Re^3: Finding the max()/min()
by cLive ;-) (Prior) on Nov 11, 2004 at 23:55 UTC
Ahh, good point ;-)

Serves me right for trying to be creative on a Windows box sans Perl :)

cLive ;-)

This is the variant I found most straightforward to use:
```use List::Util qw[min max];

\$m = max(1,2);
Here you go:

This is how one can find a maximum number in an array iteratively and recursively in Perl

```
#!/usr/bin/perl

my (@setOfnumbers);

@setOfnumbers = ( 9, 7, 90, 3, 8, 412, 67, 2, 45, 53, 1, 3,89 );

print "Finding maximum number iteratively ",
getMaximumNumber(@setOfnumbers) , "\n";
print "Finding maximum number recursively ",
findMaximumNumber(@setOfnumbers) , "\n";

#Finding maximum number iteratively
sub getMaximumNumber{
my (\$maximumNumber);
\$maximumNumber = @_[0];
foreach(@_){
if (\$_ > \$maximumNumber){
\$maximumNumber = \$_;
}
}
return \$maximumNumber;
}

#Finding maximum number recursively
sub findMaximumNumber{

my (\$maximumNumber);

if (@_ == 1){
\$maximumNumber = shift(@_);
return \$maximumNumber;
}
else{
\$maximumNumber = shift(@_);
return (findMaximumNumber(@_) > \$maximumNumber)
? findMaximumNumber(@_) : \$maximumNumber;
}

}

Create A New User
Node Status?
node history
Node Type: note [id://407168]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2018-07-18 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

Results (383 votes). Check out past polls.

Notices?