Hello ScarletRoxanne, welcome to PerlMonks and Perl!

See #4 on the Basic debugging checklist: print your data structures. For example, if I add use Data::Dump; dd \%anaHash; just after the first loop, and enter Hello and World, the output I get is:

{ dlorw => ["world"], ehllo => ["hello"] }

Whereas if I enter restful and fluster, I get:

{ eflrstu => ["fluster", "restful"] }

Now if you look at your second loop, you're iterating over values %anaHash, so I hope it becomes clear why the loop is executing twice?

I'm not exactly sure about what your requirements are, but I'm not sure why you need the second loop? Checking that the phrases are identical could be done with eq on $aWord and $bWord, and whether or not the words are anagrams could be checked by looking at the number of keys in the hash, no?

Some more general tips:

I can't get the script to print FALSE by, for example, setting a variable to '' or 0, etc. or saying return ''. Ideally, I wouldn't have to put "print TRUE/FALSE" in the script at all.

I'm not sure what you mean by this - Perl doesn't have any "TRUE" and "FALSE" constants like other languages, instead there are simply a couple of values that Perl interprets as being "false" in a "boolean context" (for example, in an if (...) condition) - see Truth and Falsehood. These values don't get converted internally into anything else. If you want Perl to print the strings TRUE and FALSE, you'll need to tell it to do that, such as in print $value?"TRUE":"FALSE".

I'm curious why my m// expression doesn't work.

If I enter hello and hell o, then the regular expressions will be / +hello/ and / +hell o/. Because both of them require spaces before the h, which neither of the input strings have, neither regex will match. I suspect you may have wanted to match on the keys instead, but even then, I think you need to rethink the overall approach, because there would still be further modifications needed to make it work with regexes. (Hint: I would've made a copy of the strings and removed all the spaces.) Another regex tip: If you don't anchor your regular expressions with ^ and $, then remember that the regular expression will match anywhere in the string (e.g. "foobar"=~/a/ is true).

Update 2019-08-17: Updated the link to "Truth and Falsehood".


In reply to Re: Final Print Statement in Foreach Loop Prints Twice by haukex
in thread Final Print Statement in Foreach Loop Prints Twice by ScarletRoxanne

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.