perl Query about uninit values in perl

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

perl Query about uninit values in perl

Suma
Hi Guru!
I have a code for merging 2 arrays so that the 3rd array will have content of arr1 and arr2 without any duplicate values . Code works, but i get a warning as below :

"Use of uninitialized value $arr3[6] in join or string at Arrcpy.pl line 23."
what could the warning be?


#!/usr/bin/perl
use warnings;
$\="\n";
@arr1= qw(Red Orange Blue);
@arr2=qw(Blue Red Green White);

@arr3=@arr1;

for ($i=0;$i<@arr2;$i++)
{
        for ($j=0;$j<@arr3;$j++)
        {
                if ($arr2[$i] eq $arr3[$j])
                {
                        #$match=1;
                        last;
                }
                #$match=0;
        }
        push @arr3,$arr2[$j];
}

print "@arr3";


Reply | Threaded
Open this post in threaded view
|

Re: perl Query about uninit values in perl

Guru
Administrator
Just a small mistake. In the push statement, you used $j instead of $i, and also use the flag variable($match):


my $match;
for ($i=0;$i<@arr2;$i++)
{
        $match=0;
        for ($j=0;$j<@arr3;$j++)
        {
                if ($arr2[$i] eq $arr3[$j])
                {
                        $match=1;
                        last;
                }
        }
        push @arr3,$arr2[$i] if ($match != 1);
}
print "@arr3";