Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Why is the List upload with STDIN from a text file giving wrong result for mean?

by roboticus (Canon)
on Nov 02, 2012 at 13:12 UTC ( #1001966=note: print w/ replies, xml ) Need Help??


in reply to Why is the List upload with STDIN from a text file giving wrong result for mean?

supriyoch_2008:

In addition to the other fine advice you've received, I'd suggest you change this:

unless ( open(FILE, $filename) ) { print "Cannot open file \"$filename\"\n\n"; exit; }

to this:

open my $FILE, '<', $filename or die "Cannot open file '$filename': $!\n";

It's a much more common usage. I'd also change this:

# Sum of all elements in array: $sum=0; $sum=eval join '+',@array;

to this:

# Sum of all elements in array: $sum=0; $sum+=$_ for @array;

That way, you can avoid a potentially scary string eval. Since you're getting the data for your string from an external source, it's possible that someone might create a string that would let you compromise your system.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


Comment on Re: Why is the List upload with STDIN from a text file giving wrong result for mean?
Select or Download Code
Re^2: Why is the List upload with STDIN from a text file giving wrong result for mean?
by supriyoch_2008 (Scribe) on Nov 02, 2012 at 14:23 UTC

    Roboticus

    Thank you very much for your constructive and useful suggestions. I shall try the codes given by you in my script.

    Regards

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1001966]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (14)
As of 2014-08-28 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (259 votes), past polls