#!/usr/bin/perl use strict; my $inFile = q{animals.txt}; open my $INFILE, q{<}, $inFile or die; my %hash; my %hash_2; while (<$INFILE>) { $_ =~ s/\s+$//; if (/^forest(\s+)(\w+)/){ my $country = $2; while (<$INFILE>){ chomp($_); if (/^-fox.*(\d\d\d\d)/){ chomp; my $fox_hour = $1; if (defined $hash{$country}){ $hash_2{$country}=$fox_hour; } else{ $hash{$country}=$fox_hour; } } last if (/monkey/); } } } close $INFILE; my $c_outfile = q{countries_results.csv}; open my $c_OUTFILE, q{>}, $c_outfile or die; my $c_inFile = q{countries_list.csv}; open my $c_INFILE, q{<}, $c_inFile or die; my $country; while (my $line = <$c_INFILE>){ my @Elements = split(";",$line); $country = $Elements[0]; $country =~ s/\s+$//; print $c_OUTFILE "$country;$hash{$country};$hash_2{$country}\n"; } close $c_INFILE; close $c_OUTFILE;