use Data::Dumper; my %result; # Be careful here, I used a hash, not a hashref { local $/ = ""; # make sure the change to $/'s value doesn't leek to the rest of the program while() # Read the lines at the end like a input file { if (/^([a-z][\w.]+)/i) # a line starting by a letter, followed by letters, numbers or a dot { my $alias = $1; my $sid = whoops!; # here some code to find the value of $sid $result{$sid} = $alias unless exists $result{$sid}; # keep the alias unless the sid is already known } } print Dumper \%result; # show result __DATA__ DB1.UK, DB2.UK = ( (ADDRESS = (PROTOCAL = TCP)) (CONNECT_DATA = (SID = db1)) ) DB1.EU, DB2.CH = ( (ADDRESS = (PROTOCAL = TCP)) (CONNECT_DATA = (SID = db1)) ) DB3.UK = ( (ADDRESS = (PROTOCAL = TCP)) (CONNECT_DATA = (SID = db3)) ) DB3.US = ( (ADDRESS = (PROTOCAL = TCP)) (CONNECT_DATA = (SID = db3)) ) DB5.UK.US, DB5.US = ( (ADDRESS = (PROTOCAL = TCP)) (CONNECT_DATA = (SID = db5)) )