Comparing Variables in Perl

Hi.

I have three arrays.

@a=('AB','CD','EF');
@b=('AB,'DG',HK');
@c=('DD','TT','MM');

I want to compare the elements of the first two array and if they match then so some substition.

I tried using the if statement using the scalar value of the array but its not giving me any output.

Help please !!!!!!!!!!!

Can you explain the comparison that you are looking for?
Is it one to one:

if ($a[0] = $b[0])
{
   your code here
}

Is it one to many:

for (@a){
   if ($b[0] = $_)
   {
      your code here
   }
}

Or is it many to many, in which case a database is much more efficient....

if ($a[0] = $b[0])
{
your code here
}

As of now one to one but is not working.

Cheers.

If you wanted to loop through each element in an array to do a one-to-one evaluation for each element pair in an array, you can:

$elementCount = @a;
$count = 0;

while ( $count < $elementCount )
{
   if ( $a[$count] == $b[$count] )
   {
      print "\$a[$count]: $a[$count] = \$b[$count]: $b[$count]\n";
   }
   $count++;
}

(remember to use eq instead of == if you are working with text)

EDIT: changed line in red

For what it's worth, I had to edit your arrays in the example above:

@a=('AB','CD','EF');
@b=('AB','DG','HK');
@c=('DD','TT','MM');

None of your example compare anything, "=" is the assignment operator.

To compare numbers use "==" to compare strings use "eq", of course sometimes its never that simple but it looks like it would be in this case if the examples are what is really being compared. There are also modules that compare arrays, one I believe is called Array::Compare

Thanks Kevin - yes, I didn't go back to edit my first post (it started out as a basic framework) that morphed into a "code sample".
If you'll notice my second post in this thread does provide an actual comparison .

Cheers!

ahhh... so it does :slight_smile: