Solved

perl calculations

Posted on 2009-07-06
7
174 Views
Last Modified: 2012-05-07
ok,

so I want to calculate the average value per line.

how do I write in code to add up the value of the variables, and divide that value by seven ?
0
Comment
Question by:MichaelGlancy
7 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 500 total points
ID: 24789880
my $avg = ($n1 + $n2 + $n3 + $n4 + $n5 + $n6 + $n7)/ 7;
0
 

Author Closing Comment

by:MichaelGlancy
ID: 31600393
excellent. I didnt know there was an avg operator thing
0
 
LVL 84

Expert Comment

by:ozo
ID: 24789894
while( <> ){
  next unless my @line=split/\D+/;
  my $sum=0;
  $sum+=$_ for @line;
  print "average=",$sum/@line,"\n";
}
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:MichaelGlancy
ID: 24789895
haha just realised there isnt, its another variable

see I know nothing
0
 

Author Comment

by:MichaelGlancy
ID: 24789903
thanks ozo again

but it really does have to be stupid simple for me so I can follow it logically as best I can
0
 
LVL 84

Expert Comment

by:ozo
ID: 24789941
You didn't say that your variables were $n1 + $n2 + $n3 + $n4 + $n5 + $n6 + $n7, so I added code to read variables into @line
0
 
LVL 48

Expert Comment

by:Tintin
ID: 24790124
Here's another way that hopefully is very clear to read.
#!/usr/bin/perl

use List::Util 'sum';
 

open FILE, "file.csv" or die "Can not open file.csv $!\n ;
 

while (<FILE>) {

  my @line=split ',';

  my $avg = sum(@line) / @line;

  print "avg=$avg\n";

}

Open in new window

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now