hari9 has asked for the wisdom of the Perl Monks concerning the following question:
Hi All.
I'm writing a perl script to check if every entry to an array is unique before putting it into an array.
I expected my code would give an output like this:
Enter a number: 1But I'm getting an infinite loop which doesnt break when I type "^Z"
also it doesnt prompt "already exist" when I enter redudant values
$i=1; @arr=(); print "Enter a number"; while(1) { $val=<>; if ($i eq 1) #Enter the first entry into the +array directly. { push(@arr,$val); # push it into the array 'arr'. } else {for($x=1;$x<=scalar(@arr);$x++) #run through the 'arr'. { if($arr[$x] eq $val) # check if every entry is unique. { print "Already exist at $x position";# if it is not, tell the user t +o enter a unique one and ignore the present (redundant)entry. exit;} else { push(@arr,$val); #If unique, push into the ar +ray. } } } print" @arr \t"; } print "Array is:", @arr; #display the array.
Any help would be appreciated.
Thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: question on Arrays
by jethro (Monsignor) on Jul 29, 2010 at 15:31 UTC | |
|
Re: question on Arrays
by BioLion (Curate) on Jul 29, 2010 at 15:44 UTC | |
|
Re: question on Arrays
by derby (Abbot) on Jul 29, 2010 at 15:19 UTC | |
by hari9 (Sexton) on Jul 29, 2010 at 15:29 UTC | |
by ikegami (Patriarch) on Jul 29, 2010 at 15:33 UTC | |
by derby (Abbot) on Jul 29, 2010 at 15:37 UTC | |
|
Re: question on Arrays
by dasgar (Priest) on Jul 29, 2010 at 15:43 UTC | |
|
Re: question on Arrays
by Utilitarian (Vicar) on Jul 29, 2010 at 15:33 UTC | |
|
Re: question on Arrays
by kennethk (Abbot) on Jul 29, 2010 at 15:41 UTC |