SayWhat?! has asked for the wisdom of the Perl Monks concerning the following question:
Hello wise ones!
I'm currently trying to compare my hash's keys and values by means of a regex. Here is a sample of my input (a tab separated two column text file:
vriendelik aardig polisieman agent net-net amper gedierte beest naak bloot homoseksueel flikker bronstig geil menskop hoofd toedraai inpakken dierekop kop onskuldig onnozel perdeteler paardenfokker dennebol pijnappel
as well as my code thus far:
#!/usr/bin/perl-w use strict; use warnings; use open ':utf8'; use autodie; open NONMATCHINPUT, "<OutputNonMatchedWords.txt"; open OIC, ">OutputIdenticalCognates.txt"; open ONIC, ">OutputNonIdenticalCognates.txt"; open ONC, ">OutputNonCognates.txt"; my %nonmatchhash; while (my $line = <NONMATCHINPUT>) { chomp $line; #split the line on tab my ($nonmatchhashkeys, $nonmatchhashvalues) = split /\t/, $line; $nonmatchhash{$nonmatchhashkeys} = $nonmatchhashvalues; #if the values of the hash are exactly the same as the keys of the + hash if ($nonmatchhash{$nonmatchhashkeys} = $nonmatchhash{$nonmatchhash +values}) { #print both key and value to OutputIdenticalCognates.txt, sepa +rated by a tab print OIC "$nonmatchhashkeys\t$nonmatchhashvalues\n"; } #assign each key in the hash to $AfrColumn1token foreach my $AfrColumn1token(keys %nonmatchhash) { #if the Afrikaans word ($AfrColumn1token) contains: anything, +followed by 'agtig', followed by 'e' or 'er' or 'ste' (optional), at +the end of the string if ($AfrColumn1token =~ /(.*)(agtig)(e|er|ste)?$/) { #then, by using a foreach, assign each value in the hash t +o $DutColumn2token foreach my $DutColumn2token (values %nonmatchhash) { #And then, if the Dutch word ($DutColumn2token) contai +ns: anything, followed by 'achtig', followed by 'e' or 'er' or 'ste' +(optional), at the end of the string if ($DutColumn2token =~ /(.*)(achtig)(e|er|ste)?$/) { #print it to OutputNonIdenticalCognates.txt print ONIC "$AfrColumn1token\t$DutColumn2token\n"; } } } else { #else, print it to OutputNonCognates.txt print ONC "$AfrColumn1token\t$DutColumn2token\n"; } } }
I want to check if the hash's key consists of that which I entered into the regex. If that is true, I want to do a similar check with the hash's values - again with a regex.
To explain:
if $AfrColumn1token consists of: anything, followed by 'agtig', followed by 'e' or 'er' or 'ste' (optio +nal), at the end of the string, #then check to see if DutColumn2token consists of: anything, followed by 'ac +htig', followed by 'e' or 'er' or 'ste' (optional), at the end of the + string. #then that particular key and value must be written to OutputNonIdenticalCog +nates.txt, else write the pair to OutputNonCognates.txt.
I then want to repeat the complete foreach loop eleven (11) times, because I have 11 different rules I need to implement in my program.
What I would like to know now: is that foreach allowed in perl? If so, what is wrong with it, because the output files OutputNonIdenticalCognates.txt andOutputNonCognates.txt are empty. If it's not allowed, how cant I change it so it does the same thing I's like it to do..?
Thank you in advance! :)
|
|---|