Sign in to follow this  
tonez9

Perl Program/Logic Question

Recommended Posts

tonez9    122
Hello all, my first post here. I was referenced here by a friend :) I'm currently taking an administrative scripting class and am having a very hard time with a lab. I'm not asking for someone to program this out for me so I can hand it in, but some guidance would be VERY appreciated. First off, i'll give you the problem. Write a Perl program that will accept (5) positive integer numbers in from the keyboard by the user. Place the numbers into an array. The output to the screen should display the numbers in highest-to-lowest format, and then on the next line in lowest-to-highest format. The program must include use of only (1) array, and the output of the number (per line) should be “tab delimited”. So far, this is what I have: #!/usr/bin/perl 2 use warnings; 3 use strict; 4 5 6 7 my ($var, $counter, @array); 8 $var = 0; 9 for ($counter =0 ; $counter < 5 ; $counter ++) { 10 11 print "Please enter a number: "; 12 chomp ($var = <STDIN>); 13 14 if ($var lt 0 ) { 15 until ($var gt 0) { 16 print "Please enter positive integer values only. Try again.\n"; 17 print "Please enter a number: "; 18 chomp ($var = <STDIN>); 19 } 20 $array[$counter] = $var; 21 } 22 else { 23 $array[$counter] = $var; 24 } 25 } 26 @array = sort {$a <=> $b} @array; 27 print "There are a total of $counter elements within your array.\n\n"; 28 foreach (0..4){ 29 printf "$array[$_]\t"; 30 } 31 @array = sort {$b <=> $a} @array; 32 print "\n"; 33 foreach (0..4){ 34 printf "$array[$_]\t"; 35 } 36 print "\n"; Using this right now, the program works fine. It breaks however when the input for one of the digits is a "letter". This is one of the professor's test entries. When a letter is entered, it should say it is not a positive integer and continue as if you entered -1 for example. This is my first problem . My second problem is: Write a Perl program that will accept only (2) alphabetic characters from the user (a-z or A-Z). Take the (2) letters, and populate an array will all characters between the (2) inclusively. Print the array to the screen from both highest-to-lowest and lowest-to-highest order (each on a separate line). The program must inform the user that they haven’t type a valid character. The program must also be able to determine which of the (2) letters re of lower value in order to start the array with (ie. The array cannot be -> e, d, c, b, a). The program must also make use of the range operator in order to populate the array initially. The program should also consider the case of the characters entered. If the user enters a lower case character for the first letter, then all of the characters in the array should be in lower case. If the user enters an upper case character for the first letter, then all of the characters in the array should be in upper case. The only thing I need help with in this problem is actually assigning the original array the values from [a-z] using the range operator. -Tony. [Edited by - tonez9 on February 20, 2008 10:01:41 PM]

Share this post


Link to post
Share on other sites
leiavoia    960
since this is homework i can only point you in the right direction:

to test for integers, you can:

- use brute force, i.e. if ( $x != 0 && $x != 1 && $x != 2 . . . )

- use a function from another module. You might google for "is_integer" or "is_numeric" or something to that effect (those are PHP equivilents).

- use a regular expression. that might be beyond your current level of study and it might look like you cheated if you use that. i would use this method if it were me, just for simpicity.

- force the value to be an integer and not issue any warning by casting the value.

- see here


your second question is answered by simply googling "perl range operator". It's the very first link.

Share this post


Link to post
Share on other sites
daviangel    604
Man I had forgotten how ugly Perl was but that chomp() always give me a laugh and I can't help but think of Perl as the pacman language because of it!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this