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:
Checking for duplicate input

 



tbone587
User

Apr 2, 2011, 7:23 PM

Post #1 of 11 (2224 views)
Checking for duplicate input Can't Post

I am working on a DNS entry script and I am trying to figure out a way to check the input for duplicates. The program first asks you for a numerical value of entries you want to make and then it asks you for the names. If you enter 3, then iad01, iad02, iad01, it should tell you "You have already entered iad01". Here is my input code below:


Code
###########  Domain Name Entry  ################################################################################# 

my $Tries = 0;
my $Max_Tries = 2;
my @DNS_Entries;
my $DNS_Entry;
my $Input_Counter = 1;


for (1..$DNS_Entry_Count)
{

DNSINPUT:

if ($Input_Counter == 1)
{
print "\n";
print "Please enter the name of the first DNS Entry: ";
}

elsif ($Input_Counter == 2)
{
print "\n";
print "Please enter the name of the second DNS Entry: ";

}

elsif ($Input_Counter == 3)
{
print "\n";
print "Please enter the name of the third DNS Entry: ";
}

elsif ($Input_Counter == 4)
{
print "\n";
print "Please enter the name of the fourth DNS Entry: ";

}

elsif ($Input_Counter == 5)
{
print "\n";
print "Please enter the name of the fifth DNS Entry: ";
}

else
{
print "\n";
print "Please enter the name of the next DNS Entry: ";
}

chomp($DNS_Entry=<STDIN>);
$DNS_Entry = lc($DNS_Entry); #Converts entries to lower case


if ($DNS_Entry =~ m/iad(\d)/i)
{
print "You have entered an IAD entry.\n";
}

elsif ($DNS_Entry =~ m/cpe(\d)/i)
{
print "You have entered a CPE entry.\n"
}

else
{
print "This is not a valid entry\n";
goto DNSINPUT;
}

push @DNS_Entries, $DNS_Entry; #Adds inputted users to Array
$Input_Counter++;

}


#################################################################################################################


Can someone help me figure out a way to check input. I have tried a few things, but it just seems to create an endless loop.


FishMonger
Veteran / Moderator

Apr 2, 2011, 7:49 PM

Post #2 of 11 (2222 views)
Re: [tbone587] Checking for duplicate input [In reply to] Can't Post

First, get rid of the if/elsif/else block and replace it with a simple for loop.

Place each entry into a hash and test for the existence of the hash key at each iteration of the for loop.

Of course, that also implies that you loaded the prior DNS data into the hash so that you can test against the already existing data.


tbone587
User

Apr 2, 2011, 7:51 PM

Post #3 of 11 (2219 views)
Re: [FishMonger] Checking for duplicate input [In reply to] Can't Post

Only reason I have all of the if/elsif statements is because i wanted it to say first, second, third, fourth, fifth. I havent used a hash before, is that something easy to implement into my code?


FishMonger
Veteran / Moderator

Apr 2, 2011, 7:52 PM

Post #4 of 11 (2219 views)
Re: [tbone587] Checking for duplicate input [In reply to] Can't Post


Code
C:\>perldoc -q duplicate 
Found in C:\Perl\lib\pods\perlfaq4.pod
How can I remove duplicate elements from a list or array?
(contributed by brian d foy)

Use a hash. When you think the words "unique" or "duplicated", think
"hash keys".

If you don't care about the order of the elements, you could just create
the hash then extract the keys. It's not important how you create that
hash: just that you use "keys" to get the unique elements.

my %hash = map { $_, 1 } @array;
# or a hash slice: @hash{ @array } = ();
# or a foreach: $hash{$_} = 1 foreach ( @array );

my @unique = keys %hash;

If you want to use a module, try the "uniq" function from
"List::MoreUtils". In list context it returns the unique elements,
preserving their order in the list. In scalar context, it returns the
number of unique elements.

use List::MoreUtils qw(uniq);

my @unique = uniq( 1, 2, 3, 4, 4, 5, 6, 5, 7 ); # 1,2,3,4,5,6,7
my $unique = uniq( 1, 2, 3, 4, 4, 5, 6, 5, 7 ); # 7

You can also go through each element and skip the ones you've seen
before. Use a hash to keep track. The first time the loop sees an
element, that element has no key in %Seen. The "next" statement creates
the key and immediately uses its value, which is "undef", so the loop
continues to the "push" and increments the value for that key. The next
time the loop sees that same element, its key exists in the hash *and*
the value for that key is true (since it's not 0 or "undef"), so the
next skips that iteration and the loop goes to the next element.

my @unique = ();
my %seen = ();

foreach my $elem ( @array )
{
next if $seen{ $elem }++;
push @unique, $elem;
}

You can write this more briefly using a grep, which does the same thing.

my %seen = ();
my @unique = grep { ! $seen{ $_ }++ } @array;



FishMonger
Veteran / Moderator

Apr 2, 2011, 8:06 PM

Post #5 of 11 (2213 views)
Re: [tbone587] Checking for duplicate input [In reply to] Can't Post


In Reply To
Only reason I have all of the if/elsif statements is because i wanted it to say first, second, third, fourth, fifth. I havent used a hash before, is that something easy to implement into my code?


Just change the verbiage.

This is untested and is pseudo code.

Code
for ( 1..$DNS_Entry_Count ) { 
print "Please enter the name of your DNS Entry: ";
my ($key = <STDIN>);
if (exists $dns{$key}) {
# add the entry to the hash
}
else {
# loop back to have the user re-enter the data
}
}



(This post was edited by FishMonger on Apr 2, 2011, 8:07 PM)


tbone587
User

Apr 2, 2011, 8:23 PM

Post #6 of 11 (2208 views)
Re: [FishMonger] Checking for duplicate input [In reply to] Can't Post

How do I do it sense I was pushing elements into an array? I basically want to continue to push into the array after the input, as well as I guess adding to a hash?


FishMonger
Veteran / Moderator

Apr 3, 2011, 8:19 AM

Post #7 of 11 (2198 views)
Re: [tbone587] Checking for duplicate input [In reply to] Can't Post

If the data is already in the hash, why do you want to duplicate that data in an array?

Instead of using push to build the array as you go, simply use the hash keys to create the array in one statement after the for loop.

Code
my @DNS_Entries = keys %dns;



tbone587
User

Apr 3, 2011, 3:03 PM

Post #8 of 11 (2187 views)
Re: [FishMonger] Checking for duplicate input [In reply to] Can't Post

im lost, ill have to read more about hashes. I never used them before...

In Reply To


FishMonger
Veteran / Moderator

Apr 3, 2011, 3:28 PM

Post #9 of 11 (2185 views)
Re: [tbone587] Checking for duplicate input [In reply to] Can't Post

I'm tied up on other things right now, but will try to give you a more complete example later tonight.


tbone587
User

Apr 5, 2011, 7:09 AM

Post #10 of 11 (2095 views)
Re: [FishMonger] Checking for duplicate input [In reply to] Can't Post

FishMonger, did you by any chance ever get the opportunity to setup an example? I would really appreciate it.


FishMonger
Veteran / Moderator

Apr 5, 2011, 10:58 PM

Post #11 of 11 (2071 views)
Re: [tbone587] Checking for duplicate input [In reply to] Can't Post

Sorry, I haven't had the time. I'm currently on a business trip and just finished a 14 hour day. I won't have any free time until Thursday.

 
 


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

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