Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Largest numeric value array

  1. Feb 2, 2005 #1

    Monique

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    How do you return the largest numeric value in an array, in Perl?
     
  2. jcsd
  3. Feb 2, 2005 #2
  4. Feb 2, 2005 #3

    NateTG

    User Avatar
    Science Advisor
    Homework Helper

    I don't think perl has a maximum function so you have to do something like
    Code (Text):

    my $tmp='';
    map {$tmp=$_ if ($_>$tmp and $_=~/^-?\d*.?\d*$/s and $_=~ /\d/}  @array
    die("The array doesn't contain any numbers ") unless ($tmp=~ /\d/);
    return $tmp;
     
     
  5. Feb 2, 2005 #4

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I always try to advise people by asking how they would do it.

    If you had to find the largest number in a list of a million numbers, how would you do it?

    You would probably go through it one by one, remembering the largest number you've seen.


    So, that's how you should do it on the computer: you have a variable for storing the largest number you've seen, and you iterate through the array, updating this variable whenever you see a bigger number.
     
  6. Feb 2, 2005 #5

    Monique

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Well, luckily I only needed to compare three values so I took the easy way out:

    Code (Text):
            if ($frame1>$frame2&&$frame3){
                $ORF=$frame1;
            } elsif ($frame2>$frame3){
                $ORF=$frame2;
            } else {
                $ORF=$frame3;
            }
    I guess the way you suggest would be best for a large array, thanks :)
     
  7. Feb 2, 2005 #6

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I'm not familiar with Perl syntax, but don't you mean something like ($frame1>$frame2)&&($frame1>$frame3)? (In other languages, at least, the two are much different)
     
  8. Feb 2, 2005 #7

    Monique

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Hey, thanks! I tested it with some numbers and you're right :blushing:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Largest numeric value array
  1. Big arrays (Replies: 13)

  2. Java Arrays (Replies: 22)

  3. Numeric keypad (Replies: 0)

Loading...