Requirements:
=============
There is an array containing unknown number of elements. Among these elements, one could be 'aaa' and the other 'bbb'. The rest elements are less significant.
If this array does contain 'aaa' and/or 'bbb', a new new array '@r' will be created whose last element is 'aaa' and the 2nd to the last is 'bbb'.
The following is my implementation:
And here is the output:
As you can see, the output is correct. But the implementation looks dumb. Could someone polish my code, please?
Many thanks!!
=============
There is an array containing unknown number of elements. Among these elements, one could be 'aaa' and the other 'bbb'. The rest elements are less significant.
If this array does contain 'aaa' and/or 'bbb', a new new array '@r' will be created whose last element is 'aaa' and the 2nd to the last is 'bbb'.
The following is my implementation:
Code:
use strict;
my @o = ('asd','wer','111','a1a2','gergh','aaa','111awre','cxvb','rewtrh','bbb','fsdgfsg','rtut');
print "\$#o = $#o\n";
my (@tmp, @n);
foreach my $e (reverse @o) {
if ($e eq 'bbb') {
push @tmp, $e;
}
else {
unshift @tmp, $e;
}
}
foreach my $e (reverse @tmp) {
if ($e eq 'aaa') {
push @n, $e;
}
else {
unshift @n, $e;
}
}
my $o = join ',', @o;
my $r = join ',', @n;
warn "#$o#\n#$r#\n";
And here is the output:
Code:
$#o = 11
#asd,wer,111,a1a2,gergh,aaa,111awre,cxvb,rewtrh,bbb,fsdgfsg,rtut#
#asd,wer,111,a1a2,gergh,111awre,cxvb,rewtrh,fsdgfsg,rtut,bbb,aaa#
As you can see, the output is correct. But the implementation looks dumb. Could someone polish my code, please?
Many thanks!!