my @relation_Arr; my @TOPArr = qw(TOP01 TOP02 TOP03); my @MIDArr = qw(SVM01 SVM02 SVM03 SVM04 SVM05 SVM06 SVM07 SVM08 SVM09 SVM10 SVM11 SVM12 SVM13 SVM14 SVM15 SVM16 SVM17 SVM18 SVM19 SVM20 SVM21 SVM22 SVM23 SVM24); my @LOWArr = qw(LOW01:19000 LOW02:19523 LOW03:13456 LOW04:18992 LOW05:19560 LOW06:18945 LOW07:19855 LOW08:12344 LOW09:17400 LOW10:18990 LOW11:14566 LOW12:17896 LOW13:19000 LOW14:19523 LOW15:13456 LOW16:18992 LOW17:19560 LOW18:18945 LOW19:19855 LOW20:12344 LOW21:17400 LOW22:16897 LOW23:19877 LOW24:18676 LOW25:13457 LOW26:12345 LOW27:18456); my $tottopsum = 0; my $totmidsum = 0; while($#LOWArr >= 0) { if($tottopsum == 0) { $lowdevName = (split /\:/, $LOWArr[0])[0]; $devCnt = (split /\:/, $LOWArr[0])[1]; $tottopsum = $tottopsum + $devCnt; $totmidsum = $totmidsum + $devCnt; push(@relation_Arr, "$TOPArr[0]:$tottopsum, $MIDArr[0]:$totmidsum, $LOWArr[0]\n"); splice(@LOWArr, 0, 1); next; } for ($index = 0; $index <= $#LOWArr; $index++) { $lowdevName = (split /\:/, $LOWArr[$index])[0]; $devCnt = (split /\:/, $LOWArr[$index])[1]; if (($tottopsum + $devCnt) < 366667) { if (($totmidsum + $devCnt) < 36667) { $tottopsum = $tottopsum + $devCnt; $totmidsum = $totmidsum + $devCnt; push(@relation_Arr, "$TOPArr[0]:$tottopsum, $MIDArr[0]:$totmidsum, $LOWArr[$index]\n"); splice(@LOWArr, $index, 1); ### Remove an element of the array $index = $index - 1; } else { next; } } else { splice(@TOPArr, 0, 1); ### Remove an element of the array splice(@MIDArr, 0, 1); ### Remove an element of the array $tottopsum = 0; $totmidsum = 0; $tottopsum = $tottopsum + $devCnt; $totmidsum = $totmidsum + $devCnt; push(@relation_Arr, "$TOPArr[0]:$tottopsum, $MIDArr[0]:$totmidsum, $LOWArr[$index]\n"); splice(@LOWArr, $index, 1); ### Remove an element of the array $index = $index - 1; last; } } splice(@MIDArr, 0, 1); ### Remove an element of the array $totmidsum = 0; } print "@relation_Arr";