;) Hi guys....

I am a beginner at Perl, and i have some problemes I wanna solve!! I hope u can help me.

the exercise is:


Make a program that counts the number of positive and negative numbers in a column. Also count the zeroes in the column, if there are any. Display the result.
and make a program that finds the maximum and minimum number in a column. Display the result.

I tried this but it didnt work! :sad:

$i=0;
while( defined($line = <STDIN>) ) {
$i++;
}
print "Number of lines:$i\n";

and

$i=0;
while( defined($line = <STDIN>) > 0) {
$i++;
print "Number of positive numbers:$i\n";
}

elsif (defined($line = <STDIN>) < 0) {
$i++;
print "Number of negative numbers:$i\n";

else (defined($line = <STDIN>) = 0) {
$i++;
print "Number of 0's: $i\n";


peace
Soad

Hi. When you say a column of numbers I'm thinking you mean one number per line. If I was doing this problem I would try this:

1. Split the column into it's component rows using the split function.
2. Do a foreach loop to perform each necessary operation per column.

However, it would be neater to feed your program the column of numbers in a file and read them into a string first, rather than read them from the keyboard (more scaleable). So, how about ask the user for a file to read the column from and then read them in i.e.

use strict;
use warnings;

&main;
exit(0);

sub main {
my($filename, $column, @data, @line);
print "Please enter the file containing your column: ";
$filename = <STDIN>;
chop($filename);
open(file1, "<", $filename);
@data = <file1>;
close(file1);
$column = join("", @data);
@line = split(/\\n/, $column);
foreach (@line) {
#Your code to process each line in the column
}

}

A few things to mention here:

1. It's a good idea to start your Perl programs with use strict and use warnings. See here: http://perldoc.perl.org/strict.html
and here: http://perldoc.perl.org/warnings.html

2. For information on opening and closing a file see here:
http://perldoc.perl.org/perlopentut.html

3. The split function can come in very useful when processing text files. Also, regular expressions are a good thing to learn if you're interested in Perl. See here: http://perldoc.perl.org/functions/split.html
and here: http://perldoc.perl.org/perlretut.html

4. If you hadn't guessed, this site is a good place to pick up information about Perl (Much reference material, some tutorials):
http://perldoc.perl.org

I hope this helps. Oh, welcome to the hood :) .

Steven.

Hi :cheesy:

Thanx alot steven for your quick respons its really a big help.. this should solved my problem hopefully...

Have a nice time...
Soad

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.