Hi Monks,
I am new to perl so trying out number of problems from all over. Here is problem where I can seek some wisdom
I have 2 files with following data
FILE 1:
NAME,STRIDE_LENGTH,STANCE
Euoplocephalus,1.87,quadrupedal
Stegosaurus,1.90,quadrupedal
Tyrannosaurus Rex,5.76,bipedal
Hadrosaurus,1.4,bipedal
Deinonychus,1.21,bipedal
Struthiomimus,1.34,bipedal
Velociraptor,2.72,bipedal
FILE 2:
NAME,LEG_LENGTH,DIET
Hadrosaurus,1.2,herbivore
Struthiomimus,0.92,omnivore
Velociraptor,1.0,carnivore
Triceratops,0.87,herbivore
Euoplocephalus,1.6,herbivore
Stegosaurus,1.40,herbivore
Tyrannosaurus Rex,2.5,carnivore
Question
# List all herbivores dinos
# List their name and leg_length sorted
I am able to find the dino's that are herbivores but I am unsure how to sort the result based on the length of leg.
Is is code right way to solve this problem ?
use Data::Dumper;
open my $fh1, '<', '1.csv' or die $!;
open my $fh2, '<', '2.csv' or die $!;
my $header1 = <$fh1>;
my $header2 = <$fh2>;
my %app_map;
while(my $row = <$fh1>){
my ($name, $leg_len, $diet) = split /\,/, $row;
if($leg_len ne ''){
$app_map{$name}{leg_len}=$leg_len;
}else{
$app_map{$name}{leg_len}="NA";
}
if ($diet ne '') {
$app_map{$name}{diet}=$diet;
} else {$app_map{$name}{diet}="NA"; }
}
close $fh1 or die $!;
while(my $row = <$fh2>){
my ($name, $str_len, $stance) = split /\,/, $row;
if($str_len ne ''){
$app_map{$name}{str_len}=$str_len;
}else {$app_map{$name}{str_len}="NA";}
if($stance ne ''){
$app_map{$name}{stance}=$stance;
}else {$app_map{$name}{stance}="NA";}
}
close $fh2 or die $!;
while ( my ($k, $v) = each %app_map ) {
if(defined($app_map{$k}{diet}) && $app_map{$k}{diet} =~/herbivo
+re/){
####SORT BY STR_LEN#####################
#print $k." - ".$app_map{$k}{str_len},"\n";
}
}
#print Dumper \%app_map;