in reply to Re: cheesy comparison program not working!!
in thread cheesy comparison program not working!!

Thank You so much for your quick responses!! After amending the program, it now seems to work..
#!/usr/bin/perl use strict ; use warnings ; sub addition { my $sum ; my(@mynumbers) = @_ ; foreach (@mynumbers){ $sum += $_ ; } return $sum } sub avg { my $aver ; my(@mynumbers) = @_ ; $aver = addition(@mynumbers)/($#mynumbers+1) ; print "$aver\n" ; return $aver ; } ## ##THIS NOW WORKS!!! ## my (@numlist, $total, $average, $n, @greater) ; chomp(@numlist = <STDIN>) ; $total = addition(@numlist) ; $average = avg(@numlist) ; $n = 0 ; foreach (@numlist) { if ($_ > $average) { $greater[$n] = $_ ; # push @greater,$_ ; $n++ ; } } if ( $n > 0 ) { print "Following numbers are greater than the average: " ; print "@greater. Array size is $#greater\n" #IS THERE A WAY I CA +N USE "$#greater+1" IN SOME WAY HERE? } else { print "All your numbers are less than or equal to the average\n" ; }
Now how do i use this above...
print "$#greater+1" ?
obviously this does not work as-...is
All said and done.. is this the best way to write the program in terms of coding standards?

Replies are listed 'Best First'.
Re^3: cheesy comparison program not working!!
by cdarke (Prior) on Sep 28, 2007 at 11:34 UTC
    Now how do i use this above... print "$#greater+1" ?

    does not work because you have quotes around it. Operators like + do not get executed inside quotes. Anyhow, it is not the greatest way to find the number of elements in an array, use the array in scalar context instead. There are many ways to do this, one of the less obscure is: print scalar(@greater),"\n";
Re^3: cheesy comparison program not working!!
by toolic (Bishop) on Sep 28, 2007 at 13:35 UTC
    If you are seriously interested in coding practices, here are some items I have found to be useful: