Hi Monks!

I have a subroutine that essentially stores all of the text from files held in a specific folder into a hash (%corpus). The idea is that you specify the folder when you call the subroutine. This subroutine has been written and used by someone with a lot more coding experience than me, so I can be sure that it is not the problem.

What I want to do is 1) call the subroutine (getCorpus); 2) specify the folder that I want it to work on; 3) instruct it to store its output into a new hash (%mycorpus), and then I want to print out the contents of this hash.

I’ve been browsing the PerlMonk archives for any similar situations to see if I could solve the problem myself, and I’ve managed to cobble together the code below from various places (notably Re: easiest way to print the content of a hash?), but I am quite obviously making some big mistakes that I don't have the skills, as of yet, to spot (as evidenced by the various error messages it returns (reproduced below the code)

I haven't uploaded the original subroutine here because I'm not sure about the etiquette of making someone else's code available without their consent. But essentially the subroutine comes first and then I write:

%mycorpus = getCorpus("C:\Users\li\test") #line 1 foreach (sort keys %mycorpus) { print "$_ : $mycorpus{$_}\n"; }

In line 1, I am trying to call the subroutine (named getCorpus), tell getCorpus what folder I want it to operate on, and store the output of the subroutine into a new hash named %mycorpus. Then in lines 2 and 3, I attempt to print the contents of %mycorpus. The error messages returned for this particular attempt are as follows:

Operator or semicolon missing before %mycorpus at get_corpus.pl line + 51. Ambiguous use of % resolved as operator % at get_corpus.pl line 51. Can't modify modulus (%) in scalar assignment at get_corpus.pl line 52 +, near ") foreach " syntax error at get_corpus.pl line 52, near ") {" Execution of get_corpus.pl aborted due to compilation errors.
Any guidance on this would be greatly appreciated.

In reply to Storing output of a subroutine into an hash and then printing hash by Maire

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.