Hi,
I have an array with following data. First field shows the owner and second is unique name. Now i have to pic the latest value with respect to the date in case of duplicate.
like "def" is from two owners "rahul/vineet", now i want the latest from the two and the owner name also for all the fields
ARRAY:
rahul_abc_Linux_Nov_02
rahul_def_Linux_Nov_02
vineet_cde_Linux_Dec_05
vineet_def_Linux_Dec_01
vineet_kbc_Linux_Dec_06
expected result
ARRAY:
rahul_abc_Linux_Nov_02
vineet_cde_Linux_Dec_05
vineet_def_Linux_Dec_01
vineet_kbc_Linux_Dec_06
Hi,
Try this below code,,
#! /usr/local/bin/perl
use strict;
use Tie::IxHash;
my @mArr = qw/rahul_abc_Linux_Nov_02 rahul_def_Linux_Nov_02 vineet_cde_Linux_Dec_05 vineet_def_Linux_Dec_01 vineet_kbc_Linux_Dec_
06/;
my %mMonth=("Jan"=>"1","Feb"=>"2","Mar"=>"3","Apr"=>"4","May"=>"5","Jun"=>"6","Jul"=>"7","Aug"=>"8","Sep"=>"9","Oct"=>"10","Nov"=
>"11","Dec"=>"12");
my %mHash = ();
tie(%mHash,"Tie::IxHash");
foreach my $mEle ( @mArr )
{
my @tArr = split(/\_/,$mEle);
my $mKey1 = $tArr[1];
if ( defined $mHash{$mKey1} )
{
my $val = $mHash{$mKey1};
my @Arr = split(/\_/,$val);
my $mExistMonth = $Arr[3];
my $mNewMonth = $tArr[3];
if ( $mMonth{$mExistMonth} < $mMonth{$mNewMonth} )
{
$mHash{$mKey1}="$mEle";
}
elsif ( ( $mMonth{$mExistMonth} == $mMonth{$mNewMonth} ) && $tArr[4] > $Arr[4] )
{
$mHash{$mKey1}="$mEle";
}
}
else
{
$mHash{$mKey1}="$mEle";
}
}
print "The Output is\n################\n\n";
foreach my $mKey1 ( keys %mHash )
{
print "$mHash{$mKey1}\n";
}
Cheers,
Ranga:)
I think i am using an older version. it gives this error. any other way?
"Can't locate Tie/IxHash.pm "
Another way:
# awk -F\_ '{a[$2""$3]=b++" "$0}END{for (i in a) {print a}}' infile|sort|awk '{print $2}'
rahul_abc_Linux_Nov_02
vineet_cde_Linux_Dec_05
vineet_def_Linux_Dec_01
vineet_kbc_Linux_Dec_06
hi Klashxx, I want it in perl.
Your array is oderer by date ?
No, we have to extract the date also from the string.
rangarasan:
Hi,
Try this below code,,
#! /usr/local/bin/perl
use strict;
#use Tie::IxHash;
my @mArr = qw/rahul_abc_Linux_Nov_02 rahul_def_Linux_Nov_02 vineet_cde_Linux_Dec_05 vineet_def_Linux_Dec_01 vineet_kbc_Linux_Dec_
06/;
my %mMonth=("Jan"=>"1","Feb"=>"2","Mar"=>"3","Apr"=>"4","May"=>"5","Jun"=>"6","Jul"=>"7","Aug"=>"8","Sep"=>"9","Oct"=>"10","Nov"=
>"11","Dec"=>"12");
my %mHash = ();
#tie(%mHash,"Tie::IxHash");
foreach my $mEle ( @mArr )
{
my @tArr = split(/\_/,$mEle);
my $mKey1 = $tArr[1];
if ( defined $mHash{$mKey1} )
{
my $val = $mHash{$mKey1};
my @Arr = split(/\_/,$val);
my $mExistMonth = $Arr[3];
my $mNewMonth = $tArr[3];
if ( $mMonth{$mExistMonth} < $mMonth{$mNewMonth} )
{
$mHash{$mKey1}="$mEle";
}
elsif ( ( $mMonth{$mExistMonth} == $mMonth{$mNewMonth} ) && $tArr[4] > $Arr[4] )
{
$mHash{$mKey1}="$mEle";
}
}
else
{
$mHash{$mKey1}="$mEle";
}
}
print "The Output is\n################\n\n";
foreach my $mKey1 ( keys %mHash )
{
print "$mHash{$mKey1}\n";
}
Cheers,
Ranga:)
you dont have Tie::IxHash module in system. so that you got error.
Please try with the above code..
Tie::IxHash - check this module in CPAN for detailed explanation.
Cheers,
Ranga:)
1 Like
Hi Ranga,
The mArr array which i have has some NULL values also. How can they be removed?
---------- Post updated at 01:12 PM ---------- Previous update was at 01:05 PM ----------
Thanks Ranga, its done.
Hi,
You want to use splice funtion to remove the array elements( you can able to add too).
for(my $i=0;$i<scalar(@mArr);$i++)
{
splice(@mArr,$i,0) if ($mArr[$i] =~ /^$/ );
}
splice - remove array element any where in the position.
Cheers,
Ranga:)