Punit Jain,
This is not the optimized code but you can refactor it. This works for the
given scenario, no matter the order of input data.
Hope it helps to some extent.
[code]
my $var = '';
my @args = ();
my %hash;
while (<DATA>) {
chomp;
my ($var,$arg) = split /=/,$_,2;
if($var eq '{') {
@args = (); #Reset if we encounter '{'
}
my @arg1 = split /,/,$arg if defined $arg;
if(scalar @arg1 > scalar @args) {
$hash{$var} = $arg unless($var eq '{' || $var eq '}');
@args = @arg1;
}
}
foreach my $k (sort keys %hash) {
print "$k = $hash{$k}\n";
}
__DATA__
{
test = ("test123");
test = ("test123","abc","xyz");
test = ("test123","abc");
}
{
test1 = ("passfile","pasfile1","user");
test1 = ("passfile");
test1 = ("passfile","pasfile1");
}
{
test2 = ("temp");
test2 = ("temp","temp1");
test2 = ("temp","temp1","username");
}
{
test3 = ("betty","betty1","jack");
test3 = ("betty","betty1");
test3 = ("betty");
}
[/code]
[output]
test = ("test123","abc","xyz");
test1 = ("passfile","pasfile1","user");
test2 = ("temp","temp1","username");
test3 = ("betty","betty1","jack");
[/output]
best,
Shaji
-------------------------------------------------------------------------------
Your talent is God's gift to you. What you do with it is your gift back to God.
-------------------------------------------------------------------------------
________________________________
From: punit jain <[email protected]>
To: "[email protected]" <[email protected]>
Sent: Tuesday, 8 January 2013 5:58 PM
Subject: Regex help needed
Hi ,
I have a file as below : -
{
test = ("test123");
test = ("test123","abc");
test = ("test123","abc","xyz");
}
{
test1 = ("passfile");
test1 = ("passfile","pasfile1");
test1 = ("passfile","pasfile1","user");
}
and so on ....
The requirement is to have the file parsing so that final output is :-
test = ("test123","abc","xyz");
test1 = ("passfile","pasfile1","user");
So basically only pick the lines with maximum number of options for each
type.
Regards.