Re: Programming Question

Christopher Pryce <[email protected]>
Newsgroups gmane.comp.db.mysql.perl
Message-ID <[email protected]>
[ CC to Author ]

On Oct 4, 2005, at 10:37 AM, Douglas S. Davis wrote:

> My problem (and I am sure it must be simple to do) is that I can't  
> figure out how to compare the list of possible numbers (100 - 499)  
> with the list of already in use numbers returned by my query.  I've  
> tried various loops and while statements all to no avail.  Usually  
> what I get is that for *each* number in the result set of my query, it  
> prints out all of the numbers from 100 - 499.
>
> Can anyone point me in the right direction.    So in other words, when  
> I do my fetchrow() on the MySQL table, how can I take the results and  
> compare them to the 100 - 499 set of numbers.
>

My suggestion - don't fetch each row, unless there is a compelling  
reason. Fetch all of the matching rows at once as a hashref. For  
instance, if you have a table called tblcourses with the following  
structure:

courseNumber | courseTitle | departmentNumber | creditHours |  
instructor |
------------------------------------------------------------------------ 
--

# UNTESTED
my $keyfield = 'courseNumber';

my $courses = $dbh->selectall_hashref( q(Select * From tblcourses Where  
departmentNumber = ?), $keyfield, $departmentNumber );

foreach ( 100..499 ) {
	# if $courses -> { $_ } is defined, the class exists
	next if $courses -> { $_ };
	
	# If not do something with the unassigned course number in $_
}

$courses has a structure like:

$courses = {
			'100' => {
						'courseTitle' => 'Introduction to Programming',
						'courseNumber' => '100',
						'departmentNumber => '10',
						'creditHours'	=> '3.0',
						'instructor => 'Douglas S. Davis'
					},	
			....
		};

						


-- 
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe:    http://lists.mysql.com/[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.