G'day Ostra,

"my program inputs values from a text file and stores them in an array called @nums. Is there an alternative code that can accomplish the same task?"

Tie::File does exactly that, e.g.

tie my @nums, 'Tie::File', $filename;

List::Util has functions to find the minimum and maximum values:

min(@nums); max(@nums);

Given this file:

$ cat pm_num_list.txt 123 12e3 12e-3 -12e3 -123

This code:

#!/usr/bin/env perl use strict; use warnings; use autodie; use List::Util qw{min max}; use Tie::File; tie my @nums, 'Tie::File', 'pm_num_list.txt'; print 'Min: ', min(@nums), "\n"; print 'Max: ', max(@nums), "\n"; untie @nums;

Produces this output:

Min: -12e3 Max: 12e3

You've been shown a few alternative methods for achieving this task. If efficiency is of concern to you, use Benchmark to compare them.

Here's links to the three pragmata I used: strict, warnings and autodie. strict and warnings are recommended for all your programs; autodie for code involving I/O (and a few other cases: check the doco).

-- Ken


In reply to Re: storage of numbers by kcott
in thread storage of numbers by ostra

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.