my %ItemUID = (); my %ItemName = (); #Get ItemUID and Item Name (ItemUID is unique to each Test.IData) { my $sql = "SELECT t.IData FROM Tests AS t JOIN Assignments AS a ON a.A +ssignmentTestID=t.ID JOIN Users AS u ON u.ID=t.UserID WHERE u.Institu +tionID='004452' AND t.IData REGEXP 'ch01.03.MultipleChoice 049';"; my $sth = $dbh->prepare($sql); $sth->execute(); while ( my @row = $sth->fetchrow_array() ) { my $idata = $row[0]; while ( $idata =~ /]+)/gi ) { my $elements = $1; my $src; my $uid; if ( $elements =~ /src=\"\w+\/([^"]+)/i ) { $src = $1 } if ( $elements =~ /uid=\"([^"]+)/i ) { $uid = $1 } if ( not exists $ItemUID{$uid} ) { $ItemUID{$uid} = $src; push(@{$ItemName{$src}}, $uid); } } } $sth->finish(); } # you can generate an array of the unique names if you like my @names = sort keys %ItemNames;