Hi,
I need a perl function that will check the following in a file, modify if only the first two columns match , and if not found in the file, insert the values.
The file is like below
lookup.txt
InstanceName1::DomainName1::Server1
InstanceName1::DomainName2::Server2
InstanceName2::DomainName3::Server3
InstanceName2::DomainName4::Server3
...
...
The perl function needs to match the instance name, domain name, and check if the servername is the same , else replace the server name, and after iterating through the file, if the string is not found, insert the same into the file. The stub function i have written explains the same.
sub functionname()
{
my $lookupfile = "lookupfile";
my($var1,$var2,$var3)=@_;
open(MAPPING,"<$lookupfile") || die $!;
while($key=<MAPPING>)
{
chomp $key;
my @keyarray =split(/::/,$key);
$cusName= $keyarray[0];
$apmName= $keyarray[1];
$host= $keyarray[2];
$skey= $cusName."::".$apmName;
$sskey= $var1."::".$ipName;
if ( ! -z $cusName && ! -z $apmName )
{
if ( $cusName eq $var1 )
{
if ( $skey eq $sskey )
{
if ( $host eq $var3 )
{
print "present";
}
else
{
print $cusName."::".$apmName."::".$host;
print $var1."::".$var2."::".$var3;
## replace first with second
$presentflag=1;
}
}
}
}
}
if ( $presentflag == 0 )
{
# insert into the file $var1,$var2,$var3 as $var1::$var2::$var3
}
close(MAPPING);
}