Prev: FAQ 8.16 How can I sleep() or alarm() for under a second?
Next: FAQ 5.22 Why can't I just open(FH, ">file.lock")?
From: sln on 7 May 2010 18:16 On Fri, 7 May 2010 13:33:10 -0700 (PDT), "sopan.shewale(a)gmail.com" <sopan.shewale(a)gmail.com> wrote: >On May 7, 11:56�pm, s...(a)netherlands.com wrote: >> On Fri, 7 May 2010 07:24:13 -0700 (PDT), IJALAB <balaji.d...(a)gmail.com> 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
From: John Bokma on 7 May 2010 18:16 Tad McClellan <tadmc(a)seesig.invalid> writes: > IJALAB <balaji.draj(a)gmail.com> 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
From: Tad McClellan on 7 May 2010 21:09 John Bokma <john(a)castleamber.com> wrote: > Tad McClellan <tadmc(a)seesig.invalid> writes: > >> IJALAB <balaji.draj(a)gmail.com> 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 :-) -- Tad McClellan email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/" The above message is a Usenet post. I don't recall having given anyone permission to use it on a Web site.
From: J�rgen Exner on 7 May 2010 22:43 "sopan.shewale(a)gmail.com" <sopan.shewale(a)gmail.com> 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
From: J�rgen Exner on 7 May 2010 22:46 Tad McClellan <tadmc(a)seesig.invalid> wrote: >John Bokma <john(a)castleamber.com> wrote: >> Tad McClellan <tadmc(a)seesig.invalid> writes: >> >>> IJALAB <balaji.draj(a)gmail.com> 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
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: FAQ 8.16 How can I sleep() or alarm() for under a second? Next: FAQ 5.22 Why can't I just open(FH, ">file.lock")? |