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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Data Sorting Example

 



AlphaScorpious
Novice

Oct 3, 2013, 6:27 PM

Post #1 of 5 (951 views)
Data Sorting Example Can't Post

In order to facilitate my learning of PERL I'm attempting to rewrite an old VBA for Excel. I'm starting this thread to confine all my questions in one place for this project.

Very simply, I'm assuming an imaginary database containing the positive integers from 1 to 50,000. The user will enter 2 numbers as a range and then decide what kind of sorting to do.

Obviously I need to ensure that the user is entering numbers between 1 and 50,000 so I have this bit of code:


Code
print "Enter an integer between 1 and 50000.".$NLCR; 
chomp ($LowNum = <STDIN>);
until (($LowNum >= 1) || ($LowNum <= 50000)) {
print "ERROR: Selection out of range.".$NLCR;
print "Enter an integer between 1 and 50000.".$NLCR;
chomp ($LowNum = <STDIN>);
}


What is supposed to happen is that the user is to be prompted to keep entering numbers until an acceptable one is entered but the program just goes to next line of code even when numbers out of range are entered. What am I doing wrong?


2teez
Novice

Oct 3, 2013, 7:36 PM

Post #2 of 5 (947 views)
Re: [AlphaScorpious] Data Sorting Example [In reply to] Can't Post

Hi,

Please try the code below and compare the result with yours

Code
use warnings; 
use strict;

print "Enter an integer between 1 and 50000.", $/;
chomp( my $LowNum = <STDIN> );

while ( ( $LowNum < 1 || $LowNum > 50000 ) ) {
print "ERROR: Selection out of range.", $/;
print "Enter an integer between 1 and 50000.", $/;
chomp( $LowNum = <STDIN> );
}


Then to add to that, the language is Perl not PERL as in acronym, just saying.
Hope this helps.


Laurent_R
Veteran / Moderator

Oct 4, 2013, 3:21 PM

Post #3 of 5 (938 views)
Re: [2teez] Data Sorting Example [In reply to] Can't Post

Well, if you want to sort all the positive integers from 1 to 50,000, just don't sort them and don't even look at your database. Just print them in their natural order:


Code
print join " ", (1..50000), "\n";



BillKSmith
Veteran

Oct 5, 2013, 7:30 AM

Post #4 of 5 (929 views)
Re: [AlphaScorpious] Data Sorting Example [In reply to] Can't Post

Do not assume that the user has entered the right data, validate it. I recommend the use of a module for prompting. It can take care of details we forget about.

Code
use strict; 
use warnings;
use IO::Prompt::Hooked;

my %opt = (
message => "Enter an integer between 1 and 49999:",
validate => sub {$_[0] !~ /[^\d]/ and $_[0] > 0 and $_[0] <= 49999},
error => "ERROR: Selection out of range\n",
tries => 5,
);
my $LowNum = prompt( %opt );

@opt{ 'message', 'validate' } = (
'Enter an integer between ' . ($LowNum+1) . 'and 50000:',
sub {$_[0] !~ /[^\d]/ and $_[0] > $LowNum and $_[0] <= 50000},
);
my $HighNum = prompt( %opt );

Good Luck,
Bill


AlphaScorpious
Novice

Oct 5, 2013, 2:17 PM

Post #5 of 5 (921 views)
Re: [BillKSmith] Data Sorting Example [In reply to] Can't Post


In Reply To
Please try the code below and compare the result with yours

That worked, thank you.


In Reply To
Well, if you want to sort all the positive integers from 1 to 50,000, just don't sort them and don't even look at your database. Just print them in their natural order:

Thanks, but the exercise is to emulate a database so I'm pulling out a section of the numbers from 1 to 50,000 and then giving the user the option to do things like display only prime numbers, or exclude numbers starting with 3 or ending with 8, etc.


In Reply To
Do not assume that the user has entered the right data, validate it. I recommend the use of a module for prompting.

Thanks, but I'm just starting with Perl so I'll wait to add modules until I understand enough of the basics.


(This post was edited by AlphaScorpious on Oct 5, 2013, 2:19 PM)

 
 


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

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