Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
For a person just learning perl, your example code is quite obtuse. You should at least attempt to explain how your code actually works.

I'd do it for you, but need to leave work now...

Update: since no one else has yet done so, and I'm back at work now, I'll try:

So, the $/ floating all over the above post is simply a special variable holding, by default, a "new line" character, ie. "\n". The first section

while(<DATA>){ chomp; $hash{$_}++ for split; }
simply reads in the data, and for every word in the DATA section, increments the hash table entry for that word. It would be more clear to beginners if it were written thusly:
while(<DATA>){ #get a line of text from the DATA area, put it in spec +ial variable $_ chomp; #remove the "new line" character from the $_ variable #create a word array from the $_ variable #split with no options splits on whitespace by default @words = split; foreach $word (@words) { $hash{$word}++; #increment the value pointed to by the $hash{ +$word} "key" } }
Once you have the data read in, you'll have the following data structure:
board => 1 chalk => 1 class => 2 desk => 1 professor => 1 school => 2 students => 2 table => 1 teacher => 3
2teez then uses more advanced perl to essentially do the following:
foreach $word (sort keys %hash) { print "$word\n"; } print "\n"; foreach $word (sort keys %hash) { if ($hash{$word} > 1) { print "$word\n"; } }
From these two beginner friendly examples, it should be pretty easy to do 2teez's 3rd challenge which is to print out the data structure I gave above.


In reply to Re^2: why use a hash instead of an array by 5mi11er
in thread why use a hash instead of an array by DarrenSol

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (6)
    As of 2018-01-21 10:57 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (227 votes). Check out past polls.