Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making an array with unique numbers

Status
Not open for further replies.

thendal

Programmer
Aug 23, 2000
284
Hey guys!

Iam trying to make an array values as unique.

for example iam having an array

@a=(1,2,3,4,4,5)

I need an output @a=(1,2,3,4,5);

I tried like this.

for($i=0;$i<@a;$i++)
{
for($j=$i+1;$j<@a;$j++)
{
if($i eq $j)
{
splice(@a,$j,1);
}
}
}

using this i got the output 12345


But when i change the array values to some thing like this

@a=(1,2,3,4,4,4,4,4,4,4,5)

its giving an output not with the unique values

@a=(1,2,3,4,4,4,5)

I know iam missing some where in logic ,I hope u can help me.




[sig][/sig]
 
have you thought about using a hash rather than a standard array?

you could have the same string as the key and the value in each element -- and would be unique

$a{1}=1;
$a{1}=1;
$a{1}=1;

results in only one array element. [sig]<p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>Making mistakes, so you don't have to. &lt;grin&gt;[/sig]
 
I am not 100% sure of your question, but are you asking to print out all the values of an array.

If so, then this will work:

[tt]

@array = qw(1,2,3,4,4,4,4,4,5,6);
foreach $arrayElement (@array) {

print &quot;$arrayElement\n&quot;;

}

[/tt]

Please tell me if I am correct.

-Vic [sig]<p>vic cherubini<br><a href=mailto:malice365@hotmail.com>malice365@hotmail.com</a><br><a href= software</a><br>====<br>
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director<br>
Wants to Know: Java, Cold Fusion, Tcl/TK<br>
====[/sig]
 
Thanks mike,vic.


My problem is iam having an array

@a=(1,2,3,4,4,4,4,5)

I need to get an output as 12345

ie) only unique numbers in the output numbers


Mike i didn't thought of using hash ,I don't know how to use hash in this situation.advise me.

vic ,i don't want to display array values.I am having an array where values will be repeating .I don't need repeated values in the array only
unique values.

in simple

input :mad:a=(1,2,2,2,2,3,3,3,3,3,4,4,4,4,5,5,5);

output: @a=(1,2,3,4,5);

Vic i hope u understand my problem.Any help will be appreciated

Thanks in advance

#-)
[sig][/sig]
 
Ahhh. I get it now. Ok, well I agree with what Mike has to say. If you put the information in a hash array:

[tt]
%hash = ('1' => 1,
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 4);

[/tt]

I think that is what Mike is trying to say.

Hope this helps.


-Vic
[sig]<p>vic cherubini<br><a href=mailto:malice365@hotmail.com>malice365@hotmail.com</a><br><a href= software</a><br>====<br>
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director<br>
Wants to Know: Java, Cold Fusion, Tcl/TK<br>
====[/sig]
 
&quot;trying to say&quot; <grin> are you suggesting that I cannot express myself in the Queens English? [sig]<p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>Making mistakes, so you don't have to. &lt;grin&gt;[/sig]
 
Vic,
Iam sorry i am not able to get what ur trying to tell,

Mike help me . [sig][/sig]
 
Heh.
Basically, I was trying to say what Mike was saying with some code.

Mike, as he said, put it in plain English, I tried to put it in code.

So basically, the value of hash key number 4 is 4, and the value of hash key number 5 is also 4. I am not the best at expressing myself in words, sorry.

I am curious, what are you using this for?

Sorry for the mixup. I'm just a 16 year old trying to get into the world of Perl programming.

Sorry.

-Vic

[sig]<p>vic cherubini<br><a href=mailto:malice365@hotmail.com>malice365@hotmail.com</a><br><a href= software</a><br>====<br>
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director<br>
Wants to Know: Java, Cold Fusion, Tcl/TK<br>
====[/sig]
 
Vic:-

I too young to the perl programming world ,But i love this language because of its robust text manipulation.

