#!/usr/bin/perl use strict; use warnings; use Data::Dump qw(dump); use Mail::Sender; use File::Basename qw(basename); sub shit_done { my @adv = @_; my $sender = new Mail::Sender {smtp => 'localhost', from => "$ENV{'USER'}\@detritus.fundacioace.com"}; $sender->MailMsg({to => "$ENV{'USER'}\@detritus.fundacioace.com", subject => 'Script terminado', msg => "$adv[0] ha terminado la ejecucion!\n\nRevise $adv[1]\n"}); } my $ifile = "medica.csv"; my $ofile = "med3.csv"; my $rules_file = "med_rules_ed_0219.txt"; my %meds; my %patients; my %wrules; # Leyendo las reglas open RDF, "<$rules_file" or die "No rules file"; while (){ (my $rkey, my $rvalue) = /^(\w*): (.*)$/; chomp $rvalue; my @lpat = split /\|/, $rvalue; %{$wrules{$rkey}} = map {$_ => 1} @lpat unless !$rkey; } close RDF; # Leyendo datos y comparando open IDF, "<$ifile" or die "No such file"; while () { (my $gay_id, my $ldata) = /^.*;(\d*)\.0;(.*)$/; if ($ldata){ my @list = split /, /, $ldata; foreach my $med (@list){ if ($med) { $med =~ s/[0-9]+.*$//; # aqui compruebo cada regla contra la palabra que he leido foreach my $rkey (sort keys %wrules){ if (exists($wrules{$rkey}{$med})){ $meds{$rkey} = 1; $patients{$gay_id}{$rkey} = 1; last; } } } } } } close IDF; open ODF, ">$ofile" or die "Could not open file!!!"; print ODF "Interno"; foreach my $med (sort keys %meds){ print ODF ",$med"; } print ODF "\n"; foreach my $patient (sort keys %patients){ print ODF "$patient"; foreach my $med (sort keys %meds){ if(exists($patients{$patient}{$med})){ print ODF ",1"; }else{ print ODF ",0"; } } print ODF "\n"; } close ODF; shit_done basename($ENV{_}), $ofile;