<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to find the minimum value in an array in PERL in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837805#M100708</link>
    <description>The solution JRF is more resource effective than sorting, and also delivers the position of the min value in the original array.&lt;BR /&gt;If the is usefull for you, or if you also need to pick up the max then walk teh list as indicated.&lt;BR /&gt;&lt;BR /&gt;If it is a modest size list (thousands, not millions) then as Victor indicates, just sort the array.&lt;BR /&gt;&lt;BR /&gt;Example:&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;my @times = ( "91:02:03", "02:11:17", "00:08:11", "03:33:33" );&lt;BR /&gt;my @sorted_times = sort @times;&lt;BR /&gt;print join(' - ', @sorted_times), "\n";&lt;BR /&gt;print "min = $sorted_times[0]\n";&lt;BR /&gt;print "max = $sorted_times[@sorted_times - 1]\n";&lt;BR /&gt;&lt;BR /&gt;Cheers,&lt;BR /&gt;Hein.</description>
    <pubDate>Sun, 06 Aug 2006 09:30:11 GMT</pubDate>
    <dc:creator>Hein van den Heuvel</dc:creator>
    <dc:date>2006-08-06T09:30:11Z</dc:date>
    <item>
      <title>How to find the minimum value in an array in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837802#M100705</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;My values in an array are in the form hours:mins:secs.&lt;BR /&gt;I want to find the minimum of these values.&lt;BR /&gt;&lt;BR /&gt;Please answer me .</description>
      <pubDate>Sat, 05 Aug 2006 00:51:19 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837802#M100705</guid>
      <dc:creator>Pankaj Yadav_1</dc:creator>
      <dc:date>2006-08-05T00:51:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value in an array in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837803#M100706</link>
      <description>&lt;!--!*#--&gt;Hi:&lt;BR /&gt;&lt;BR /&gt;This builds on what I showed you yesterday.&lt;BR /&gt;&lt;BR /&gt;# cat ./findmin&lt;BR /&gt;#!/usr/bin/perl &lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;my @times = ( "91:02:03", "02:11:17", "00:08:11", "03:33:33" );&lt;BR /&gt;my $idx     = -1;&lt;BR /&gt;my $minidx  =  0;&lt;BR /&gt;my $mintime =  0;&lt;BR /&gt;for my $t (@times) {&lt;BR /&gt;    $idx++;&lt;BR /&gt;    my ($hr, $min, $sec) = split /:/, $t;&lt;BR /&gt;    my $elapsed = ($hr * 3600) + ($min * 60) + $sec;&lt;BR /&gt;    $mintime = $elapsed if $mintime == 0;&lt;BR /&gt;    if ($elapsed &amp;lt; $mintime) {&lt;BR /&gt;        $mintime = $elapsed;&lt;BR /&gt;        $minidx  = $idx;&lt;BR /&gt;    }&lt;BR /&gt;}&lt;BR /&gt;print "mininum is $times[$minidx] at element $minidx\n";&lt;BR /&gt;1;&lt;BR /&gt;&lt;BR /&gt;...I have used a list (array) called "@times" with four 0-relative elements in the format "hh:mm:ss".&lt;BR /&gt;&lt;BR /&gt;We iterate over the list, converting each element into a number of seconds.  We track the index number of the array along with the smallest value (in seconds).&lt;BR /&gt;&lt;BR /&gt;At the end, we simply print the "hh:mm:ss" and the element number that represents the minimum.&lt;BR /&gt;&lt;BR /&gt;# ./findmin&lt;BR /&gt;mininum is 00:08:11 at element 2&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Sat, 05 Aug 2006 07:43:17 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837803#M100706</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-08-05T07:43:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value in an array in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837804#M100707</link>
      <description>Try to sort the array and take the first element&lt;BR /&gt;&lt;BR /&gt;HTH</description>
      <pubDate>Sat, 05 Aug 2006 10:07:23 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837804#M100707</guid>
      <dc:creator>Victor Fridyev</dc:creator>
      <dc:date>2006-08-05T10:07:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value in an array in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837805#M100708</link>
      <description>The solution JRF is more resource effective than sorting, and also delivers the position of the min value in the original array.&lt;BR /&gt;If the is usefull for you, or if you also need to pick up the max then walk teh list as indicated.&lt;BR /&gt;&lt;BR /&gt;If it is a modest size list (thousands, not millions) then as Victor indicates, just sort the array.&lt;BR /&gt;&lt;BR /&gt;Example:&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;my @times = ( "91:02:03", "02:11:17", "00:08:11", "03:33:33" );&lt;BR /&gt;my @sorted_times = sort @times;&lt;BR /&gt;print join(' - ', @sorted_times), "\n";&lt;BR /&gt;print "min = $sorted_times[0]\n";&lt;BR /&gt;print "max = $sorted_times[@sorted_times - 1]\n";&lt;BR /&gt;&lt;BR /&gt;Cheers,&lt;BR /&gt;Hein.</description>
      <pubDate>Sun, 06 Aug 2006 09:30:11 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837805#M100708</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2006-08-06T09:30:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value in an array in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837806#M100709</link>
      <description>The array has value in the form of hrs:mins:secs&lt;BR /&gt;&lt;BR /&gt;Will the direct sort work or we have to convert the values in seconds and then sort using the sort function.&lt;BR /&gt;</description>
      <pubDate>Tue, 08 Aug 2006 04:43:43 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837806#M100709</guid>
      <dc:creator>Pankaj Yadav_1</dc:creator>
      <dc:date>2006-08-08T04:43:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value in an array in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837807#M100710</link>
      <description>Hi (again):&lt;BR /&gt;&lt;BR /&gt;The answer to your last question of whether or not a direct sort will work, or whether you will have to first convert to seconds, depends upon your data.  Consider:&lt;BR /&gt;&lt;BR /&gt;# perl -le '@a=qw(1:17:19 01:17:17 1:33:48 01:34:48);print for sort (@)'&lt;BR /&gt;&lt;BR /&gt;01:17:17&lt;BR /&gt;01:34:48&lt;BR /&gt;1:17:19&lt;BR /&gt;1:33:48&lt;BR /&gt;&lt;BR /&gt;...is *not* ordered the way you want; but:&lt;BR /&gt;&lt;BR /&gt;# perl -le '@a=qw(1:17:19 1:17:17 1:33:48 1:34:48);print for sort (@a)'&lt;BR /&gt;&lt;BR /&gt;...is ordered correctly.&lt;BR /&gt;&lt;BR /&gt;Thus, it is simpler to convert to epoch seconds; walk through the array; and track the mininimum value and index of the original data.  Of course, a more complicated sort can be written if that was the goal in the first place.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Tue, 08 Aug 2006 06:45:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837807#M100710</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-08-08T06:45:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value in an array in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837808#M100711</link>
      <description>Right, I used a implies string sort which relies on a consisten format.  If the format is not exactly predictable, then you need to convert to formatted string, or to numeric first. &lt;BR /&gt;You can tell sort to use a function to do all that:&lt;BR /&gt;&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;&lt;BR /&gt;sub seconds{&lt;BR /&gt;my ($hr, $min, $sec) = split /:/, $_[0];&lt;BR /&gt;return ($hr * 3600) + ($min * 60) + $sec;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;my @times = qw(1:17:19 01:17:17 1:33:48 01:34:48);&lt;BR /&gt;my @sorted_times = sort @times;&lt;BR /&gt;print join(' - ', @sorted_times), "\n";&lt;BR /&gt;print "min - $sorted_times[0]\n";&lt;BR /&gt;print "max - $sorted_times[@sorted_times - 1]\n\n";&lt;BR /&gt;&lt;BR /&gt;@sorted_times = sort {&amp;amp;seconds($a) &amp;lt;=&amp;gt; &amp;amp;seconds($b)} @times;&lt;BR /&gt;print join(' - ', @sorted_times), "\n";&lt;BR /&gt;print "min = $sorted_times[0]\n";&lt;BR /&gt;print "max = $sorted_times[@sorted_times - 1]\n";&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;fwiw,&lt;BR /&gt;Hein.&lt;BR /&gt;</description>
      <pubDate>Tue, 08 Aug 2006 07:18:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837808#M100711</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2006-08-08T07:18:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value in an array in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837809#M100712</link>
      <description>Thanks for all help on sorting.&lt;BR /&gt;&lt;BR /&gt;Please help me on the following code--&amp;gt;&lt;BR /&gt;&lt;BR /&gt;while(my $data = $sth-&amp;gt;fetchrow_hashref) {&lt;BR /&gt;        my %loopdata;&lt;BR /&gt;        ($Hrs,$Mins,$Secs) = &amp;amp;diffDateTime($data-&amp;gt;{'date'},$data-&amp;gt;{'time_in'},$data-&amp;gt;{'time_out'},$data-&amp;gt;{'date_out'});&lt;BR /&gt;        $loopdata{'type'}=$data-&amp;gt;{'type'};&lt;BR /&gt;        $loopdata{'interval'}= "$Hrs:$Mins:$Secs";&lt;BR /&gt;        push(@intlist, \%loopdata);&lt;BR /&gt;&lt;BR /&gt;    }&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Please tell me whether the code is correct.&lt;BR /&gt;</description>
      <pubDate>Tue, 08 Aug 2006 07:52:29 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837809#M100712</guid>
      <dc:creator>Pankaj Yadav_1</dc:creator>
      <dc:date>2006-08-08T07:52:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value in an array in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837810#M100713</link>
      <description>Only you can tell.&lt;BR /&gt;First does it compile by now without raising sysntax errors and warnings?&lt;BR /&gt;Run perl -cw on your script.&lt;BR /&gt;But this won't tell if the code executes correctly.&lt;BR /&gt;We don't know the database's table you are querying, and therefore cannot tell whether the field names you used as keys to your result set are ok.&lt;BR /&gt;I would suggest you run your code in the Perl debugger and have a look at what $data contains after each fetch.&lt;BR /&gt;Alternatively you could use Data::Dumper or YAML, but using the debugger is easier.&lt;BR /&gt;perl -d /your/script.pl&lt;BR /&gt;will lead you into a debug session.&lt;BR /&gt;Continue to the first statement in the while loop (c line#).&lt;BR /&gt;There have a look at $data in all its beauty&lt;BR /&gt;|x $data&lt;BR /&gt;This will clarify the structure of your hash.&lt;BR /&gt;You can step through your code by just hitting Return, and repeat the data dump.</description>
      <pubDate>Tue, 08 Aug 2006 09:04:27 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837810#M100713</guid>
      <dc:creator>Ralph Grothe</dc:creator>
      <dc:date>2006-08-08T09:04:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value in an array in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837811#M100714</link>
      <description>My code is like this --&amp;gt;&lt;BR /&gt;&lt;BR /&gt;my $con_sql = "SELECT type,date,time_in,time_out,date_out ".&lt;BR /&gt;                  "FROM contract_log,contract_times WHERE ".&lt;BR /&gt;                  "contract_log.refid=contract_times.conid AND date BETWEEN '$fromDate' AND '$toDate' AND date_out&amp;lt;&amp;gt;'0000-00-00'";&lt;BR /&gt;    my $dbh = &amp;amp;TpsUtils::db_connect();&lt;BR /&gt;    my $sth = $dbh-&amp;gt;prepare($con_sql);&lt;BR /&gt;    die "Error with SQL: $con_sql" if !defined $sth-&amp;gt;execute;&lt;BR /&gt;    &lt;BR /&gt;    my @intlist;&lt;BR /&gt;    my ($Hrs,$Mins,$Secs);&lt;BR /&gt;    use Data::Dumper;&lt;BR /&gt;    open(FILE,'&amp;gt;/home/pankaj/report/test.txt');&lt;BR /&gt;&lt;BR /&gt;    #store the time intervals in the array @intlist&lt;BR /&gt;    while(my $data = $sth-&amp;gt;fetchrow_hashref) {&lt;BR /&gt;        my %loopdata;&lt;BR /&gt;        ($Hrs,$Mins,$Secs) = &amp;amp;diffDateTime($data-&amp;gt;{'date'},$data-&amp;gt;{'time_in'},$data-&amp;gt;{'time_out'},$data-&amp;gt;{'date_out'});&lt;BR /&gt;        $loopdata{'type'}=$data-&amp;gt;{'type'};&lt;BR /&gt;        $loopdata{'interval'}= "$Hrs:$Mins:$Secs";&lt;BR /&gt;        push(@intlist, \%loopdata);&lt;BR /&gt;&lt;BR /&gt;    }&lt;BR /&gt;&lt;BR /&gt;    print FILE Dumper(\@intlist);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;When I run the query directly I get data but when I dump that data(@intlist) in a file I can't find any data.&lt;BR /&gt;&lt;BR /&gt;Please tell me whats going wrong.</description>
      <pubDate>Wed, 09 Aug 2006 00:55:27 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837811#M100714</guid>
      <dc:creator>Pankaj Yadav_1</dc:creator>
      <dc:date>2006-08-09T00:55:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the minimum value in an array in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837812#M100715</link>
      <description>ok no need to apply your brain.&lt;BR /&gt;I am getting what I want.&lt;BR /&gt;Thanks a lot .</description>
      <pubDate>Wed, 09 Aug 2006 03:34:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-find-the-minimum-value-in-an-array-in-perl/m-p/3837812#M100715</guid>
      <dc:creator>Pankaj Yadav_1</dc:creator>
      <dc:date>2006-08-09T03:34:07Z</dc:date>
    </item>
  </channel>
</rss>

