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! :)


In reply to Comparing hash keys and values with Regular Expressions by SayWhat?!

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.