×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Making an array with unique numbers

Making an array with unique numbers

Making an array with unique numbers

(OP)
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.




RE: Making an array with unique numbers

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.

Mike
michael.j.lacey@ntlworld.com
Cargill's Corporate Web Site
Making mistakes, so you don't have to. <grin>

RE: Making an array with unique numbers

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:



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

print "$arrayElement\n";

}



Please tell me if I am correct.

-Vic

vic cherubini
malice365@hotmail.com
epic software
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
Wants to Know: Java, Cold Fusion, Tcl/TK
====

RE: Making an array with unique numbers

(OP)
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 :@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


RE: Making an array with unique numbers

Ahhh. I get it now. Ok, well I agree with what Mike has to say. If you put the information in a hash array:


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



I think that is what Mike is trying to say.

Hope this helps.


-Vic

vic cherubini
malice365@hotmail.com
epic software
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
Wants to Know: Java, Cold Fusion, Tcl/TK
====

RE: Making an array with unique numbers

(OP)
Vic,
Iam sorry i am not able to get what ur trying to tell,

Mike help me .

RE: Making an array with unique numbers

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

vic cherubini
malice365@hotmail.com
epic software
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
Wants to Know: Java, Cold Fusion, Tcl/TK
====

RE: Making an array with unique numbers

(OP)
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 "||" 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.


Thendal



 

RE: Making an array with unique numbers

(OP)
Guys were are u all

RE: Making an array with unique numbers

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....)

Mike
michael.j.lacey@ntlworld.com
Cargill's Corporate Web Site
Making mistakes, so you don't have to. <grin>

RE: Making an array with unique numbers

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

#!/usr/local/bin/perl

print "Content-type: text/html\n\n";

$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 "$counter\n";
  if ($item ne $carry)
    {
    push (@results, $item);
    }
  $carry = $item;
}

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

#print the new results array
print "@results\n";

fortytwo
will@hellacool.co.uk
my test site

RE: Making an array with unique numbers

I just looked at that code.  It is terrible.  Here is a better version


#!/usr/local/bin/perl

print "Content-type: text/html\n\n";

@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 "@array<br>\n";
print "@results\n";


it is designed for running on the web, hence the html sneaked in there

fortytwo
will@hellacool.co.uk
my test site

RE: Making an array with unique numbers

(OP)
Cool ! Thank you SO MUCH.Today really iam happy to see ur code.Thanks alot 4u(42)

RE: Making an array with unique numbers

(OP)
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

RE: Making an array with unique numbers

(OP)
Sorry 42,

  I misunderstood it,
worked with
@a=(a,a,a,a,b,b,b,b,c,c,c,c);
Thanks

RE: Making an array with unique numbers

Here's a simple subroutine to get only the unique values in an array:

sub unique {

my(&#64;list) = &#64;_;

my %list = map { $_ => 1 } &#64;list;

return sort keys %list;

} # unique


Use it like this:

&#64;nodups = unique(&#64;hasdups);


Trust me, it works, although it looks kinda wierd. If you want, I'll explain what it's doing.

RE: Making an array with unique numbers

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?


"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."

RE: Making an array with unique numbers

(OP)
What the &#64 will do is it a bug  in the forum.
or interpreting the @ symbol.

:)
thendal

RE: Making an array with unique numbers

I'm flattered you thought my solution was good enough to go into a FAQ. I got the idea from the perlfunc man page description of the map function. (You all have read the perl man pages, haven't you?)

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close