Perl: Data Filtering (Array, Hash, Keys/Values) - by Eun Bae Kim (08/03/2018)
 

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
  
use strict;

# Filtering an array
my @aNumbers = (2,3,4,2,6,4,6,7,6,8,8,0,8,7,6,7,8,9,8,7,6,6,7,8,9);

my $iCntEvenNumber   = 0;                           # Variable for counting even numbers
my $iDataCnt         = 0;

print "--------------------------------------------\n";
foreach my $iCurVal (@aNumbers) {
	$iDataCnt++;
	my $sDataCnt = sprintf("%02d", $iDataCnt);
	if ($iCurVal % 2 == 0) {
		$iCntEvenNumber++;
		print $sDataCnt.": ".$iCurVal." - Even Number (".$iCntEvenNumber.")\n";
	} else {
		print $sDataCnt.": ".$iCurVal." - Odd Number\n";
	}
}

$iCntEvenNumber = 0;
$iDataCnt = 0;
print "--------------------------------------------\n";
for (my $i=0; $i<10;$i++) {
	$iDataCnt++;
	my $iCurVal = $aNumbers[$i];
	my $sDataCnt = sprintf("%02d", $iDataCnt);
	if ($iCurVal % 2 == 0) {
		$iCntEvenNumber++;
		print $sDataCnt.": ".$iCurVal." - Even Number (".$iCntEvenNumber.")\n";
	} else {
		print $sDataCnt.": ".$iCurVal." - Odd Number\n";
	}
}



# Unique Array Elements
my %hNum_by_Num = ();
foreach my $iCurVal (@aNumbers) {
	$hNum_by_Num{$iCurVal}=$iCurVal;
}
my @aNum = keys %hNum_by_Num;                       # Return keys in a array
@aNum = sort {$a <=> $b} @aNum;                     # Sorting numerically (Ascending)
print "--------------------------------------------\n";
print "Direct Processing..........\n";
foreach my $iCurVal (@aNum) {
	print $iCurVal."\n";
}
my $iUniqueNumCnt = ($#aNum + 1);
print "No of Unique Numbers: ".$iUniqueNumCnt."\n";
print "---------------------------------\n";
print "Processing by a Function.........\n";
foreach my $iCurVal (funcUniqueArray(@aNumbers)) {
	print $iCurVal."\n";
}

sub funcUniqueArray {
	my @aNumbers = @_;
	my %hNum_by_Num = ();
	foreach my $iCurVal (@aNumbers) {
		$hNum_by_Num{$iCurVal}=$iCurVal;
	}
	my @aNum = keys %hNum_by_Num;                       # Return keys in a array
	@aNum = sort {$a <=> $b} @aNum;                     # Sorting numerically (Ascending)

	return @aNum;
}


# Filtering a hash
my %hSeq_by_Name = ();
$hSeq_by_Name{'EBKim'}  = "GCAAATGCTGACTGATGCTAGCTTATACTAGTAGTATCGAT";
$hSeq_by_Name{'JBPark'} = "GCAGATGCTGACTGATGCTAGCTTATCCTAGTAGTATCGAT";
$hSeq_by_Name{'SJKim'}  = "GCACATGCTGACTGATGCTAGCTTATACTAGTAGTATCGAT";
$hSeq_by_Name{'IHYoo'}  = "GCAGATGCTGACTGATGCTAGCTTATCCTAGTAGTATCGAT";
$hSeq_by_Name{'SJChoi'} = "GCAAATGCTGACTGATGCTAGCTTATCCTAGTAGTATCGAT";
$hSeq_by_Name{'ISKim'}  = "GCAGATGCTGACTGATGCTAGCTTATACTAGTAGTATCGAT";
$hSeq_by_Name{'HSKim'}  = "GCACATGCTGACTGATGCTAGCTTATCCTAGTAGTATCGAT";
$hSeq_by_Name{'YHChoi'} = "GCAGATGCTGACTGATGCTAGCTTATCCTAGTAGTATCGAT";
$hSeq_by_Name{'STLee'}  = "GCAAATGCTGACTGATGCTAGCTTATACTAGTAGTATCGAT";

my @aNames = keys(%hSeq_by_Name);
print "--------------------------------------------\n";
foreach my $sCurKey (@aNames) {                      # key vs value
	my $sCurVal = $hSeq_by_Name{$sCurKey};

	if ($sCurVal=~/CTAGCTTATCCTAGTAGTA/) {           # Filtering values and printing keys
		print $sCurKey."\t".$sCurVal."\n";
	}
}
print "---------------------------------\n";
foreach my $sCurKey (sort(@aNames)) {                # Sorting lexically
	if ($sCurKey=~/Kim/) {                           # Filtering keys and printing values
		my $sCurVal = $hSeq_by_Name{$sCurKey};
		print $sCurKey."\t".$sCurVal."\n";
	}
}

# For more information, refer to the following link
# https://www.tutorialspoint.com/perl