in reply to Just starting out with Perl

Here are some pointers.

Since you are counting unique words don't use an array, arrays don't care about uniqueness. Use a hash which has unique keys.
Use split to get the words.

If you still have problems post what you have been trying.

Also please use the Perlmonks '<code>' tags to wrap your code in.



grep
One dead unjugged rabbit fish later

Replies are listed 'Best First'.
The Code for counting unique words (help?)
by iridius (Novice) on Oct 22, 2006 at 02:53 UTC
    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...
      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
      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 (' ', ...)