how do i find the max value out of an array?

Discussion in 'Perl Misc' started by IJALAB, May 7, 2010.

  1. IJALAB

    IJALAB Guest

    Hi,

    i have a text file which is comma seperated and i have extracted few
    values from the text in an array for example,
    30, 1, 4,5, 31, 4, 2, 3, 32, 2,3,0, 38

    my goal is to find the max of 30, 31, 32, 38 (i, i+4, i + 8, i+
    12....so on)
    i have put a split statement and in a loop and captured these 4
    elements in an array. how do i find the max of these values, which is
    in an array using perl?

    thanks
    IJALAB, May 7, 2010
    #1
    1. Advertising

  2. IJALAB <> wrote:
    >i have a text file which is comma seperated and i have extracted few
    >values from the text in an array for example,
    >30, 1, 4,5, 31, 4, 2, 3, 32, 2,3,0, 38
    >
    >my goal is to find the max of 30, 31, 32, 38 (i, i+4, i + 8, i+
    >12....so on)
    >i have put a split statement and in a loop and captured these 4
    >elements in an array. how do i find the max of these values, which is
    >in an array using perl?


    Is this some kind of homework? This kind of algorithm is usually an
    introductory example when discussing complex data structures and their
    algorithms.

    Standard way it to loop through the array and remember the (so far)
    largest element..

    Or you simply use List::Util.

    jue
    Jürgen Exner, May 7, 2010
    #2
    1. Advertising

  3. IJALAB

    Guest

    On Fri, 7 May 2010 07:24:13 -0700 (PDT), IJALAB <> wrote:

    >Hi,
    >
    >i have a text file which is comma seperated and i have extracted few
    >values from the text in an array for example,
    >30, 1, 4,5, 31, 4, 2, 3, 32, 2,3,0, 38
    >
    >my goal is to find the max of 30, 31, 32, 38 (i, i+4, i + 8, i+
    >12....so on)
    >i have put a split statement and in a loop and captured these 4
    >elements in an array. how do i find the max of these values, which is
    >in an array using perl?
    >
    >thanks


    This would be my preferred method.
    $string = join '', <DATA>

    -sln
    ---------------

    use strict;
    use warnings;

    my $string = q(30, 1, 4 ,5, -31, 4, 2, 3, 32, 2,3, 0, 38 , 39, 40 );
    my ($max,$min) = (0,0);

    for (split / (?: \s*,\s*[^,]*){3} \s* , \s* | \s*, .* $/xs , $string) {
    $max = $_ unless $max > $_ ;
    $min = $_ unless $min < $_ ;
    }
    print "min/max = $min, $max\n";

    __END__

    min/max = -31, 38
    , May 7, 2010
    #3
  4. IJALAB

    Guest

    Once you have array, how about?
    my $max = (sort { $b <=> $a } @array)[0];





    On May 7, 11:56 pm, wrote:
    > On Fri, 7 May 2010 07:24:13 -0700 (PDT), IJALAB <> wrote:
    > >Hi,

    >
    > >i have a text file which is comma seperated and i have extracted few
    > >values from the text in an array for example,
    > >30, 1, 4,5, 31, 4, 2, 3, 32, 2,3,0, 38

    >
    > >my goal is to find the max of 30, 31, 32, 38 (i, i+4, i + 8, i+
    > >12....so on)
    > >i have put a split statement and in a loop and captured these 4
    > >elements in an array. how do i find the max of these values, which is
    > >in an array using perl?

    >
    > >thanks

    >
    > This would be my preferred method.
    > $string = join '', <DATA>
    >
    > -sln
    > ---------------
    >
    > use strict;
    > use warnings;
    >
    > my $string = q(30, 1, 4 ,5, -31, 4, 2, 3, 32, 2,3, 0, 38 , 39, 40 );
    > my ($max,$min) = (0,0);
    >
    > for (split / (?: \s*,\s*[^,]*){3} \s* , \s* | \s*, .* $/xs , $string) {
    >     $max = $_ unless $max > $_ ;
    >     $min = $_ unless $min < $_ ;}
    >
    > print "min/max = $min, $max\n";
    >
    > __END__
    >
    > min/max = -31, 38
    , May 7, 2010
    #4
  5. IJALAB

    Guest

    On Fri, 7 May 2010 13:33:10 -0700 (PDT), "" <> wrote:

    >On May 7, 11:56 pm, wrote:
    >> On Fri, 7 May 2010 07:24:13 -0700 (PDT), IJALAB <> wrote:
    >> >Hi,

    >>
    >> >i have a text file which is comma seperated and i have extracted few
    >> >values from the text in an array for example,
    >> >30, 1, 4,5, 31, 4, 2, 3, 32, 2,3,0, 38

    >>
    >> >my goal is to find the max of 30, 31, 32, 38 (i, i+4, i + 8, i+
    >> >12....so on)
    >> >i have put a split statement and in a loop and captured these 4
    >> >elements in an array. how do i find the max of these values, which is
    >> >in an array using perl?

    >>
    >> >thanks

    >>
    >> This would be my preferred method.
    >> $string = join '', <DATA>
    >>
    >> -sln
    >> ---------------
    >>
    >> use strict;
    >> use warnings;
    >>
    >> my $string = q(30, 1, 4 ,5, -31, 4, 2, 3, 32, 2,3, 0, 38 , 39, 40 );
    >> my ($max,$min) = (0,0);
    >>
    >> for (split / (?: \s*,\s*[^,]*){3} \s* , \s* | \s*, .* $/xs , $string) {
    >>     $max = $_ unless $max > $_ ;
    >>     $min = $_ unless $min < $_ ;}
    >>
    >> print "min/max = $min, $max\n";
    >>
    >> __END__
    >>
    >> min/max = -31, 38


    >
    >
    >
    >
    >>Once you have array, how about?

    >my $max = (sort { $b <=> $a } @array)[0];


    Why get an array? But sure why not, I never
    met a sort that uses boolean that I didn't like.

    -sln
    , May 7, 2010
    #5
  6. IJALAB

    John Bokma Guest

    Tad McClellan <> writes:

    > IJALAB <> wrote:
    >
    >> captured these 4
    >> elements in an array. how do i find the max of these values, which is
    >> in an array using perl?

    >
    > my($max) = sort {$b <=> $a} @array;


    use List::Util 'max';

    my $max = max @array;

    A linear search /might/ be faster than sorting if @array is large.

    See perldoc List::Util

    (in my experience an under used module)

    --
    John Bokma j3b

    Hacking & Hiking in Mexico - http://johnbokma.com/
    http://castleamber.com/ - Perl & Python Development
    John Bokma, May 7, 2010
    #6
  7. "" <> wrote:
    >Once you have array, how about?
    >my $max = (sort { $b <=> $a } @array)[0];


    If you insist on being inefficient, then that is certainly a good
    solution.
    Sort is O(n*log n), while computing the max value can easily be done in
    O(n).

    jue
    Jürgen Exner, May 8, 2010
    #7
  8. Tad McClellan <> wrote:
    >John Bokma <> wrote:
    >> Tad McClellan <> writes:
    >>
    >>> IJALAB <> wrote:
    >>>
    >>>> captured these 4
    >>>> elements in an array. how do i find the max of these values, which is
    >>>> in an array using perl?
    >>>
    >>> my($max) = sort {$b <=> $a} @array;

    >
    >> A linear search /might/ be faster than sorting if @array is large.

    >
    >
    >He said n=4, which is why I quoted where he said that n=4


    Good catch, I missed it.

    jue
    Jürgen Exner, May 8, 2010
    #8
  9. Jürgen Exner wrote:
    > "" <> wrote:
    >> Once you have array, how about?
    >> my $max = (sort { $b <=> $a } @array)[0];

    >
    > If you insist on being inefficient,


    We are discussing Perl, aren't we?

    > then that is certainly a good
    > solution.
    > Sort is O(n*log n), while computing the max value can easily be done in
    > O(n).


    Xho
    Xho Jingleheimerschmidt, May 8, 2010
    #9
  10. IJALAB

    John Bokma Guest

    Tad McClellan <> writes:

    > John Bokma <> wrote:
    >> Tad McClellan <> writes:
    >>
    >>> IJALAB <> wrote:
    >>>
    >>>> captured these 4
    >>>> elements in an array. how do i find the max of these values, which is
    >>>> in an array using perl?
    >>>
    >>> my($max) = sort {$b <=> $a} @array;

    >
    >> A linear search /might/ be faster than sorting if @array is large.

    >
    > He said n=4, which is why I quoted where he said that n=4


    Yes, yes, I didn't miss this, hence the "/might/" and "large" ;-)

    (Also, it was a bit of a plug for List::Util)

    --
    John Bokma j3b

    Hacking & Hiking in Mexico - http://johnbokma.com/
    http://castleamber.com/ - Perl & Python Development
    John Bokma, May 8, 2010
    #10
  11. IJALAB

    John Bokma Guest

    Xho Jingleheimerschmidt <> writes:

    > Jürgen Exner wrote:
    >> "" <> wrote:
    >>> Once you have array, how about?
    >>> my $max = (sort { $b <=> $a } @array)[0];

    >>
    >> If you insist on being inefficient,

    >
    > We are discussing Perl, aren't we?


    Yup, and my experience is that good Perl runs fast enough. I suspect
    Python to be a bit slower, but note that that's just a suspicion. On top
    of that I don't care.

    --
    John Bokma j3b

    Hacking & Hiking in Mexico - http://johnbokma.com/
    http://castleamber.com/ - Perl & Python Development
    John Bokma, May 8, 2010
    #11
  12. Xho Jingleheimerschmidt <> wrote:
    >Jürgen Exner wrote:
    >> "" <> wrote:
    >>> Once you have array, how about?
    >>> my $max = (sort { $b <=> $a } @array)[0];

    >>
    >> If you insist on being inefficient,

    >
    >We are discussing Perl, aren't we?


    Well, yeah, so? Are you implying that people should use inefficent
    algorithms when programming in Perl?

    Of course the cost for designing, writing, and maintaining the code is
    an important factor, too, and it often justifies the use of a less
    efficient algorithm.
    However in this case where there are trivial, short and well-known
    algorithms for computing the largest element there is really no excuse
    for using a poor algorithm(*).

    *: Well, except that Tad noticed the "only 4 element list" and thus
    efficieny is REALLY totally irrelevant.

    jue
    Jürgen Exner, May 9, 2010
    #12
  13. IJALAB

    Uri Guttman Guest

    >>>>> "JE" == Jürgen Exner <> writes:

    JE> Xho Jingleheimerschmidt <> wrote:
    >> Jürgen Exner wrote:
    >>> "" <> wrote:
    >>>> Once you have array, how about?
    >>>> my $max = (sort { $b <=> $a } @array)[0];
    >>>
    >>> If you insist on being inefficient,

    >>
    >> We are discussing Perl, aren't we?


    JE> Well, yeah, so? Are you implying that people should use inefficent
    JE> algorithms when programming in Perl?

    JE> Of course the cost for designing, writing, and maintaining the
    JE> code is an important factor, too, and it often justifies the use
    JE> of a less efficient algorithm. However in this case where there
    JE> are trivial, short and well-known algorithms for computing the
    JE> largest element there is really no excuse for using a poor
    JE> algorithm(*).

    JE> *: Well, except that Tad noticed the "only 4 element list" and thus
    JE> efficieny is REALLY totally irrelevant.

    so you think applying Sort::Maker on this list is not worth the effort?
    :)

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, May 9, 2010
    #13
  14. John Bokma wrote:
    > Xho Jingleheimerschmidt <> writes:
    >
    >> Jürgen Exner wrote:
    >>> "" <> wrote:
    >>>> Once you have array, how about?
    >>>> my $max = (sort { $b <=> $a } @array)[0];
    >>> If you insist on being inefficient,

    >> We are discussing Perl, aren't we?

    >
    > Yup, and my experience is that good Perl runs fast enough.


    Well, that really depends on fast enough for what. On occasion I've had
    to rewrite certain parts in C to get the required speed, but that is
    rare. And more to my point, my experience is that using sort is also
    fast enough, at least in the context where Perl itself is fast enough.
    I now use List::Util::max or min, but for stylistic reasons, not
    efficiency reasons.

    > I suspect
    > Python to be a bit slower, but note that that's just a suspicion. On top
    > of that I don't care.


    Right.

    Xho
    Xho Jingleheimerschmidt, May 9, 2010
    #14
  15. Jürgen Exner wrote:
    > Xho Jingleheimerschmidt <> wrote:
    >> Jürgen Exner wrote:
    >>> "" <> wrote:
    >>>> Once you have array, how about?
    >>>> my $max = (sort { $b <=> $a } @array)[0];
    >>> If you insist on being inefficient,

    >> We are discussing Perl, aren't we?

    >
    > Well, yeah, so? Are you implying that people should use inefficent
    > algorithms when programming in Perl?


    Depends on in what domain it is inefficient.

    > Of course the cost for designing, writing, and maintaining the code is
    > an important factor, too, and it often justifies the use of a less
    > efficient algorithm.


    Right, especially the difference in efficiency is factor of lnN, rather
    than N or some higher order.


    > However in this case where there are trivial, short and well-known
    > algorithms for computing the largest element there is really no excuse
    > for using a poor algorithm(*).


    There is no such thing as a trivial algorithm. I've seen people screw
    up even this one, no matter how short and well-known it may be.


    Xho
    Xho Jingleheimerschmidt, May 9, 2010
    #15
  16. IJALAB

    John Bokma Guest

    Xho Jingleheimerschmidt <> writes:

    > John Bokma wrote:
    >> Xho Jingleheimerschmidt <> writes:
    >>
    >>> Jürgen Exner wrote:
    >>>> "" <> wrote:
    >>>>> Once you have array, how about?
    >>>>> my $max = (sort { $b <=> $a } @array)[0];
    >>>> If you insist on being inefficient,
    >>> We are discussing Perl, aren't we?

    >>
    >> Yup, and my experience is that good Perl runs fast enough.

    >
    > Well, that really depends on fast enough for what.


    For the stuff I do with it, hence "my experience is". ;-). (Mostly
    processing a lot of data)

    > On occasion I've
    > had to rewrite certain parts in C to get the required speed, but that
    > is rare. And more to my point, my experience is that using sort is
    > also fast enough, at least in the context where Perl itself is fast
    > enough. I now use List::Util::max or min, but for stylistic reasons,
    > not efficiency reasons.


    Yup, that was more or less my point.

    >> I suspect
    >> Python to be a bit slower, but note that that's just a suspicion. On top
    >> of that I don't care.

    >
    > Right.


    No, really. For (most if not all of) my stuff the speed of Perl/Python
    is more than enough. Perl is often able to surprise me in a very
    pleasant wa,y speedwise :).

    --
    John Bokma j3b

    Hacking & Hiking in Mexico - http://johnbokma.com/
    http://castleamber.com/ - Perl & Python Development
    John Bokma, May 14, 2010
    #16
  17. IJALAB

    Guest

    On Thu, 13 May 2010 22:01:16 -0500, John Bokma <> wrote:

    >Xho Jingleheimerschmidt <> writes:
    >
    >> John Bokma wrote:
    >>> Xho Jingleheimerschmidt <> writes:
    >>>
    >>>> Jürgen Exner wrote:
    >>>>> "" <> wrote:
    >>>>>> Once you have array, how about?
    >>>>>> my $max = (sort { $b <=> $a } @array)[0];
    >>>>> If you insist on being inefficient,
    >>>> We are discussing Perl, aren't we?
    >>>
    >>> Yup, and my experience is that good Perl runs fast enough.

    >>
    >> Well, that really depends on fast enough for what.

    >
    >For the stuff I do with it, hence "my experience is". ;-). (Mostly
    >processing a lot of data)


    Regardless of what the OP literally said,
    you would recommend sort to find the min or max value?

    -sln
    , May 17, 2010
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cayblood
    Replies:
    5
    Views:
    6,212
    Calum Grant
    Nov 3, 2005
  2. Summercool
    Replies:
    9
    Views:
    878
    dorayme
    Oct 23, 2007
  3. John
    Replies:
    0
    Views:
    282
  4. Greg Ferris

    Textarea max rows and max characters per row

    Greg Ferris, Jan 16, 2004, in forum: Javascript
    Replies:
    2
    Views:
    576
    Greg Ferris
    Jan 16, 2004
  5. kriton
    Replies:
    0
    Views:
    334
    kriton
    Apr 22, 2005
Loading...

Share This Page