My problem iam searching for a keyword and from dropdown box in a database(flat file).(iam using &quot;||&quot; in the matching pattern)

Iam collecting all the matched values in the array.
It will be having values matched with both keyword and dropdown,so it will be populated twice in the array .

so i need to get an array with only unique values.

I hope this will convey what i need.

s-)
Thendal





[sig][/sig]
 
Sorry Thendal -- This is CGI stuff and I know (*&%% all about CGI... so I was worried about giving advice that would lead you up the wrong path.

If you can give us some more detail, some code maybe, I'd be happy to have a go..... (I really *should* learn something about CGI....) [sig]<p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>Making mistakes, so you don't have to. &lt;grin&gt;[/sig]
 
Hi thendal,

I was interested in this one. This seems to work OK. I won't try and explain it as it seems quite easy to follow and I am quite tired, but if you want I will :)
[tt]
#!/usr/local/bin/perl

print &quot;Content-type: text/html\n\n&quot;;

$array[0] = 1;
$array[1] = 2;
$array[2] = 2;
$array[3] = 2;
$array[4] = 2;
$array[5] = 3;
$array[6] = 4;
$array[7] = 4;
$array[8] = 4;
$array[9] = 4;
$array[10] = 4;
$array[11] = 4;
$array[12] = 4;
$array[13] = 4;
$array[14] = 4;
$array[15] = 5;
$array[16] = 6;

$carry = null;
$counter = 0;

foreach $item (@array)
{
$counter++;
print &quot;$counter\n&quot;;
if ($item ne $carry)
{
push (@results, $item);
}
$carry = $item;
}

#print the original array
print &quot;@array<br>\n&quot;;

#print the new results array
print &quot;@results\n&quot;;
[/tt] [sig]<p>fortytwo<br><a href=mailto:will@hellacool.co.uk>will@hellacool.co.uk</a><br><a href= test site</a><br> [/sig]
 
I just looked at that code. It is terrible. Here is a better version :)

[tt]
#!/usr/local/bin/perl

print &quot;Content-type: text/html\n\n&quot;;

@array = (1,2,2,2,2,3,4,4,4,4,4,4,4,4,4,5,6);

$carry = null;
$counter = 0;

foreach $item (@array)
{
$counter++;
if ($item ne $carry)
{
push (@results, $item);
}
$carry = $item;
}

print &quot;@array<br>\n&quot;;
print &quot;@results\n&quot;;
[/tt]

it is designed for running on the web, hence the html sneaked in there [sig]<p>fortytwo<br><a href=mailto:will@hellacool.co.uk>will@hellacool.co.uk</a><br><a href= test site</a><br> [/sig]
 
Cool ! Thank you SO MUCH.Today really iam happy to see ur code.Thanks alot 4u(42)

;-) [sig][/sig]
 
Hai 42!

Here is another issue.This works fine in an array which contains numbers .What about if the array conatins

@a=(A,A,A,b,b,b,c,c,c,c,c);


Thendal [sig][/sig]
 
Sorry 42,

I misunderstood it,
worked with
@a=(a,a,a,a,b,b,b,b,c,c,c,c);
Thanks
[sig][/sig]
 
Here's a simple subroutine to get only the unique values in an array:
Code:
sub unique {

my(@list) = @_;

my %list = map { $_ => 1 } @list;

return sort keys %list;

} # unique

Use it like this:
Code:
@nodups = unique(@hasdups);

Trust me, it works, although it looks kinda wierd. If you want, I'll explain what it's doing.
 
Check it! I made a FAQ for this answer @:
faq219-420
This concept is in plenty of books on perl, but hey, what does open source mean if not redundancy, eh?

&quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
What the &amp;#64 will do is it a bug in the forum.
or interpreting the @ symbol.

:)
thendal
 
It was a bug and it seems to be fixed now.
Mike
michael.j.lacey@ntlworld.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top