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: IJALAB on 7 May 2010 10:24 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
From: J�rgen Exner on 7 May 2010 10:48 IJALAB <balaji.draj(a)gmail.com> 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
From: sln on 7 May 2010 14:56 On Fri, 7 May 2010 07:24:13 -0700 (PDT), IJALAB <balaji.draj(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
From: sopan.shewale on 7 May 2010 16:33 Once you have array, how about? my $max = (sort { $b <=> $a } @array)[0]; 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
From: Tad McClellan on 7 May 2010 17:35 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; -- 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.
|
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")? |