A
alwaysonnet
hi all -- Help needed
I'm trying to sort an array of hash-references based on the "A/C No" by
preserving the "Type" order,'ZCurrency' and 'Status' , where "A/C No"
is unique for every record.
Important Note is that - if Records having same "Type","Status" and
"ZCurrency" have different "A/C No" so sorting them with "A/C No"
doesn't effect the actual order of display.
1) I'm looping through every element, checking current record if
"Type","Status" and "ZCurrency" is equal to previous record. if not,
pushing it to final array.
2) if current record matches with previous record, i'm pushing to
another array and sorting them with the "A/C No". Because, both the
records will have same "Type","Status" and "ZCurrency".
3) By this, I got two arrays @final and @curr ... need to merge them
into one by sorting with "A/C No" and preserving the order with
original but sort with "A/C No" if the records have same
"Type","Status" and "ZCurrency".
Partially working code ---
#!/usr/bin/perl
my %type_order = qw/Prefund 1 Receipt 2 Payment 3/;
my @records = (
{'Type'=>'Prefund' , 'A/C No'=>12345 , 'Status'=>'Y',
'ZCurrency' =>'GBP'},
{'Type'=>'Prefund' , 'A/C No'=>45678 , 'Status'=>'Y',
'ZCurrency' =>'SEK'},
{'Type'=>'Prefund' , 'A/C No'=>33333 , 'Status'=>'Y',
'ZCurrency' =>'SEK'},
{'Type'=>'Prefund' , 'A/C No'=>32222 , 'Status'=>'N',
'ZCurrency' =>'SEK'},
{'Type'=>'Receipt' , 'A/C No'=>32365 , 'Status'=>'Y',
'ZCurrency' =>'EUR'},
{'Type'=>'Receipt' , 'A/C No'=>78878 , 'Status'=>'N',
'ZCurrency' =>'AIR'},
{'Type'=>'Receipt' , 'A/C No'=>32435 , 'Status'=>'N',
'ZCurrency' =>'AIR'},
{'Type'=>'Receipt' , 'A/C No'=>64237 , 'Status'=>'N',
'ZCurrency' =>'GBP'},
{'Type'=>'Payment' , 'A/C No'=>22476 , 'Status'=>'Y',
'ZCurrency' =>'AUS'},
{'Type'=>'Payment' , 'A/C No'=>22447 , 'Status'=>'Y',
'ZCurrency' =>'BEL'},
{'Type'=>'Payment' , 'A/C No'=>56546 , 'Status'=>'N',
'ZCurrency' =>'EUR'},
{'Type'=>'Payment' , 'A/C No'=>44444 , 'Status'=>'N',
'ZCurrency' =>'EUR'},
{'Type'=>'Payment' , 'A/C No'=>43434 , 'Status'=>'N',
'ZCurrency' =>'EUR'},
);
foreach $current (@records) {
if ($previous->{'Type'} eq $current->{'Type'} && $previous->{'Status'}
eq $current->{'Status'} && $previous->{'ZCurrency'} eq
$current->{'ZCurrency'}) {
push(@curr,$previous);
push(@curr,$current);
} else {
push(@final,$current);
}
$previous = $current;
}
%seen = ();
foreach $item (@curr) {
push(@uniq, $item) unless $seen{$item}++;
}
@uniq = sort { $type_order{$a->{'Type'}} <=> $type_order{$b->{'Type'}}
||
$a->{'A/C No'} <=> $b->{'A/C No'}
} @uniq;
foreach $x(@uniq) {
print " $x->{'Type'} $x->{'A/C No'} $x->{'Status'}
$x->{'ZCurrency'}\n";
}
I want my final output as
{'Type'=>'Prefund' , 'A/C No'=>12345 , 'Status'=>'Y', 'ZCurrency'
=>'GBP'},
{'Type'=>'Prefund' , 'A/C No'=>33333 , 'Status'=>'Y', 'ZCurrency'
=>'SEK'},
{'Type'=>'Prefund' , 'A/C No'=>45678 , 'Status'=>'Y', 'ZCurrency'
=>'SEK'},
{'Type'=>'Prefund' , 'A/C No'=>32222 , 'Status'=>'N', 'ZCurrency'
=>'SEK'},
{'Type'=>'Receipt' , 'A/C No'=>32365 , 'Status'=>'Y', 'ZCurrency'
=>'EUR'},
{'Type'=>'Receipt' , 'A/C No'=>32435 , 'Status'=>'N', 'ZCurrency'
=>'AIR'},
{'Type'=>'Receipt' , 'A/C No'=>78878 , 'Status'=>'N', 'ZCurrency'
=>'AIR'},
{'Type'=>'Receipt' , 'A/C No'=>64237 , 'Status'=>'N', 'ZCurrency'
=>'GBP'},
{'Type'=>'Payment' , 'A/C No'=>22476 , 'Status'=>'Y', 'ZCurrency'
=>'AUS'},
{'Type'=>'Payment' , 'A/C No'=>22447 , 'Status'=>'Y', 'ZCurrency'
=>'BEL'},
{'Type'=>'Payment' , 'A/C No'=>43434 , 'Status'=>'N', 'ZCurrency'
=>'EUR'},
{'Type'=>'Payment' , 'A/C No'=>44444 , 'Status'=>'N', 'ZCurrency'
=>'EUR'},
{'Type'=>'Payment' , 'A/C No'=>56546 , 'Status'=>'N', 'ZCurrency'
=>'EUR'},
So records to be effected are
{'Type'=>'Prefund' , 'A/C No'=>45678 , 'Status'=>'Y', 'ZCurrency'
=>'SEK'},
{'Type'=>'Prefund' , 'A/C No'=>33333 , 'Status'=>'Y', 'ZCurrency'
=>'SEK'},
{'Type'=>'Receipt' , 'A/C No'=>78878 , 'Status'=>'N', 'ZCurrency'
=>'AIR'},
{'Type'=>'Receipt' , 'A/C No'=>32435 , 'Status'=>'N', 'ZCurrency'
=>'AIR'},
{'Type'=>'Payment' , 'A/C No'=>56546 , 'Status'=>'N', 'ZCurrency'
=>'EUR'},
{'Type'=>'Payment' , 'A/C No'=>44444 , 'Status'=>'N', 'ZCurrency'
=>'EUR'},
{'Type'=>'Payment' , 'A/C No'=>43434 , 'Status'=>'N', 'ZCurrency'
=>'EUR'},
Thanks,
Raj
I'm trying to sort an array of hash-references based on the "A/C No" by
preserving the "Type" order,'ZCurrency' and 'Status' , where "A/C No"
is unique for every record.
Important Note is that - if Records having same "Type","Status" and
"ZCurrency" have different "A/C No" so sorting them with "A/C No"
doesn't effect the actual order of display.
1) I'm looping through every element, checking current record if
"Type","Status" and "ZCurrency" is equal to previous record. if not,
pushing it to final array.
2) if current record matches with previous record, i'm pushing to
another array and sorting them with the "A/C No". Because, both the
records will have same "Type","Status" and "ZCurrency".
3) By this, I got two arrays @final and @curr ... need to merge them
into one by sorting with "A/C No" and preserving the order with
original but sort with "A/C No" if the records have same
"Type","Status" and "ZCurrency".
Partially working code ---
#!/usr/bin/perl
my %type_order = qw/Prefund 1 Receipt 2 Payment 3/;
my @records = (
{'Type'=>'Prefund' , 'A/C No'=>12345 , 'Status'=>'Y',
'ZCurrency' =>'GBP'},
{'Type'=>'Prefund' , 'A/C No'=>45678 , 'Status'=>'Y',
'ZCurrency' =>'SEK'},
{'Type'=>'Prefund' , 'A/C No'=>33333 , 'Status'=>'Y',
'ZCurrency' =>'SEK'},
{'Type'=>'Prefund' , 'A/C No'=>32222 , 'Status'=>'N',
'ZCurrency' =>'SEK'},
{'Type'=>'Receipt' , 'A/C No'=>32365 , 'Status'=>'Y',
'ZCurrency' =>'EUR'},
{'Type'=>'Receipt' , 'A/C No'=>78878 , 'Status'=>'N',
'ZCurrency' =>'AIR'},
{'Type'=>'Receipt' , 'A/C No'=>32435 , 'Status'=>'N',
'ZCurrency' =>'AIR'},
{'Type'=>'Receipt' , 'A/C No'=>64237 , 'Status'=>'N',
'ZCurrency' =>'GBP'},
{'Type'=>'Payment' , 'A/C No'=>22476 , 'Status'=>'Y',
'ZCurrency' =>'AUS'},
{'Type'=>'Payment' , 'A/C No'=>22447 , 'Status'=>'Y',
'ZCurrency' =>'BEL'},
{'Type'=>'Payment' , 'A/C No'=>56546 , 'Status'=>'N',
'ZCurrency' =>'EUR'},
{'Type'=>'Payment' , 'A/C No'=>44444 , 'Status'=>'N',
'ZCurrency' =>'EUR'},
{'Type'=>'Payment' , 'A/C No'=>43434 , 'Status'=>'N',
'ZCurrency' =>'EUR'},
);
foreach $current (@records) {
if ($previous->{'Type'} eq $current->{'Type'} && $previous->{'Status'}
eq $current->{'Status'} && $previous->{'ZCurrency'} eq
$current->{'ZCurrency'}) {
push(@curr,$previous);
push(@curr,$current);
} else {
push(@final,$current);
}
$previous = $current;
}
%seen = ();
foreach $item (@curr) {
push(@uniq, $item) unless $seen{$item}++;
}
@uniq = sort { $type_order{$a->{'Type'}} <=> $type_order{$b->{'Type'}}
||
$a->{'A/C No'} <=> $b->{'A/C No'}
} @uniq;
foreach $x(@uniq) {
print " $x->{'Type'} $x->{'A/C No'} $x->{'Status'}
$x->{'ZCurrency'}\n";
}
I want my final output as
{'Type'=>'Prefund' , 'A/C No'=>12345 , 'Status'=>'Y', 'ZCurrency'
=>'GBP'},
{'Type'=>'Prefund' , 'A/C No'=>33333 , 'Status'=>'Y', 'ZCurrency'
=>'SEK'},
{'Type'=>'Prefund' , 'A/C No'=>45678 , 'Status'=>'Y', 'ZCurrency'
=>'SEK'},
{'Type'=>'Prefund' , 'A/C No'=>32222 , 'Status'=>'N', 'ZCurrency'
=>'SEK'},
{'Type'=>'Receipt' , 'A/C No'=>32365 , 'Status'=>'Y', 'ZCurrency'
=>'EUR'},
{'Type'=>'Receipt' , 'A/C No'=>32435 , 'Status'=>'N', 'ZCurrency'
=>'AIR'},
{'Type'=>'Receipt' , 'A/C No'=>78878 , 'Status'=>'N', 'ZCurrency'
=>'AIR'},
{'Type'=>'Receipt' , 'A/C No'=>64237 , 'Status'=>'N', 'ZCurrency'
=>'GBP'},
{'Type'=>'Payment' , 'A/C No'=>22476 , 'Status'=>'Y', 'ZCurrency'
=>'AUS'},
{'Type'=>'Payment' , 'A/C No'=>22447 , 'Status'=>'Y', 'ZCurrency'
=>'BEL'},
{'Type'=>'Payment' , 'A/C No'=>43434 , 'Status'=>'N', 'ZCurrency'
=>'EUR'},
{'Type'=>'Payment' , 'A/C No'=>44444 , 'Status'=>'N', 'ZCurrency'
=>'EUR'},
{'Type'=>'Payment' , 'A/C No'=>56546 , 'Status'=>'N', 'ZCurrency'
=>'EUR'},
So records to be effected are
{'Type'=>'Prefund' , 'A/C No'=>45678 , 'Status'=>'Y', 'ZCurrency'
=>'SEK'},
{'Type'=>'Prefund' , 'A/C No'=>33333 , 'Status'=>'Y', 'ZCurrency'
=>'SEK'},
{'Type'=>'Receipt' , 'A/C No'=>78878 , 'Status'=>'N', 'ZCurrency'
=>'AIR'},
{'Type'=>'Receipt' , 'A/C No'=>32435 , 'Status'=>'N', 'ZCurrency'
=>'AIR'},
{'Type'=>'Payment' , 'A/C No'=>56546 , 'Status'=>'N', 'ZCurrency'
=>'EUR'},
{'Type'=>'Payment' , 'A/C No'=>44444 , 'Status'=>'N', 'ZCurrency'
=>'EUR'},
{'Type'=>'Payment' , 'A/C No'=>43434 , 'Status'=>'N', 'ZCurrency'
=>'EUR'},
Thanks,
Raj