Not able to read unique values in array

Hi Friends,

I am having some trouble reading into an array. Basically, I am trying to grep for a pattern and extract it's value and store the same into an array. For eg., if my input is:

<L:RECORD>name=faisel farooq,age=21,
company=TCS,project=BT</L:RECORD>
<L:RECORD>name=abc xyz,age=22,company=Infosys Tech,project=AT</L:RECORD>
<L:RECORD>name=uresh kum,
age=20,company=TCS L,project=VGP</L:RECORD>
<L:RECORD>name=maha laxmi,age=23,company=Tata Co,project=JP Morgan and STanly</L:RECORD>

I grep for 'age' and want to extract 21 and store it in the array.
Here's the code I have come up with:

#/bin/perl
$search="Seq";
$seqCount=0;
@seqArray;
$found=0;
open(READ,"op.txt") or die "Can not open file: $!\n";
@array=<READ>;
close READ;
sub checkExist {
        my $a=$_[0];
        for($i=0;$i<=$seqCount;$i++) {
                if($seqArray[$i]==$a) {
#                       $found=1;
                        return 1;
                }
        }
}
foreach $line(@array)
{
        if ($line =~ /$search/)
        {
                $,="\n";
                @sa=split(/([=,])/,$line);
                $count=0;
                print"@sa\n";
                $length=@sa;
                for($i=0;$i<$length;$i++)
                {
                        $found=0;
                        print "Loop $i: $sa[$i]\n";
                        if($sa[$i] =~ /$search/)
                        {
                                if(!checkExist($sa[$i+2]))
                                {
                                        $seqArray[$seqCount]=$sa[$i+2];
                                        print"Bus Txn Seq is: $sa[$i+2]\n";
                                        $seqCount++;
                                }
                        }
                }
        }
}
print "@seqArray\n";

This does not seem to be working, and the program is going into infinite loop.
One thing I noticed is, if i comment out the below if loop:

if($sa[$i] =~ /$search/)
                        {
                           #     if(!checkExist($sa[$i+2]))
                             #   {
                                        $seqArray[$seqCount]=$sa[$i+2];
                                        print"Bus Txn Seq is: $sa[$i+2]\n";
                                        $seqCount++;
                            #    }
                        }

then the contents do get stored in the array, but not uniquely. Means to say, if i have more records having age=21, then 21 gets stored in the array as many times as it is present in the input file. I would like this to be avoided, and no matter how many times age=21 is present, it should be stored just once.
Thanks in advance for helping..

Not going to bother looking into the guts of what you're trying to do. When you say your code is not working and going into an infinite loop, I don't know how that can be. I understand it doesn't work but I don't know how it's getting into an infinite loop.

  • This
#/bin/perl

is not going to invoke the perl interpreter. It should try to run those commands under your shell.

  • Looking at your input file, how will
if ($line =~ /$search/)

ever match? How will it ever get to the heart of the code?

  • This
print "@seqArray\n";

may/may not be what you want but it will be empty anyway.