use List::Util qw( min ); my %grouped_maxes; while () { chomp; my ($hash, $amount, $end) = split /_/; push @{ $grouped_maxes{$hash} }, $amount if defined($end) && $end eq 'max'; } for my $hash (keys %grouped_maxes) { print("$hash: ", min( @{ $grouped_maxes{$hash} } ), "\n"); } __DATA__ 111aaa222ccc324567fed54333221235_1.04 111aaa222ccc324567fed54333221235_1.05_max 111aaa222ccc324567fed54333221235_0.98_min 111aaa222ccc324567fed54333221235_1.02_max 111aaa222ccc324567fed54333221235_0.21 777aaa222ccc324567fed54333221235_1.04 777aaa222ccc324567fed54333221235_1.07_min 777aaa222ccc324567fed54333221235_1.04_max 888aaa222ccc324567fed54333221235_0.21 #### use List::Util qw( min ); my %hashes; my %grouped_maxes; while () { chomp; my ($hash, $amount, $end) = split /_/; ++$hashes{$hash}; push @{ $grouped_maxes{$hash} }, $amount if defined($end) && $end eq 'max'; } for my $hash (keys %hashes) { if (exists($grouped_maxes{$hash})) { my $min_max = min( @{ $grouped_maxes{$hash} } ); print("$hash: $min_max\n"); } else { print("$hash: No max\n"); } } __DATA__ 111aaa222ccc324567fed54333221235_1.04 111aaa222ccc324567fed54333221235_1.05_max 111aaa222ccc324567fed54333221235_0.98_min 111aaa222ccc324567fed54333221235_1.02_max 111aaa222ccc324567fed54333221235_0.21 777aaa222ccc324567fed54333221235_1.04 777aaa222ccc324567fed54333221235_1.07_min 777aaa222ccc324567fed54333221235_1.04_max 888aaa222ccc324567fed54333221235_0.21 #### 777aaa222ccc324567fed54333221235: 1.04 888aaa222ccc324567fed54333221235: No max 111aaa222ccc324567fed54333221235: 1.02