Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re^3: Anagrams & Letter Banks

by Cristoforo (Curate)
on Oct 27, 2017 at 20:41 UTC ( #1202197=note: print w/replies, xml ) Need Help??

in reply to Re^2: Anagrams & Letter Banks
in thread Anagrams & Letter Banks

Hi dominick_t

The parentheses capture one letter and look for a repeat, \1. He greps for a string which doesn't cause the regular expression to succed, (! /.....


I should have been more thorough in my reply. When there is a parentheses around an expression, in this case '.', \1 refers to that captured value. If an 'a' was captured, then \1 would be a. If a 'd' was captured, then for \1 to match, it would need to find a 'd' somewhere further in the string.

Perl will try to make the match succeed, so it will progress matching each letter until it finds a match. If it does, then the the regexp will succeed. Else, it will fail.

If it fails then the grep succeeds!

There would be a \2 if there were 2 sets of capturing parentheses. \1 would be what was captured in the first parens and \2 would be what was captured in the second set of parens. In this problem there is only one set of parens so only \1 would be involved.

Replies are listed 'Best First'.
Re^4: Anagrams & Letter Banks
by dominick_t (Acolyte) on Oct 27, 2017 at 20:45 UTC
    Thank you, Cristoforo. Would \2 look for two repeats? Does "grep" generally return "true" if it succeeds but here the "!" returns "false" if it succeeds?
      I was just reading the documentation on grep. I'm familiar with using grep in the Terminal simply to find matches using regular expressions. I am less familiar with how grep behaves in Perl. I just added a line of code to the script I'm working on for the letter banks, and it appears to be doing exactly what I need it to. But I think it would only work if somehow the grep were returning true or false (as opposed to returning the actual string which is a match (or non-match)).

      for (sort keys %words) { my @list = sort @{$words{$_}}; next unless @list > 1; next unless grep !/(.).*\1/, @list; print "@list\n";

      I'm trying to print @list only if it contains more than one element, AND it has at least one element which contains no repeat letters. This seems to be working, but again, as I see it, the grep command must be returning true or false for it to work.
        Although they are similar, Unix shell (sh, csh, tcsh, bash, etc.) grep and Perl grep are not exactly the same.

        In Perl, grep takes a list or array of values as input, and outputs those for which the grep command returns true.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2020-09-18 20:54 GMT
Find Nodes?
    Voting Booth?
    If at first I don’t succeed, I …

    Results (113 votes). Check out past polls.