Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

The Code for counting unique words (help?)

by iridius (Novice)
on Oct 22, 2006 at 02:53 UTC ( [id://579807]=note: print w/replies, xml ) Need Help??


in reply to Re: Just starting out with Perl
in thread Just starting out with Perl

Here's what I have so far:
#!/perl/bin/perl -w # text analyzer use strict; use CGI qw/:standard/; print header(); print start_html("This will count your words"); # check for param, if params present, then form is submitted if(param()) { my $fname = param ('fname'); if(!$fname){ dienice("You did not enter your name, please click the back bu +tton\n"); } # code for assigning variables for each param # get all other data and check if present, otherwise dienice my $email = param('email'); if(!$email){ dienice("You did not enter your email, please click the back b +utton\n"); } my $text2count = param('text2count'); # get a text array from text my @textarray = get_text_array($text); # make a hash out of this array @textarray my %texthash = get_counting_hash(@textarray); # after get all of the values needed, print out the content for th +e result: print <<EOHTML1; # I know I need to do a foreach loop here with a table tag before +the loop and and ending table tag after the loop EOHTML1 } else { #print the HTML form out here so we can get the name,email and the + text print <<EOHTML; # THIS IS WHERE THE FORM WILL GO - LEFT THIS OUT TO CHOP CODE +- This I have completed EOHTML } print end_html(); # end of the page right here # subroutine to die - provided by our teacher sub dienice { my ($s) = @_; print $s, "\n"; exit(1); } # make a text array out of the plain text sub get_text_array { return split(/ /, $_[0]); } # building a hash counting words from an array of words # the keys are the words # the values are the number of occurrences of that word in the array sub get_counting_hash { my @wordArray = @_; my %wordHash = (); # I think I need some code here? return %wordHash; }
I'm hazy on the foreach loop to make the table and also on the subroutine get_counting_hash...

Replies are listed 'Best First'.
Re: The Code for counting unique words (help?)
by grep (Monsignor) on Oct 22, 2006 at 04:00 UTC
    Overall not a bad start.

    sub get_counting_hash { my @wordArray = @_; my %wordHash; ## OK so you have an array. You'll need to loop over it. foreach my $word (@wordArray) { ## Now you need to populate the hash ## You assign a value to a hash like ## $wordHash{$key} = $value; ## so you'll need to adapt that to your code } return %wordHash; }


    grep
    One dead unjugged rabbit fish later
Re: The Code for counting unique words (help?)
by lyklev (Pilgrim) on Oct 22, 2006 at 14:27 UTC
    I see a small pitfall in your code.

    You split on spaces using a regular expression. The consequence of this is that the array you get will contain empty (undefined) elements and this will give you headaches in the remainder of the excercise.

    The solution is to split on the character space instead. So

    split (' ', ...)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://579807]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-23 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found