Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hi perlmonks, nice to meet you. I am practicing my regexes and want to know how many times a word in a string occurs but i am not succeeding. However i did wrote a working example but not by using a regex. Here it is:
use strict; use warnings; my %hash=(); my $re = "iowq john stepy andy anne alic bert stepy anne bert andy ste +p alic andy"; my @names = split(/\s+/,$re); foreach(@names) { $hash{$_}++; } foreach(keys %hash) { print "key: $_ value: $hash{$_} \n"; }
Can someone show me a regex to do all this?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: counting words in string
by haukex (Archbishop) on Aug 07, 2018 at 15:12 UTC | |
There Is More Than One Way To Do It :-) The method you showed is fine. If the input string starts getting long, you might have to watch out for memory usage. Here's a version that uses a regex and a while loop to scan the string, without building an intermediate @names array. I'm using the regex \S+ (one or more non-whitespace characters) to match "names", so that this code should produce the same output as yours regardless of the input string. If you need to match only certain characters in the names, you'd have to tell us more about that (Re: How to ask better questions using Test::More and sample data).
| [reply] [d/l] [select] |
by frazap (Monk) on Aug 08, 2018 at 14:26 UTC | |
Since I'm a not an expert with perl regex, I start digging in the code of haukex with commenting it's original code and with searching a simpler loop. Here we go: I have 3 questions
Cheers François | [reply] [d/l] |
by haukex (Archbishop) on Aug 10, 2018 at 21:44 UTC | |
pos($str)=undef; Note: This may seem like overkill in the simple example I showed, but it might become important if the code is copy and pasted into a larger script, where it's possible that a previous regex operation on $str left its pos set (but we want to start matching at the beginning of the string). Where is the /c modifier documented in the perldoc ? Hm, you're right that the doc seems a little hard to find. There's a pretty good explanation in perlretut's "Global Matching", and a more complex example of the usage of m/\G.../gc in perlop under "\G assertion". How comes that I have to remember pos $str in the second loop ? That would be the effect of the /c modifier: your second loop ends when the regex fails to match. The regex failing to match also resets pos, except when /c is used. Are the two loops equivalent or will the second one failed in some situation ? The way you've written it, no, because \w+ is not the same as \S+: the latter will match any non-word characters too, anything except \s characters. If I change your loop's regex to /(\S+)/g, you can get the "failed to parse $str" error, namely when there's whitespace at the end of $str. This is because the last match doesn't reach all the way to the end of the string, while the regex I showed does, because it includes a \s+ after the \S. The pos==length check makes sense if you want to make sure your loop went all the way to the end of the string and there's no garbage left at the end that we didn't match, but in this case, the only thing that could possibly be left after matching \S+ is whitespace, which we're not interested in. searching a simpler loop I showed the m/\G.../gc technique because I wanted to demonstrate it - it's a great tool for certain types of parsing (one example in the first paragraph of this post, or this), but as long as the OP just wants to split on whitespace, it's admittedly overkill. If you wanted to simplify the loop, you could say this (others have shown variations of this):
But as soon as the matching rule isn't as simple as \S+, things can get tricky. For example, if the rule is \w+, and the input string is " foo bar *", anything that looks for \w+ only will miss the junk at the end of the string. | [reply] [d/l] [select] |
|
Re: counting words in string
by Tux (Canon) on Aug 07, 2018 at 13:45 UTC | |
Simplest I could come up with is (more or less the same what you did)
edit: and here the code inside the regex:
Enjoy, Have FUN! H.Merijn | [reply] [d/l] [select] |
|
Re: counting words in string
by AnomalousMonk (Archbishop) on Aug 07, 2018 at 13:55 UTC | |
What's in a name? As soon as you've figured this out (and it may not be easy), the rest of the problem is fairly simple. For clarity, the example below takes a few steps to do what could be done in a single statement.
Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] |
|
Re: counting words in string
by tybalt89 (Monsignor) on Aug 07, 2018 at 20:03 UTC | |
Hash free ! :)
Outputs:
| [reply] [d/l] [select] |
|
Re: counting words in string
by thanos1983 (Parson) on Aug 07, 2018 at 14:33 UTC | |
Hello Anonymous Monk, Sorry I missed understood the question I will update it. Another possible way (maybe not so efficient) is to use map and List::MoreUtils::uniq.
There is a similar question asked in the forum, see here Count duplicates in array..
Hope this helps, BR.
Seeking for Perl wisdom...on the process of learning...not there...yet!
| [reply] [d/l] [select] |
by AnomalousMonk (Archbishop) on Aug 07, 2018 at 18:30 UTC | |
map($hash{$_}++, @names); You can also use grep($hash{$_}++, @names); as an alternative to map. (But why would you? :) Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] |
|
Re: counting words in string
by Anonymous Monk on Aug 08, 2018 at 08:53 UTC | |
Thank you all for responding, you've been mot helpful. | [reply] |