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

Perl Array - Unique 1

Status
Not open for further replies.

not4google

Programmer
Nov 6, 2006
15
0
0
GB
Hi all,

I have an array which muliple values in it and what I wanted to do is add the 5th element of the array to a new array which only stores unique values.

I would normally do this in shell scripting but wanted to learn some Perl so any suggestions would be much appreciated,

Thanks,
 
Lets see what perl code you have or have tried so far.



- Kevin, perl coder unexceptional!
 
Here's one way to do it. You will have to define the @array and @newArray.

Code:
foreach $val (@newArray) {
        if ($val != $array[4]) {
                 unshift @newArray, "$array[4]";
        print @newArray;
        last;
        }
}
 
max1x, it looks like that the code shown will add the element as long as the $newArray[0] does not equal to $array[4].
The following code should work, perhaps a little awkward.
# add fifth element if not a double in @newArray
my @array=(1,4,7,10,11);
my @newArray=(2,5,6,7,8,10,9);
my $duplicate=0;
my $size=@newArray;
for($i=0;$i<$size;$i++){
if($newArray[$i]==$array[4]){ $duplicate=1;}
}
if($duplicate==0){
push @newArray,$array[4];
print @newArray;
}
 
Code:
my @oldarray = qw(1 2 3 4 8 6 7);
my @newarray = qw(1 2 3 4 5 6 7);

unless (grep {$_ eq $oldarray[4]} @newarray) {
 push @newarray,$oldarray[4];
}

print "@newarray";

- Kevin, perl coder unexceptional!
 
PC888,

The foreach loop will check each element of @newArray with that of $array[4] (not just the first) and if they don't match, that's the only time it will append to the Beginning of the @newArray.

Though I like Kevin's way better, I'm not "unexceptional" like him :).

 
if the arrays were gigantic another method would be beter to get out as soon as possible:

Code:
my @array=(some elements);
my @newArray=(millions of elements);
my $duplicate=0;
foreach my $line (@newarray){
  if($line eq $array[4]){
      $duplicate=1;
      [b]last;[/b]
}
unless($duplicate){
    push @newarray,$array[4];
}

- Kevin, perl coder unexceptional!
 
Max1x,

Perhaps I didn't express it correctly.

What I mean is that the code will insert the element and quit as soon as it finds a different array in newArray. If the first element happens to be different from array[4], then it will be inserted and quit without checking for duplication of the remaining elements of newArray.

For example,
my @array=(1,4,7,10,9);
my @newArray=(2,5,6,7,8,10,9);
will produce
925678109
which incurs a duplication of '9'.

On the other hand, we both agree that Kevin's code is concise and functional!
 
Looks like you work with Perl a lot from the number of answers you've given. I am new to Perl, so it's almost sure I will need some help from you some day!
Cheers.
 
Actually, I'm not in the IT industry and don't get to do much with Perl, but I like it. I learn from the likes of Kevin, Ishind, Kirsle and others, they are the true masters...
 
You're right, we're lucky to have the masters around, and most of all, helping out. I learn quite a bit by reading the posts and the answers.
Nice talking to you, cheers.
 
Code:
[URL unfurl="true"]http://www.bayview.com/blog/2002/07/21/unique-arrays/[/URL]
 
WinblowsME

how would you apply that approach to the question?

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top