#!/usr/bin/perl use strict; use warnings; my $ifile = "medica.csv"; my $ofile = "med2.csv"; my $rules_file = "med_rules_l.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})){ $med = $rkey; last; } } $meds{$med} = 1; $patients{$gay_id}{$med} = 1; } } } } 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;