Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1jB1wq-00073d-QT for pgsql-general@arkaria.postgresql.org; Sun, 08 Mar 2020 19:50:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jB1wp-0008Gr-8k for pgsql-general@arkaria.postgresql.org; Sun, 08 Mar 2020 19:50:51 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1jB1wo-0008G4-Rw for pgsql-general@lists.postgresql.org; Sun, 08 Mar 2020 19:50:50 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jB1wk-0001n6-2F for pgsql-general@lists.postgresql.org; Sun, 08 Mar 2020 19:50:49 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 0DA3C274F; Sun, 8 Mar 2020 20:50:44 +0100 (CET) Date: Sun, 8 Mar 2020 20:50:44 +0100 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: Determining the type of an obkect in plperl Message-ID: <20200308195044.GC22585@hjp.at> References: <20200304232135.GA16468@panix.com> <20200305105910.GA2832@panix.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="oTHb8nViIGeoXxdp" Content-Disposition: inline In-Reply-To: <20200305105910.GA2832@panix.com> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --oTHb8nViIGeoXxdp Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2020-03-05 05:59:10 -0500, stan wrote: > On Wed, Mar 04, 2020 at 05:09:19PM -0700, David G. Johnston wrote: > > On Wed, Mar 4, 2020 at 4:21 PM stan wrote: > > > I am in the process of writing a plperl function. In this function > > > I need to compare the data in the NEW versus OLD structures. I am > > > writing this as a generic subroutine, so I am looping through and > > > comparing the 2 to see what is different. Problem is, that I need > > > to know whether to us n !=3D or a ne comparison. > > > > > > how can I determine what the data type of the value element is? > > > > >=20 > > Not up to speed on Perl but you basically want everything to be done us= ing > > string equality - can't you just use "ne" everywhere and not worry about > > comparing numbers using string comparison logic? Might want to disabled > > warnings... [...] >=20 > Since I am just looking for differences, this may work. It should work. In fact it is probably the most robust method. Perl scalars don't have a type like "int" or "string". They can be both (or neither) at the same time. Also, a numeric comparison may not be the right thing even for values which look like numbers: Consider: #!/usr/bin/perl use v5.12; use warnings; use Scalar::Util qw(looks_like_number); my $x =3D "123456789012345678901"; my $y =3D "123456789012345678902"; say "eq(1): ", $x eq $y; if (looks_like_number($x) && looks_like_number($y)) { say "=3D=3D(2): ", $x =3D=3D $y; } else { say "eq(2): ", $x eq $y; } say "eq(3): ", $x eq $y; This will print eq(1): =3D=3D(2): 1 eq(3): on my system: The string comparisons correctly determine $x and $y to be not equal, but the numeric comparison says they are equal (the two numbers are too large to fit into a 64 bit unsigned int, so perl will use a 64 bit float for the comparison which will cause both to be rounded to 123456789012345683968). (I included the second string comparison to check that the numeric comparison didn' affect the values, which I wasn't sure of). > Presently I am getting some warnings, so I think I need to deal with the > types. I already dealt with the columns that return NULL, these are > undefined in the Perl hash, so I have to test for their existence before > attempting the compare. What do you mean by "undefined in the hash"? * The keys exist but the values are undef * The keys don't exist Those are not the same thing, and you use different functions to test for them (defined() and exists() respectively). (I think it should be the former, but I'm too lazy to test it myself) hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --oTHb8nViIGeoXxdp Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAl5lTI8ACgkQ8g5IURL+ KF17iA/+Ikkwb1KvZkrHB2Li4ilMEdhyjZpGJB4riD/gT7LjY9w6VU7ZG3VksQDD b0CIf8RcEG2JPmtvo6ifuHREI89AA2NhRFG7rGYW5w2uFO22KwwFxu5e7Bx57jYN dPw3EUinTFz7NXnKgthUkv0vPhvU1Oqa6nJauW/e21zb9AsF3OjurYijCFayJXFw VODapuJqcODYxLjIajyBUo3+BeYneWVBtCbH7BdscXqGTtIN9uaYsAlRErZr9MPX dqJuRVdv2YgbAS4qAJZpFem5Ogvche21RyMOOms0rKNfK24uZq2qyUMoXv3z6ydi 0w1AQCtPuYX5KgnlLmSJQ7L/gf2JGM6kTrTo33RjWd4gJmNESrs52r0jdCY4XtVB LI7b0bZ+IveRo7hCSdiVqEBoxD4BU1xK4dZz+6XVFKyw5LIN+SHPUtsLdhOEBah1 abKJwwbd3Bqu3nF3/vNlfpFEZdA9YNS7awcXXN9nE2QtsLcztWIcvG9f9c4yXH6q Z298UrX81gzdYIOobFi5Id2IpCmNQxw7lP3mwEJcG80xlWd56fQuXTu6RSa7logo spfq7EfGSB3iQZl+JOFOe2kLk742jNk+v9/m6CyEE3DwbEJbTtHtW9rk1yySH/sw Ncf+ebVoewt92OfI4fUJyiGdnhyzXLxQhU5lyaqNx+iHYjca10s= =TK6B -----END PGP SIGNATURE----- --oTHb8nViIGeoXxdp--