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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

erase duplicated elements in array and make new array

Status
Not open for further replies.

PerlElvir

Technical User
Aug 23, 2005
68
Hi all,

I want to ask can I erase duplicate elements and make new array without that old elements.

example:
@ins= ("ace","name","ace","car");
@outs= ("2","3");

foreach $var1 (0 .. $#ins)


{



foreach $var2 (0 .. $#outs)
{

$buffer = $outs[$var2]."----".$ins[$var1];


print $buffer."\n";
push(@new_array,$buffer)
}
}

now I get:

2----ace
3----ace
2----name
3----name
2----ace
3----ace
2----car
3----car

so can I erase

2----ace
3----ace

and to get in @new_array:


2----ace
3----ace
2----name
3----name
2----car
3----car


 
To get rid of duplicate elements put this in your code.

undef %saw;
@ins1= ("ace","name","ace","car");
@ins = grep(!$saw{$_}++, @ins1);


Now @ins only has unique elements.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top