For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2006 > DBI Strings and numbers









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author DBI Strings and numbers
petermonsson@yahoo.dk

2006-07-12, 3:57 am

Hi all,

I'm trying to get my head around the DBI package and unfortunately I've
hit a barrier when it comes to comparing two fields. If I compare two
Strings I should use the eq operator and if I compare two numbers I
should use == but what I I don't know the type of what I compare except
that they're the same type? How can I compare them without getting a
warning from perl f. x. "Use of uninitialized value in string gt at
../comparedb.pl line 162."

Here is the relevant code snippet:

sub greaterThan {
my($a1, $a2) = @_;
my @array1 = @$a1;
my @array2 = @$a2;

for(my $count=0; $count <= $#array1; $count++) {
if ($array1[$count] gt $array2[$count]) { # problem with
numbers here
return 1;
}
}
return 0;
}

Background: I've got two databases which are almost identical and I
need to be able to see the rows of every table that differs from the
other so I loop over all tables and their rows and print the rows that
are different.

Thank you in advance
Peter

Paul Lalli

2006-07-12, 6:57 pm

petermonsson@yahoo.dk wrote:
> I'm trying to get my head around the DBI package and unfortunately I've
> hit a barrier when it comes to comparing two fields. If I compare two
> Strings I should use the eq operator and if I compare two numbers I
> should use == but what I I don't know the type of what I compare except
> that they're the same type? How can I compare them without getting a
> warning from perl f. x. "Use of uninitialized value in string gt at
> ./comparedb.pl line 162."


You are mis-diagnosing your problem. The warning you receive has
nothing to do with == vs eq or > vs gt. It has to do with at least one
of your arguments being undefined. That's what you need to check for.

> Here is the relevant code snippet:
>
> sub greaterThan {
> my($a1, $a2) = @_;
> my @array1 = @$a1;
> my @array2 = @$a2;
>
> for(my $count=0; $count <= $#array1; $count++) {


You're looping once for each element of @array1, without regard to how
many elements are in @array2.

> if ($array1[$count] gt $array2[$count]) {
> # problem with numbers here


No, you have a problem with one of the arrays having less elements than
the other, not with whether or not they're numbers.

Check the sizes of the arrays first. If the sizes are not the same,
then obviously your rows are not the same. How you deal with that is
up to you.

To answer the question you actually asked - which is not at all
relevant to the code and error message you showed, check to see if the
arguments are numbers before comparing. If they are, use > or ==. If
not, use gt or eq.

There are a variety of ways to make these checks, with varying levels
of precision. Check out Scalar::Util's looks_like_number function, and
the Regexp::Common family of regular expressions.

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com