mmonroe-ryffine / teamperl

0 stars 0 forks source link

Grepping through an array #3

Open philiprbrenan opened 6 years ago

philiprbrenan commented 6 years ago

Now that you have arrays so beautifully under control we can put them to use: at your convenience, please declare an array like this:

my @a = qw( Paste the contents of the file attached below here as several lines closing with
);

and then count the number of instances of the words 'the' and likewise of 'a'. Say the one that occurs most first and then the other giving counts for each.

firesInPortugal.txt

mmonroe-ryffine commented 6 years ago

Sorry, I didn't see this in my email! Getting to this now...

mmonroe-ryffine commented 6 years ago

How's this? Check out my new firesInPortugal.pl file!

philiprbrenan commented 6 years ago

Your solution is superb - completely different from what I had anticipated - but actually much better. I like the bold use of scalar context on grep to get the count without messing around with for loops.

So, now, if you will, please consider how you would tackle these related problems:

(a) Find the longest word (b) Find the most common word (c) Find the second most common word (d) Find the N'th commonest word where N is an integer given to you on the command line or in a variable at the start of your program. (e) The N'th commonest word for each length of word from 1 to N, that is words of length 1, length 2 etc. are classified separately

I shall be intrigued to see how far you can go without using for.

Please confirm that you have set up your editor so that you can press a key to get a syntax check of your program with the clickable results appearing another editor pane and likewise execution of your program with the program output appearing in an editor pane - as eventually this will save you a lot of time programming, debugging, running.

Nice going!

mmonroe-ryffine commented 6 years ago

I'm working on this one and I'm accidentally writing programs I didn't mean to.

1) I can search for the word that the user inputs and output the number of times it's used. 2) I can count words in the string:

my @words = split / /, @ A;
    print Dumper \@words;

My attempt here was to try to chop the string into smaller ones. For some reason it only works when I use the $ symbol for A. What's this about??

Hopefully a real solution for at least question a is on its way...

philiprbrenan commented 6 years ago

Please see: http://htmlpreview.github.io/?https://github.com/mmonroe-ryffine/teamperl/blob/master/foreach.html

(I do not know how to put html directly into the response)