in reply to storage of numbers
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
|
|---|