rups has asked for the wisdom of the Perl Monks concerning the following question:

I have a database which contains mobile numbers. How do I write perl script which get all of numbers into array & check new number already exist or not in that array?

Create Table:** CREATE TABLE consumeruser ( ConsumerId int(10) NOT NULL AUTO_INCREMENT, ConsumerName varchar(45) DEFAULT NULL, ConsumerMobNo varchar(10) DEFAULT NULL, PRIMARY KEY (ConsumerId) ) ENGINE=InnoDB AUTO_INCREMENT=4494 DEFAULT CHARSET=latin1
#!/usr/bin/perl -w use strict; use DBI; use warnings qw(all); use Getopt::Long; use Pod::Usage; use Text::CSV_XS; my $username = 'root'; # set your MySQL username my $password = 'xxxx'; # set your MySQL password my $database = 'app'; # set your MySQL database name my $server = 'localhost'; # set your server hostname (probabl +y localhost) my $dbh = DBI->connect("DBI:mysql:$database;host=$server", $u +sername, $password) || die "Could not connect to database: $DBI::errstr"; my $CustomerMobileNumber = 9999999; my @MobileNumbers; my $mobileNumberQuery = "select ConsumerMobNo from consumerus +er"; my $sth = $dbh->prepare($mobileNumberQuery); $sth->execute(); while(my @row = $sth->fetchrow_array()){ push @MobileNumbers, $row; if (present){ 9999999 found in array; } else{ 9999999 not found in array; } }

Replies are listed 'Best First'.
Re: array search on mysql result
by LanX (Saint) on Sep 20, 2014 at 10:40 UTC
    > check new number already exist or not

    why can't you use a WHERE-clause in SQL?

    Seems off topic here...

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: array search on mysql result
by roboticus (Chancellor) on Sep 20, 2014 at 13:16 UTC

    rups:

    As LanX indicated, that's the kind of work you can have your database do. In fact, you can also prevent duplicates from being inserted into the table by putting a unique index on the ConsumeMobNo column. However, that table looks like it will be a problem in the future: You're forcing a 1:1 relationship between customers and mobile phone numbers. I'd personally have a table that has the phone number as the primary key and have a foreign key relationship to the consumeruser table.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: array search on mysql result
by choroba (Cardinal) on Sep 20, 2014 at 10:58 UTC
    Crossposted at StackOverflow. It's considered polite to inform about crossposting, so people not attending both sites don't waste their efforts hacking a solution to a problem already solved at the other end of the Internet.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: array search on mysql result
by Laurent_R (Canon) on Sep 20, 2014 at 12:37 UTC
    If you want to check the existence of something in a list of items, then you should probably use a hash rather than an array. Store your items as keys of the hash and just put anything you like as value (it's not gonna be used anyway). Then you only need to so something like this:
    if (exists $mobile_numbers{$new_number}) { # ... } else { $mobile_numbers{$new_number} = 1; # ... }