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

Merging using Perl

Status
Not open for further replies.

akrishmohan

Programmer
May 3, 2002
20
US
HI all

I am very new to perl... I was just wondering about the solution to this question.

I have two arrays A and B containing integers. Now i want to merge the two arrays. I was kinda confused with the syntax in perl so I didnt know which method to chhose. Anyways the idea is to remove the elements in B that exist in A before merging them.

An efficient algorithm would be helpful

Thanks
Regards
Perl_Krishna
 
#!/usr/bin/perl
use strict;

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

my @a = qw( a b c d e f g);
my @b = qw(e f g h i j k l);

my ($item,$item2);
foreach $item (@a){
foreach $item2 (@b){
if ($item eq $item2){
$item2 = '';
}
}
}

foreach $item2 (@b){
if ($item2 ne ''){
push(@a, $item2);
}
}
print join(',',@a);
 
Code:
#!perl
use strict;
use warnings;

my @a = (0..9);
my @b = (2, 4, 6, 8, 10, 12);

my @c;
for my $c (@a, @b) {
    push(@c, $c) unless grep(/^$c$/, @c);
}

print "$_\n" for @c;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top