CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Help writing a script using array and STDIN


New User

Jan 14, 2008, 2:18 PM

Post #1 of 2 (462 views)
Help writing a script using array and STDIN Can't Post

Hello, this is my first time posting here. I am a student and have a PERL college class. I am familiar with a few other programming languages but not PERL (yet!). I have a question in regards to using <STDINPUT> to write number elements to an array, until a blank line is entered. I then want to find out how many numbers are less than the last number entered (the last index of the array). My code I have so far is:

@numbers = <STDIN>; 

$lastNum = $numbers[-1];
$lessThan = 0;
$i = 0;

while ($i < $#numbers)
if ($lastNum > $numbers[$i])
$lessThan += 1;
$i ++;

print "$lessThan numbers were less than $lastNum.";

I'm not sure what the problem is... The program is supposed to read numbers as many times as the user puts a new number until the user inputs a blank line, then perform the operation on the array. Any ideas? Need clarification? Sorry I am a newbie at this. Thanks in advance!

New User

Jan 16, 2008, 10:04 PM

Post #2 of 2 (453 views)
Re: [wild.katana] Help writing a script using array and STDIN [In reply to] Can't Post

<STDIN> takes command line input at a time...I believe you have to read the numbers by putting <STDIN> in a while loop and an exit statement if the condition matches (in this case you must exit if a blank line is encountered).

I think you need to modify your code alot to suit your requirements.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives