Home: Perl Programming Help: Beginner:
display ascii



adiolord
Novice

Feb 9, 2009, 3:01 PM


Views: 2576
display ascii

Basically I have to prompt the user for a case insensitive selection of either (a, A, b, B) string value, if they choose a/A, display all 255 ASCII characters in an array in ascending order, b/B, descending order.

If neither a/A/b/B are chosen report an invalid selection and loop until successful. I am brand new to Perl and would love some help getting this script off the ground.

Thankyou!


KevinR
Veteran


Feb 9, 2009, 7:23 PM


Views: 2570
Re: [adiolord] display ascii


Code
use strict; 
use warnings;

print "Enter a/A/b/B: ";
my $in = <STDIN>;
chomp $in;
unless ($in =~ /^[ab]$/i) {
print "Ivalid entry\n";
exit;
}
#here you display the ascii stuff

-------------------------------------------------


KevinR
Veteran


Feb 9, 2009, 8:08 PM


Views: 2561
Re: [adiolord] display ascii

Why did you remove your code?
-------------------------------------------------


adiolord
Novice

Feb 9, 2009, 8:12 PM


Views: 2560
Post deleted by adiolord

 


adiolord
Novice

Feb 9, 2009, 8:14 PM


Views: 2559
Re: [adiolord] display ascii

I'm basically throwing rocks at everything I see untill I hit something I can kill and eat... must look like a horid mess eh


KevinR
Veteran


Feb 9, 2009, 8:59 PM


Views: 2556
Re: [adiolord] display ascii


Quote
I posted it, and saw a huge mistake lol


Thats cool, we love to point out errors. Wink
-------------------------------------------------


KevinR
Veteran


Feb 9, 2009, 9:02 PM


Views: 2554
Re: [adiolord] display ascii

Error here:


Code
for ( $i;$i <= 255; $i++ ) {  
$ascii[$i] = chr($i) ;
}


Should be:


Code
for ( $i=0;$i <= 255; $i++ ) {  
$ascii[$i] = chr($i) ;


I see you set $i at the beginning of the script but you have to initialize it inside the loop condition and assign it a value.
}
-------------------------------------------------


adiolord
Novice

Feb 9, 2009, 10:05 PM


Views: 2549
Re: [KevinR] display ascii

It works though assigned at the beginning, and also, would that not slow down the loop? or does it only work with what is after the set variable being $i=0?


FishMonger
Veteran / Moderator

Feb 9, 2009, 11:00 PM


Views: 2548
Re: [adiolord] display ascii

Why do you keep deleting your code? Is that so your instructor doesn't know that you're getting help from others?

for ( $i=0;$i <= 255; $i++ ) {

is better written as

for my $i ( 0..255 ) {

There are a number of issues I could point out, but your instructor might accept this homework as is.