Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Anagrams & Letter Banks

by dominick_t (Acolyte)
on Oct 27, 2017 at 20:31 UTC ( #1202196=note: print w/replies, xml ) Need Help??


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

Thank you, tybalt89. It's nice to see this regex solution as well as the hash solution offered earlier. I was just reading about backreferences, and again it makes sense to me in principle, but the details remain opaque. What do the parentheses in (.) accomplish? And how does the backreference determine if something has been seen again?

Replies are listed 'Best First'.
Re^3: Anagrams & Letter Banks
by Cristoforo (Curate) on Oct 27, 2017 at 20:41 UTC
    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, (! /.....

    Update

    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.

      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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2022-05-17 06:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (65 votes). Check out past polls.

    Notices?