Solved

PERL HELP

Posted on 2006-10-25
11
391 Views
Last Modified: 2008-02-01
hello experts. I am trying to create a function that takes in an array of numbers and calculates the average, the total and a new array of numbers which is other numbers divided by two and return the list of info. here is what I have so far and im kinda stuck. help would be greatly apprectiated.

#main script to be called

#!/usr/bin/perl
@numbers;
require 'function.pl'
while(<STDIN>){
push(@numbers,$_);
}
$list = result(@numbers)
print "$list\n";

#this is the function script
sub result()
{
@half;
@array = shift(@_);
$i=0;
foreach (@array){
$sum += @arrray[$i];
push(@half,@array[$i]/2);
$i++;
}
$average = $sum/2;
$list = join(":",@half,$sum,$average);
return $list;
}


0
Comment
Question by:micromarch
  • 5
  • 4
  • 2
11 Comments
 
LVL 4

Assisted Solution

by:hclgroup
hclgroup earned 200 total points
ID: 17805379
#!/usr/bin/perl
@numbers;

while(<STDIN>){
last if (/^end$/);
push(@numbers,$_);
}
$list = result(@numbers);
print "$list\n";

#this is the function script
sub result()
{
@half;
@array = @_;
$i=0;
$sum=0;
foreach (@array){
$sum += $_;
print "$_";
push(@half,$_/2);
$i++;
}
$average = $sum/$i;
$list = join(":",@half,$sum,$average);
return $list;
}
0
 
LVL 4

Expert Comment

by:hclgroup
ID: 17805391
You can remove line 20 : print "$_";
That was just for debuggin purposes...
0
 
LVL 4

Expert Comment

by:hclgroup
ID: 17805419
Also, I changed it to read input until the 'end' word is entered (in a new line of course)
0
 
LVL 2

Author Comment

by:micromarch
ID: 17805631
ok thanks but why does it always give me an error that my script did not return a true value at line 2? my functions is located into another script. it seems to be bugging where I state. require 'name of script';
thanks.
0
 
LVL 84

Expert Comment

by:ozo
ID: 17805702
perl -Mdiagnostics -e "require 'name of script'"
name of script did not return a true value at -e line 1 (#1)
    (F) A required (or used) file must return a true value to indicate that
    it compiled correctly and ran its initialization code correctly.  It's
    traditional to end such a file with a "1;", though any true value would
    do.  See perlfunc/require.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:micromarch
ID: 17805735
and that is suppose to mean what?
0
 
LVL 84

Accepted Solution

by:
ozo earned 300 total points
ID: 17805786
#!/usr/bin/perl
use strict;
use warnings;
my @numbers;

while(<>){
 last if (/^end$/);
 push(@numbers,$_);
}
my $list = result(@numbers);
print "$list\n";

#this is the function script
sub result{
  my @half = map{$_/2}@_;
  my @array = @_;
  my $sum=0;
  foreach( @array ){
    $sum += $_;
  }
  my $average = $sum/@array;
  return join(":",@half,$sum,$average);
}
1;
0
 
LVL 2

Author Comment

by:micromarch
ID: 17805932
how can I make it accept more then one number?
0
 
LVL 2

Author Comment

by:micromarch
ID: 17805955
nevermind gotcha
thanks allot for both your help. I really appreciate this.
0
 
LVL 2

Author Comment

by:micromarch
ID: 17805992
i think you both deserve points so how do i split them?
0
 
LVL 4

Expert Comment

by:hclgroup
ID: 17806095
You click on the link that says Split.
Then a box show up next to each username and comment so you can assign points to each person.
But you can only accept one answer.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
count7 challenge 12 101
How does Google get such fast response times? 5 151
Non-recursive backtracking, using a stack 1 101
Is doing tutor.com teaching in my situation advisable? 2 51
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

911 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

20 Experts available now in Live!

Get 1:1 Help Now