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.
ThanksIn reply to question on Arrays by hari9
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |